Skip to content

Feature request: different colours in different columns of text #61

Description

@geejaytee

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):
fV5ah

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)

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions