Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

multiple graphs in one canvas using ggplot2

Tags:

r

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", geom="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.

like image 593
Julio Diaz Avatar asked Mar 07 '11 23:03

Julio Diaz


People also ask

How do I plot multiple graphs on the same page in R?

We can put multiple graphs in a single plot by setting some graphical parameters with the help of par() function. R programming has a lot of graphical parameters which control the way our graphs are displayed. The par() function helps us in setting or inquiring about these parameters.

How do I show multiple Ggplots together?

The function ggarrange() [ggpubr] provides a convenient solution to arrange multiple ggplots over multiple pages. After specifying the arguments nrow and ncol, ggarrange()` computes automatically the number of pages required to hold the list of the plots. It returns a list of arranged ggplots.


2 Answers

You can use grid.arrange() in the gridExtra package like this:

grid.arrange(p1, p2) 
like image 152
Ista Avatar answered Sep 30 '22 05:09

Ista


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.

like image 44
Chase Avatar answered Sep 30 '22 06:09

Chase