在forestplot中更改字符串的一部分的字体 [英] Changing font of part of string in forestplot

查看:1055
本文介绍了在forestplot中更改字符串的一部分的字体的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下面有一个森林图,由R中的forestplot包创建.

在此处输入图片描述

我使用以下代码创建了它:

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")

正如大家所看到的,我设法在第一列中将字体更改为斜体.但是我想做的是将第二列中的r更改为斜体,将数字保留在常规字体中.有可能吗?

非常感谢.

解决方案

我设法找到了解决方案.这并不容易,因为我需要将矩阵(不能具有不同类型的变量)转换为多个列表和子列表.然后可以在相应的索引中将expression存储在新列表中.表达式可以保存不同的格式.不幸的是,它们无法评估变量,因此其他变量的索引必须通过substitute函数完成.非常棘手,我不知道是否有更优雅的方法,但是它可以工作.

另一个也许更简单的选择可能是使用unicode(示例代码中也包括了),但是我发现这很棘手.

希望代码有帮助:

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")

我试图解释代码中的不同步骤.如果要键入手动字符串,则不需要替代函数.要点:每当使用表达式时,都需要创建一个新列表.也许有一种使用parse函数的更优雅的方法,但这是可行的.

因此字段中格式不同的绘图如下:

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.

解决方案

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:

这篇关于在forestplot中更改字符串的一部分的字体的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆