我开始学习R。我从datasets包中的Iris数据集开始。要绘制索姆图,我需要使用ggplot2包。如何分割地块窗口并绘制两幅图?
我尝试使用下面的代码,但只显示了一个图。
iris=datasets::iris
par(mfrow=c(2,1))
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+ geom_point(size=3)
ggplot(iris, aes(x=Petal.Length,y=Petal.Width,color=Species))+ geom_point(size=3)发布于 2018-11-12 00:28:23
使用win.graph()将窗口拆分为两部分。
由于您没有提供数据集,如果您想创建一个并排的绘图,请根据下面的示例进行尝试。
试试这个:
library(cowplot)
iris1 <- ggplot(iris, aes(x = Species, y = Sepal.Length)) +
geom_boxplot() + theme_bw()
iris2 <- ggplot(iris, aes(x = Sepal.Length, fill = Species)) +
geom_density(alpha = 0.7) + theme_bw() +
theme(legend.position = c(0.8, 0.8))
plot_grid(iris1, iris2, labels = "AUTO")发布于 2018-11-12 04:02:00
由于ggplot2基于grid图形系统而不是基本绘图,因此par无法有效地调整ggplot2绘图,而最新版本的ggplot2已经支持不同绘图的排列,您可以为每个绘图设置标记:
iris=datasets::iris
ggplot(iris, aes(x=Sepal.Length,y=Sepal.Width,color=Species))+ geom_point(size=3) + labs(tag = "A") -> p1
ggplot(iris, aes(x=Petal.Length,y=Petal.Width,color=Species))+ geom_point(size=3) + labs(tag = "B") -> p2
p1 + p2对于更复杂的安排,您可以使用patchwork包来安排它们。
https://stackoverflow.com/questions/53254539
复制相似问题