英文:
ggplot: Horizontally align of plots of different widths with fixed coordinates
问题
在这个问题的基础上,我试图实现完全相同的效果,但是水平排列而不是垂直排列。
在问题中被接受的解决方案似乎在对齐方式为垂直时不起作用。
有人可以解释为什么吗?以及如何修复它?
这里有一个使用egg
的示例,正如在原问题的答案中建议的那样。
dat1 <- data.frame(y = rep(1:10, 2), x = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
egg::ggarrange(plot1, plot2, nrow = 1)
请注意,左边的图在宽度上失去了它的coord_equal
,以便两个图具有相同的宽度。
英文:
Building on this question, I am trying to do the exact same, but horizontally instead of vertically.
The solution accepted in the question doesn't seem to work when the alignment is vertical.
Can anyone explain why? And how to fix it?
Here's an example unsing egg
, as suggested in the answer to the original question.
dat1 <- data.frame(y = rep(1:10, 2), x = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
egg::ggarrange(plot1, plot2, nrow = 1)
Notice that the plot on the left lost its coord_equal in favour of the two plots having the same width.
答案1
得分: 0
一种选择是使用patchwork
包。
library(ggplot2)
library(patchwork)
dat1 <- data.frame(y = rep(1:10, 2), x = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
使用+
将图形并排放置。
plot1 + plot2
创建于2023-06-08,使用reprex v2.0.2
英文:
One option is to use the patchwork
package.
library(ggplot2)
library(patchwork)
dat1 <- data.frame(y = rep(1:10, 2), x = 1:20)
dat2 <- data.frame(x = 1:10, y = 1:10)
plot1 <- ggplot(dat1, aes(x = x, y = y)) + geom_point() + coord_equal()
plot2 <- ggplot(dat2, aes(x = x, y = y)) + geom_point() + coord_equal()
Use +
to put plots side-by-side.
plot1 + plot2
<!-- -->
<sup>Created on 2023-06-08 with reprex v2.0.2</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论