forestplot
forestplot copied to clipboard
Feature request: different colours in different columns of text
I wish to plot a forest plot using this package with the text labels in the first column being one colour (or a selection of colours), and the remaining columns remaining black. The following MWE generates a plot of the type I wish to modify:
library(forestplot)
cohort <- data.frame(Age = c(43, 39, 34, 55, 70, 59, 44, 83, 76, 44,
75, 60, 62, 50, 44, 40, 41, 42, 37, 35, 55, 46),
Group = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), levels = c("1","2"), class = "factor"))
age.lm <- lm(Age ~ Group, data = cohort)
age.data <- summary(age.lm)$coefficients[2,]
age.data <- rbind(c(0,0,0,1,"Group 1", "n=15"),
c(age.data[1], age.data[1]-age.data[2]*1.95,
age.data[1]+age.data[2]*1.95, age.data[4], "Group 2", "n=7"))
colnames(age.data) <- c("mean","lower","upper","p-val","labeltext","numbers")
age.data <- data.frame(age.data)
age.data$mean <- as.numeric(age.data$mean)
age.data$lower <- as.numeric(age.data$lower)
age.data$upper <- as.numeric(age.data$upper)
age.plot <- forestplot(age.data,
labeltext = c(labeltext,numbers),
boxsize = 0.1,
xlog = FALSE,
clip=c(-20,20),
xticks=c(-20,-10,0,10,20),
txt_gp = fpTxtGp(ticks=gpar(cex=1)),
align=c("l","c","l"))
This generates a monochrome plot, but I want the labels for the groups to be different colours like (mockup):
This is currently not possible using fpColors(text=...), as this colours the text in all columns on each row.
(I am aware it can be done in post-processing, but would prefer a programmatic solution in the script generating this plot)
Sounds doable and like a nice idea. Unfortunately I lack the time to implement this. If you are willing to put in the work I would happily include it to my package. I would add a function like fp_decorate_graph, e.g. fp_decorate_table for this.