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

标签 r ggplot2

这个问题在这里已经有了答案:





ggplot2 plot area margins?

(1 个回答)


5年前关闭。




我有几个 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)

最佳答案

您的问题会有更好的解决方案:只需点击链接 user3055034假如。调整 plot.margin与新 margin()类似于我下面的例子。

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))

关于r - 如何操作 R 中的 ggplot 以在 lhs 上为角度 = 45 长 x 轴标签留出额外空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32918947/

相关文章:

reprex 设置输出宽度

r - 从github安装flipMaxDiff软件包时出错

r - 如何在ggplot中应用legend.key背景透明?

R 在 map 上绘制填充的经纬度网格单元

r - 我希望主要/次要网格线的 ggplot 网格线厚度不同

r - 可以获得非标准评估以在 dplyr 中为 filter_ 和 count_ 但不是 distinct_ 工作

r - 如何使用 gtable_add_grob() 不 'add'

r - 连接意味着在箱线图上用一条线

r - `geom_a(stat="b", ... )` and ` stat_b(geom ="a",...)`之间有什么区别吗?

r - 解决错误消息 "step halving factor reduced below minimum in NLS step": adjusting nlsTols not working