【问题标题】:Changing font of part of string in forestplot更改森林图中部分字符串的字体
【发布时间】:2018-01-12 15:33:20
【问题描述】:

下面有一个森林图,是用 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 更改为斜体,将数字保留在正常字体中。这可能吗?

非常感谢。

【问题讨论】:

    标签: r expression forestplot


    【解决方案1】:

    我设法找到了解决方案。这并不容易,因为我需要将矩阵(不能有不同类型的变量)转换为多个列表和子列表。然后可以在新列表中的相应索引处存储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 函数,但是这个方法有效。

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

    【讨论】:

    • 这对我很有帮助,感谢您提供的所有示例!如果其他人需要这样做并且也不是 R 专家:当您需要变量中的整数为斜体时,您可以在 italic 函数中引用它,但必须先将整数转换为字符串。所以例如list(substitute(expression(paste(italic(integer_to_str))), list(integer_to_string=sprintf("%.3f", DF$int_var[1])))))
    猜你喜欢
    • 2021-05-14
    • 1970-01-01
    • 2015-01-18
    • 2021-04-20
    • 1970-01-01
    • 1970-01-01
    • 2011-06-29
    • 2018-01-23
    • 1970-01-01
    相关资源
    最近更新 更多