【问题标题】:How to change the position of long axis titles in ggtern?如何更改ggtern中长轴标题的位置?
【发布时间】:2019-08-01 17:08:57
【问题描述】:

我在ggtern 编码的三元图中有长轴标题名称。我无法让 R 和 L(基线)标题移入(从它们被切断的地方)。顶部标题看起来不错,但其他两个则不然。

我尝试使用axis.title.x = element_text(margin = margin(t = 0, r = 0, b = 0, l = 0)) 并更改了定位变量,但这似乎不起作用。所以我不知道该怎么做。

#library(tidyverse)
#library(ggtern)

confidencebreaks <- c(0.95)
x  <- runif(1000,min = 0, max = 1)
y  <- runif(1000,min = 0, max = 1)
z  <- runif(1000,min = 0, max = 1)
df <- tibble(x,y,z)

tern1 <- 
  ggtern(data = df,
         mapping = aes(x = z, y = y, z = z)
  ) +
  labs(title = "A title", 
       subtitle = "A subtitle",
       x = expression(paste(atop("Title 2", 
                                 "A long line 2 that goes on and on"))), 
       y = expression(paste(atop("Title 0", 
                                 "A long line 2 that goes on and on"))),
       z = expression(paste(atop("Title 1", 
                                 "A long line 2 that goes on and on")))
  ) + 
  theme(axis.title = element_text(size=10)) 
print(tern1)

上面的代码重现了截断和长轴标题的问题。我希望能够将“Title 2”和“Title 1”的长轴标题向内移动,但没有成功。

【问题讨论】:

  • 很好地创建了一个reprex。小建议,您也可以使用x &lt;- y &lt;- z &lt;- runif(1000,min = 0, max = 1),但这是风格问题,与您的问题无关。

标签: r ggplot2 ggtern


【解决方案1】:

因此,要调整 ggtern 上的标签,您应该使用 tern.axis.title 并指定哪一侧是 R 代表右侧,L 代表左侧,T 代表顶部。在您的问题中,您可以按如下方式调整标签

    library(tidyverse)
    library(ggtern)

    confidencebreaks <- c(0.95)
    x  <- runif(1000,min = 0, max = 1)
    y  <- runif(1000,min = 0, max = 1)
    z  <- runif(1000,min = 0, max = 1)
    df <- tibble(x,y,z)

    df

    ggtern(data = df,
             mapping = aes(x = z, y = y, z = z)
      ) +
      labs(title = "A title", 
           subtitle = "A subtitle")+
           xlab("Title 2 \n A long line 2 that goes on and on")+ 
           ylab("Title 0 \n A long line 2 that goes on and on")+
           zlab("Title 1 \n A long line 2 that goes on and on")+ 
      theme(tern.axis.title.L = element_text(hjust = 0),
            tern.axis.title.R = element_text(hjust = 1))

这将导致这样的情节

【讨论】:

    【解决方案2】:

    除了 Sebastian 提供的解决方案之外,以下是替代方案,具体取决于您的特定用户案例:

    library(ggtern)
    x  <- runif(1000,min = 0, max = 1)
    y  <- runif(1000,min = 0, max = 1)
    z  <- runif(1000,min = 0, max = 1)
    df = data.frame(x,y,z)
    ggtern(data = df, 
           mapping = aes(x = z, y = y, z = z)) +
      labs(title = "A title",  
           subtitle = "A subtitle",
           x = "Title 2", xarrow="A long line 2 that goes on and on",
           y = "Title 0", yarrow="A long line 0 that goes on and on",
           z = "Title 1", zarrow="A long line 1 that goes on and on"
      ) + 
      theme_showarrows()
    

    【讨论】:

      猜你喜欢
      • 2018-12-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多