【问题标题】:how to use multiple fonts in r plot axis-labels?如何在 r 绘图轴标签中使用多种字体?
【发布时间】:2016-07-14 10:23:50
【问题描述】:

我正在自定义绘图的 x 轴标签。每个标签包含几个值和几个字符,其中一个 ('x') 应该是斜体。尽管标签的内容仅以常规字体显示,但此代码工作正常:

df <- data.frame(sp=c('a', 'b', 'c', 'd', 'e'), n=c(1, 2, 3, 4, 5))
labels <- c()
for(i in 1:nrow(df)){
labels[i] = paste(df$sp[i], '\n(x = ', df$n[i], ')', sep = '')
}
plot(df$n, df$sp, xaxt = 'n')
axis( 1, at = seq( 1, nrow(df) ), labels = labels)

如何将字符x 斜体化?例如,这个对substitute 的调用不起作用:

substitute(paste(df$sp[i], italic('\n(x = '), df$n[i], ')', sep = ''), list(df$sp=df$sp, df$n=df$n) )

【问题讨论】:

    标签: r plot fonts axis-labels


    【解决方案1】:

    一种解决方案是为每个标签调用一次axis,并使用atop() 而不是\n

    plot(df$sp, df$n, xaxt = 'n')
    axis(1, at = 1:nrow(df), labels = rep("", nrow(df)))
    labels <-sapply(1:nrow(df), function(i){
      axis( 1, at = i, line = 2, lty = 0, 
       labels = bquote(atop(.(as.character(df$sp[i])),.("(")~italic(x)~"="~.(i)~")")))
    })
    

    【讨论】:

    • 不幸的是,这不起作用。我希望字符 x 用斜体而不是 df$sp
    • @SantiXGR OK - 修改后的答案
    • 快到了!然而,这最后一段代码.(i~") 应该包含值.(df$n[i]~"),但这个更改不起作用。它既不工作.(as.character(df$n[i]~")
    • 知道了。括号labels = bquote(atop(.(as.character(df$sp[i])),.("(")*italic(n)~"="~.(i)*")")))有问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-21
    • 2012-09-12
    • 1970-01-01
    • 2023-04-06
    • 2018-07-20
    相关资源
    最近更新 更多