【问题标题】:Splitting axis labels with expressions用表达式分割轴标签
【发布时间】:2012-01-24 10:28:21
【问题描述】:

我有一个带有长标签的图,其中包含一个表达式,我想将它分成两行。
在表达式中添加“\n”,结果与预期不符。

ylabel <- expression("A very long label with text and \n 
expression"*(alpha+beta) [ij]*"  A very long label with text and expression")
curve(x^3 - 3*x, -2, 2, xlab=xlabel)

任何帮助将不胜感激。谢谢

【问题讨论】:

    标签: r graph axis


    【解决方案1】:

    这是另一个解决方案,依赖于atop,就像@AndresT 在他的编辑中所做的那样。 请注意,我们不能在表达式中使用像 \n 这样的控制字符,这就解释了为什么使用像 expression(paste("...\n", alpha[i], "....")) 这样的字符不会产生所需的输出。

    xlabel <- expression(atop("A very long label with text and expression", 
                              paste((alpha+beta)[ij], "   A very long label ...")))
    curve(x^3 - 3*x, -2, 2, sub=xlabel, xlab="")
    

    请注意,我使用 sub 而不是 xlab 以避免与 x 刻度线发生冲突。

    【讨论】:

      【解决方案2】:
      plot(1:10, ann = FALSE)
       mtext(side = 2, text = "Y Axis", line = 3)
       mtext(side = 2, text = "and something extra", line = 2)
      

      对于ggplot2:

      set.seed(124)
      data1 = data.frame(x=1:10,y=rnorm(10,1))
      
      colnames(data1) = c("X", "Y") 
      theme = theme_set(theme_bw()) 
      
      # atop is the function to use in order to get two lines  
      
       xlab = expression(atop(paste("x Axis"),
                  "More text"))
       ylab = expression(atop(paste("y Axis"),
                      "Two lines"))
      
      
      
      
        ggplot(data1, aes(x = X, y = Y))+
                  geom_point(shape = 1, color ="black") +
                       xlab(xlab) +  
                       ylab(ylab)
      
      
      #And adjust the margins with opts.
      

      【讨论】:

      • 感谢安德烈斯!是否可以为 ggplot2 图找到类似的技巧?
      • (+1) 很好地使用了mtextatop
      • ggplot2 你可以使用tableGrob
      【解决方案3】:

      ggplot2 中的示例行:

      ylab(expression(atop(paste(italic("Saxifraga tridactylites")), 
      "individuals per plot")))+
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2021-08-03
        • 2013-03-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多