multiple graphs in one canvas using ggplot2
I am trying to merge two ggplot2 plots into one based on this table:
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
I want to make two scatter plots with the mean of the ratings in the y axis and type on the x axis.
This is how I create each graph:
p1 <- ggplot(test, aes(x=reorder(Type, RatingA, mean), y=RatingA)) +
stat_summary(fun.y="mean", geo开发者_如何学JAVAm="point")
p2 <- ggplot(test, aes(x=reorder(Type, RatingB, mean), y=RatingB)) +
stat_summary(fun.y="mean", geom="point")
Since p1 and p2 have the same x axis I would like them to be ordered vertically. I looked at facet_align but I couldnt find something that would do the job.
You can use grid.arrange()
in the gridExtra package like this:
grid.arrange(p1, p2)
Julio,
You mention that p1 and p2 have the same x-axis, but the reordering you do based on mean does not make them the same. p1
's axis goes "one --> two --> three" while p2
's axis goes "two --> one --> three". Is this intentional?
Regardless, ggplot
offers a few other solutions to combine these plots into one, namely colour
and faceting
(which you may have already tried?). The first step to either of these is to melt
your data.frame to long format. We will identify the id variable "Type" and melt
assumes the rest of the columns are to be melted
.
test.m <- melt(test, id.var = "Type")
A quick check of the structure of the new object indicates most everything is in line, except the levels for type are a bit out of whack:
> 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 ...
So let's rearrage the levels:
test.m$Type <- factor(test.m$Type, c("One", "Three", "Two"), c("One", "Two", "Three"))
Now for the plotting. With colour:
ggplot(test.m, aes(x = Type, y = value, group = variable, colour = variable)) +
stat_summary(fun.y = "mean", geom = "point")
or with facets:
ggplot(test.m, aes(x = Type, y = value, group = variable)) +
stat_summary(fun.y = "mean", geom = "point") +
facet_grid(variable ~ ., scales = "free")
Note I used the scales = "free"
argument in the faceting so that each plot has its' own scale. Simply remove that argument if that's not the effect you want.
this is an old question, but I recently found multiplot
function, with make his job very well.
The multiplot
function is from Cookbook for R:
The function it self is:
# 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))
}
}
}
You need just source this function to your script.
精彩评论