Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
geejaytee opened this issue Nov 20, 2023 · 1 comment
Open

Comments

@geejaytee
Copy link

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)

@gforge
Copy link
Owner

gforge commented Nov 23, 2023

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.

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

No branches or pull requests

2 participants