我想使用ggplot2包并排放置两个图,即执行par(mfrow=c(1,2))的等效操作。
例如,我想让下面两个图以相同的比例并排显示。
x <- rnorm(100)
eps <- rnorm(100,0,.2)
qplot(x,3*x+eps)
qplot(x,2*x+eps)
我需要把它们放到同一个数据帧里吗?
qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()
我想使用ggplot2包并排放置两个图,即执行par(mfrow=c(1,2))的等效操作。
例如,我想让下面两个图以相同的比例并排显示。
x <- rnorm(100)
eps <- rnorm(100,0,.2)
qplot(x,3*x+eps)
qplot(x,2*x+eps)
我需要把它们放到同一个数据帧里吗?
qplot(displ, hwy, data=mpg, facets = . ~ year) + geom_smooth()
当前回答
您可以使用温斯顿张的R食谱下面的多绘图函数
multiplot(plot1, plot2, cols=2)
multiplot <- function(..., plotlist=NULL, cols) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# Make the panel
plotCols = cols # Number of columns of plots
plotRows = ceiling(numPlots/plotCols) # Number of rows needed, calculated from # of cols
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(plotRows, plotCols)))
vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
# Make each plot, in the correct location
for (i in 1:numPlots) {
curRow = ceiling(i/plotCols)
curCol = (i-1) %% plotCols + 1
print(plots[[i]], vp = vplayout(curRow, curCol ))
}
}
其他回答
根据我的经验,网格。如果您试图在循环中生成情节,那么Arrange工作得很好。
简短代码片段:
gridExtra::grid.arrange(plot1, plot2, ncol = 2)
**更新此注释以展示如何在for循环中使用grid.arrange()为类别变量的不同因素生成图表。
for (bin_i in levels(athlete_clean$BMI_cat)) {
plot_BMI <- athlete_clean %>% filter(BMI_cat == bin_i) %>% group_by(BMI_cat,Team) %>% summarize(count_BMI_team = n()) %>%
mutate(percentage_cbmiT = round(count_BMI_team/sum(count_BMI_team) * 100,2)) %>%
arrange(-count_BMI_team) %>% top_n(10,count_BMI_team) %>%
ggplot(aes(x = reorder(Team,count_BMI_team), y = count_BMI_team, fill = Team)) +
geom_bar(stat = "identity") +
theme_bw() +
# facet_wrap(~Medal) +
labs(title = paste("Top 10 Participating Teams with \n",bin_i," BMI",sep=""), y = "Number of Athletes",
x = paste("Teams - ",bin_i," BMI Category", sep="")) +
geom_text(aes(label = paste(percentage_cbmiT,"%",sep = "")),
size = 3, check_overlap = T, position = position_stack(vjust = 0.7) ) +
theme(axis.text.x = element_text(angle = 00, vjust = 0.5), plot.title = element_text(hjust = 0.5), legend.position = "none") +
coord_flip()
plot_BMI_Medal <- athlete_clean %>%
filter(!is.na(Medal), BMI_cat == bin_i) %>%
group_by(BMI_cat,Team) %>%
summarize(count_BMI_team = n()) %>%
mutate(percentage_cbmiT = round(count_BMI_team/sum(count_BMI_team) * 100,2)) %>%
arrange(-count_BMI_team) %>% top_n(10,count_BMI_team) %>%
ggplot(aes(x = reorder(Team,count_BMI_team), y = count_BMI_team, fill = Team)) +
geom_bar(stat = "identity") +
theme_bw() +
# facet_wrap(~Medal) +
labs(title = paste("Top 10 Winning Teams with \n",bin_i," BMI",sep=""), y = "Number of Athletes",
x = paste("Teams - ",bin_i," BMI Category", sep="")) +
geom_text(aes(label = paste(percentage_cbmiT,"%",sep = "")),
size = 3, check_overlap = T, position = position_stack(vjust = 0.7) ) +
theme(axis.text.x = element_text(angle = 00, vjust = 0.5), plot.title = element_text(hjust = 0.5), legend.position = "none") +
coord_flip()
gridExtra::grid.arrange(plot_BMI, plot_BMI_Medal, ncol = 2)
}
下面包含了上面for循环中的一个样例图。 上述循环将为BMI类别的所有级别生成多个图。
样本图像
如果您希望在for循环中看到grid.arrange()的更全面的使用,请访问https://rpubs.com/Mayank7j_2020/olympic_data_2000_2016
还有一个多面板图形包是值得一提的。看看这个答案。
library(ggplot2)
theme_set(theme_bw())
q1 <- ggplot(mtcars) + geom_point(aes(mpg, disp))
q2 <- ggplot(mtcars) + geom_boxplot(aes(gear, disp, group = gear))
q3 <- ggplot(mtcars) + geom_smooth(aes(disp, qsec))
q4 <- ggplot(mtcars) + geom_bar(aes(carb))
library(magrittr)
library(multipanelfigure)
figure1 <- multi_panel_figure(columns = 2, rows = 2, panel_label_type = "none")
# show the layout
figure1
figure1 %<>%
fill_panel(q1, column = 1, row = 1) %<>%
fill_panel(q2, column = 2, row = 1) %<>%
fill_panel(q3, column = 1, row = 2) %<>%
fill_panel(q4, column = 2, row = 2)
figure1
# complex layout
figure2 <- multi_panel_figure(columns = 3, rows = 3, panel_label_type = "upper-roman")
figure2
figure2 %<>%
fill_panel(q1, column = 1:2, row = 1) %<>%
fill_panel(q2, column = 3, row = 1) %<>%
fill_panel(q3, column = 1, row = 2) %<>%
fill_panel(q4, column = 2:3, row = 2:3)
figure2
由reprex包(v0.2.0.9000)于2018-07-06创建。
还可以考虑ggpubr包中的ggarrange。它有很多好处,包括在情节之间对齐轴和将常见图例合并为一个图例的选项。
是的,我认为你需要适当地安排你的数据。一种方法是:
X <- data.frame(x=rep(x,2),
y=c(3*x+eps, 2*x+eps),
case=rep(c("first","second"), each=100))
qplot(x, y, data=X, facets = . ~ case) + geom_smooth()
我相信在plyr或重塑中有更好的技巧——我仍然没有真正跟上速度 哈德利设计的这些强大的软件包。
并排的任意ggplot(或网格上的n个plot)
gridExtra包中的grid.arrange()函数将组合多个图;这就是把两个放在一起的方法。
require(gridExtra)
plot1 <- qplot(1)
plot2 <- qplot(1)
grid.arrange(plot1, plot2, ncol=2)
当两个图不是基于相同的数据时,这很有用,例如,如果您想在不使用重塑()的情况下绘制不同的变量。
这将把输出作为副作用绘制出来。要将副作用打印到文件中,请指定一个设备驱动程序(如pdf、png等)。
pdf("foo.pdf")
grid.arrange(plot1, plot2)
dev.off()
或者,将arrangeGrob()与ggsave()结合使用,
ggsave("foo.pdf", arrangeGrob(plot1, plot2))
这相当于使用par(mfrow = c(1,2))绘制两个不同的图。这不仅节省了整理数据的时间,而且当你想要两个不同的图时,这是必要的。
附录:facet的使用
切面有助于为不同的群体制作相似的图。下面的许多回答都指出了这一点,但我想用与上面的图等效的例子来强调这种方法。
mydata <- data.frame(myGroup = c('a', 'b'), myX = c(1,1))
qplot(data = mydata,
x = myX,
facets = ~myGroup)
ggplot(data = mydata) +
geom_bar(aes(myX)) +
facet_wrap(~myGroup)
更新
cowplot中的plot_grid函数值得作为grid.arrange的替代。参见下面@claus-wilke的回答和这个小插图,了解等效的方法;但该功能允许基于此小插图对地块位置和大小进行更精细的控制。