Changing font of part of string in forestplot

笑着哭i 提交于 2019-12-11 16:21:37

问题


Below there is a forestplot, created with the package forestplot in R.

enter image description here

I created it using the following code:

library("forestplot")
data(HRQoL)
clrs <- fpColors(box="royalblue",line="darkblue", summary="royalblue")
tabletext <- cbind(rownames(HRQoL$Sweden),
                   paste("r=", sprintf("%.2f", HRQoL$Sweden[,"coef"])))

forestplot(tabletext, 
           txt_gp = fpTxtGp(label = list(gpar(fontfamily = "Times", fontface="italic"),
                                         gpar(fontfamily = "",
                                              col = "blue")),
                            ticks = gpar(fontfamily = "", cex=1),
                            xlab  = gpar(fontfamily = "HersheySerif", cex = 1.5)),
           rbind(HRQoL$Sweden),
           col=clrs,
           xlab="EQ-5D index")

As one can see, I managed to change the fontface to italics for the whole first column. But what I would like to do is to change the r in the second column to italics, leaving the numbers in the normal fontface. Is that possible?

Thanks a lot.


回答1:


I managed to find a solution. It was not easy, as I needed to convert the matrix (which can not have different types of variables) into multiple lists and sublists. Then it is possible to store at the corresponding index an expression in a new list. Expressions can save different formatting. Unfortunately they cannot evaluate variables, so indexing of other variables has to be done via the substitute function. Very tricky, I don't know if there is a more elegant way, but it works.

Another, maybe easier option might be to use unicode (also included in the example code), but I found this to tricky.

Hope the code helps:

library("forestplot")
data(HRQoL)
clrs <- fpColors(box="royalblue",line="darkblue", summary="royalblue")

tabletext <- list(list(), list()) #Creating a list with "sublists" for each column
tabletext[[1]] <- rownames(HRQoL$Sweden)
tabletext[[2]][1] <- list(expression(paste(italic("r"), " = .42"))) #manual way using expression and paste
tabletext[[2]][2] <- list(substitute(expression(paste(italic("r"),"=",r_string)), list(r_string=HRQoL$Sweden[,2]))) #need substitute function to access variables
tabletext[[2]][3] <- list(substitute(expression(paste(italic("r"),"=",r_string)), list(r_string=sprintf("%.3f", HRQoL$Sweden[,3])))) #The substitute functions allows addicitonal manipulation of strings to be put back into the expression
tabletext[[2]][4] <- list(substitute(expression(paste(italic("t")[df],"=",r_string)), list(r_string=sprintf("%.3f", HRQoL$Sweden[,3]), df="23"))) #One can also substitute multiple elements of expression, use subscripts etc. 

tabletext[[1]][2] <- "Use unicode: \u03B2" #Manipulate strings manually, using unicode
tabletext[[1]][4] <- list(expression(bold("Make single line bold"))) #Manipulate strings manually, using unicode


forestplot(tabletext, 
           txt_gp = fpTxtGp(label = list(gpar(fontfamily = "Times", fontface="italic"),
                                         gpar(fontfamily = "",
                                              col = "blue")),
                            ticks = gpar(fontfamily = "", cex=1),
                            xlab  = gpar(fontfamily = "HersheySerif", cex = 1.5)),
           rbind(HRQoL$Sweden),
           col=clrs,
           xlab="EQ-5D index")

I tried to explain the different steps in the code. If you want to type manual strings, you don't need the substitute function. Important: Whenever expression is used, you need to create a new list. Maybe there is a more elegant way using the parse function, but this one works.

So the plot with different formatting within a field looks like this:



来源:https://stackoverflow.com/questions/48229334/changing-font-of-part-of-string-in-forestplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!