【问题标题】:How to prevent the y-axis from squishing the labels when using facet_wrap in ggplot/ggplotly?在 ggplot/ggplotly 中使用 facet_wrap 时如何防止 y 轴挤压标签?
【发布时间】:2021-12-07 14:16:49
【问题描述】:

在我的示例中,我试图让 y 轴上的标签不被挤压在一起。有没有办法使用 ggplot2 或 ggplotly 来做到这一点?这是一个可重现的示例:

mpg2 <- subset(mpg, manufacturer %in% c("audi", "chevrolet", 
"dodge","ford","honda","hyundai","jeep","land rover","lincoln","mercury","nissan", "pontiac", 
 "subaru",  "volkswagen"))

base <- ggplot(mpg2, aes(displ, hwy)) + 
geom_blank() + 
xlab(NULL) + 
ylab(NULL)
ggplotly(base + facet_wrap(~manufacturer, ncol = 1))

请注意:我不想让 ncol 超过 1。我想保持这种方式。我试图防止它被挤在一起。谢谢!

【问题讨论】:

  • 没有空间了。
  • 有没有办法减小y标签本身的大小?
  • 是的,base + theme(axis.text.y=element_text(size=rel(0.5))。将rel 值设置为您想要的任何值。您还可以相对于水平尺寸增加输出图的垂直尺寸。如何做到这一点取决于您使用的输出方法。
  • 抱歉,我指的是 y_axis 上的实际数字。这只是改变y轴标题的大小吗?对于造成的混乱,我深表歉意。
  • 没关系,这似乎有效。谢谢!

标签: r ggplot2 ggplotly


【解决方案1】:

感谢@eipi10 的评论

mpg2 <- subset(mpg, manufacturer %in% c("audi", "chevrolet", 
"dodge","ford","honda","hyundai","jeep","land rover","lincoln","mercury","nissan", "pontiac", 
 "subaru",  "volkswagen"))

base <- ggplot(mpg2, aes(displ, hwy)) + 
geom_blank() + 
xlab(NULL) + 
ylab(NULL)

ggplotly(base + facet_wrap(~manufacturer, ncol = 1) + theme(axis.text.y=element_text(size=rel(0.5)))

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-07-28
    • 1970-01-01
    • 2021-03-07
    • 1970-01-01
    • 2021-10-26
    • 1970-01-01
    • 2021-12-13
    • 1970-01-01
    相关资源
    最近更新 更多