[英]multiple graphs in one canvas using ggplot2
我試圖基於此表將兩個ggplot2圖合並為一個:
Type RatingA RatingB
1 One 3 36
2 Two 5 53
3 One 5 57
4 One 7 74
5 Three 4 38
6 Three 8 83
我想制作兩個散點圖,其中y軸的等級平均值,x軸上的類型。
這是我創建每個圖形的方式:
p1 <- ggplot(test, aes(x=reorder(Type, RatingA, mean), y=RatingA)) +
stat_summary(fun.y="mean", geom="point")
p2 <- ggplot(test, aes(x=reorder(Type, RatingB, mean), y=RatingB)) +
stat_summary(fun.y="mean", geom="point")
由於p1和p2具有相同的x軸,我希望它們可以垂直排序。 我看着facet_align,但我找不到能做到這一點的東西。
您可以在gridExtra包中使用grid.arrange()
,如下所示:
grid.arrange(p1, p2)
胡里奧
你提到p1和p2具有相同的x軸,但你基於均值進行的重新排序並不能使它們相同。 p1
的軸變為“一 - >二 - >三”,而p2
的軸變為“兩 - >一 - >三”。 這是故意的嗎?
無論如何, ggplot
提供了一些其他解決方案來將這些圖組合成一個,即colour
和faceting
(您可能已經嘗試過了?)。 第一步,無論這些是melt
你的data.frame長格式。 我們將識別id變量“Type”並且melt
假定其余的列將被melted
。
test.m <- melt(test, id.var = "Type")
快速檢查新對象的結構表明大多數都是一致的,除了類型的級別有點不明顯:
> str(test.m)
'data.frame': 12 obs. of 3 variables:
$ Type : Factor w/ 3 levels "One","Three",..: 1 3 1 1 2 2 1 3 1 1 ...
$ variable: Factor w/ 2 levels "RatingA","RatingB": 1 1 1 1 1 1 2 2 2 2 ...
$ value : int 3 5 5 7 4 8 36 53 57 74 ...
所以讓我們重溫水平:
test.m$Type <- factor(test.m$Type, c("One", "Three", "Two"), c("One", "Two", "Three"))
現在為了密謀。 有顏色:
ggplot(test.m, aes(x = Type, y = value, group = variable, colour = variable)) +
stat_summary(fun.y = "mean", geom = "point")
或與方面:
ggplot(test.m, aes(x = Type, y = value, group = variable)) +
stat_summary(fun.y = "mean", geom = "point") +
facet_grid(variable ~ ., scales = "free")
注意我在刻面中使用了scales = "free"
參數,這樣每個繪圖都有自己的刻度。 如果那不是您想要的效果,只需刪除該參數即可。
這是一個老問題,但我最近發現了multiplot
函數,使他的工作非常好。
所述multiplot
功能是從食譜為R:
它本身的功能是:
# Multiple plot function
#
# ggplot objects can be passed in ..., or to plotlist (as a list of ggplot objects)
# - cols: Number of columns in layout
# - layout: A matrix specifying the layout. If present, 'cols' is ignored.
#
# If the layout is something like matrix(c(1,2,3,3), nrow=2, byrow=TRUE),
# then plot 1 will go in the upper left, 2 will go in the upper right, and
# 3 will go all the way across the bottom.
#
multiplot <- function(..., plotlist=NULL, file, cols=1, layout=NULL) {
require(grid)
# Make a list from the ... arguments and plotlist
plots <- c(list(...), plotlist)
numPlots = length(plots)
# If layout is NULL, then use 'cols' to determine layout
if (is.null(layout)) {
# Make the panel
# ncol: Number of columns of plots
# nrow: Number of rows needed, calculated from # of cols
layout <- matrix(seq(1, cols * ceiling(numPlots/cols)),
ncol = cols, nrow = ceiling(numPlots/cols))
}
if (numPlots==1) {
print(plots[[1]])
} else {
# Set up the page
grid.newpage()
pushViewport(viewport(layout = grid.layout(nrow(layout), ncol(layout))))
# Make each plot, in the correct location
for (i in 1:numPlots) {
# Get the i,j matrix positions of the regions that contain this subplot
matchidx <- as.data.frame(which(layout == i, arr.ind = TRUE))
print(plots[[i]], vp = viewport(layout.pos.row = matchidx$row,
layout.pos.col = matchidx$col))
}
}
}
您只需要將此函數提供給您的腳本即可。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.