在forestplot中更改字符串的一部分字体

时间:2018-01-12 15:33:20

标签: r expression forestplot

下面是一个森林图,使用R中的包forestplot创建。

enter image description here

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

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更改为斜体,将数字保留为普通字体。这可能吗?

非常感谢。

1 个答案:

答案 0 :(得分:0)

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

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

希望代码有所帮助:

substitute

我试着解释代码中的不同步骤。如果要键入手动字符串,则不需要替换功能。要点:每当使用表达式时,您都需要创建一个新列表。也许使用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") 函数有一种更优雅的方式,但这个方法有效。

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