首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >更改forestplot中部分字符串的字体

更改forestplot中部分字符串的字体
EN

Stack Overflow用户
提问于 2018-01-12 23:33:20
回答 1查看 2K关注 0票数 1

下面是用R中的包forestplot创建的forestplot。

enter image description here

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

代码语言:javascript
复制
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改为斜体,将数字保留为普通字体。这有可能吗?

非常感谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-01-18 02:34:25

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

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

希望代码能有所帮助:

代码语言:javascript
复制
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函数的更优雅的方法,但这一种方法可以用。

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

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48229334

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档