如何操作 R 中的 ggplot 以在 lhs 上为角度 = 45 长 x 轴标签留出额外空间?

Sus*_*san 7 r ggplot2

我有几个 geom_bar ggplots,其中我有很长的 x 轴文本名称。如果我以角度 = 90 绘制它们,则图表底部会占用大量空间,因此我尝试使用角度 = 45。这会导致第一个标签的左侧被切断。有没有办法增加左边距?

(不允许发布图片示例)

ggplot(aes(x = cm, y = ahead_aadt),
        data = sbt) + 
   geom_point( ) + geom_line() +
   ggtitle("Ahead AADT Traffic Counts On US 101 in S Santa Barbara Cty") + 
   theme(axis.text.x = element_text(angle=45, size = 9,
     color = "black", face = "plain", vjust = 1, hjust = 1), 
     panel.grid.major.x = element_line(colour = "black", linetype = "dotted")) +
  xlab("Cumulative Mileage") + ylab("Ahead AADT") +
   scale_x_continuous(breaks = sbt$cm,
                      labels =  sbt$description)
Run Code Online (Sandbox Code Playgroud)

Tho*_*s K 8

您的问题会有更好的解决方案:只需按照提供的链接user3055034 即可。类似于我下面的示例plot.margin,使用 newmargin()进行调整。

library(ggplot2)

# long labels
labels <- c(paste(c(letters, letters), collapse = ""), "6", "8")

ggplot(mtcars, aes(as.factor(cyl), mpg)) +
  geom_point() +
  scale_x_discrete(labels = labels) +
  theme(axis.text.x = element_text(angle = 45, size = 9,
        color = "black", face = "plain", vjust = 1, hjust = 1),
        plot.margin = margin(10, 10, 10, 100))
Run Code Online (Sandbox Code Playgroud)


Sus*_*san 2

这可能不是最好的答案,但我在 y 轴标签文本之前添加了几个“\n\n\n”,这使得标签文本更宽。这会将实际绘图及其相关标签移至更右侧,为左侧的文本提供更多空间。

ggplot(aes(x = cm, y = ahead_aadt),
        data = sbt) + 
   geom_point( ) + geom_line() +
   ggtitle("Ahead AADT Traffic Counts On US 101 in S Santa Barbara Cty") + 
   theme(axis.text.x = element_text(angle=45, size = 9,
     color = "black", face = "plain", vjust = 1, hjust = 1), 
     panel.grid.major.x = element_line(colour = "black", linetype = "dotted")) +
  xlab("Cumulative Mileage") + ylab("\n\n\nAhead AADT") +
   scale_x_continuous(breaks = sbt$cm,
                      labels =  sbt$description)
Run Code Online (Sandbox Code Playgroud)