如何分离y轴标题(ggplot)并放置在单独的网格中?(How to separate the y

编程入门 行业动态 更新时间:2024-10-28 00:22:10
如何分离y轴标题(ggplot)并放置在单独的网格中?(How to separate the y-axis title (ggplot) and place in a separate grid?)

我想把几个地块连在一起。 我已经找到了如何使用鸡蛋对齐地块的解决方案:ggarrange。 这很好地照顾了主要情节。 但我想让所有情节的y.axis标题也一致。 我不能通过使用ggplot选项的通常建议来做到这一点,因为每次运行我的脚本时,数字的范围会有所不同,并且无法提供固定范围,因此轴标题的位置会发生变化。 我知道可以将图例和位置分开放在一个单独的网格中,我想知道我们是否可以对轴做同样的事情?

我在这里创建了一个简单的示例代码。

# example created to explain my problem gdf <- data.frame(a = runif(10, 0, 1), b = runif(10, 100, 1000), c= runif(10, 10000,100000), d= runif(10, 100000,1000000)) p1 <- ggplot(gdf, aes(a, b)) + geom_point() + ylab ("first") p2 <- ggplot(gdf, aes(a, c)) + geom_point() + ylab ("Second") p3 <- ggplot(gdf, aes(a, d)) + geom_point() + ylab ("third" ) egg::ggarrange(p1, p2, p3, ncol= 1, nrow = 3, heights=c(1, 1, 1))

I am trying to attach several plots together. I have found the solution on how to align the plots using egg:ggarrange. This nicely takes care of main plots. But I would like to have the y.axis title of all plots to be aligned also. And I cannot do it through the usual suggestion of working with ggplot options, since everytime I run my scripts the range of the numbers would be different and cannot come up with a fixed range, therefore the location of the axis title change. I know it is possible to separate the legend and place in a separate grid, I was wondering if we could do the same thing with the axis?

I have created a simple sample code here.

# example created to explain my problem gdf <- data.frame(a = runif(10, 0, 1), b = runif(10, 100, 1000), c= runif(10, 10000,100000), d= runif(10, 100000,1000000)) p1 <- ggplot(gdf, aes(a, b)) + geom_point() + ylab ("first") p2 <- ggplot(gdf, aes(a, c)) + geom_point() + ylab ("Second") p3 <- ggplot(gdf, aes(a, d)) + geom_point() + ylab ("third" ) egg::ggarrange(p1, p2, p3, ncol= 1, nrow = 3, heights=c(1, 1, 1))

最满意答案

你可以使用cowplot::plot_grid ,它有一个align参数:

cowplot::plot_grid(p1, p2, p3, align = "v", ncol = 1)

情节与牛仔图

You can use cowplot::plot_grid which has an align argument :

cowplot::plot_grid(p1, p2, p3, align = "v", ncol = 1)

plot with cowplot

更多推荐

本文发布于:2023-08-07 21:15:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1465915.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:网格   标题   ggplot   separate

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!