Well, if it is to put all the SEPARATE graphics on the same screen, this is possible with the gridExtra package. However, depending on the situation, there is the feature of facets or also put all curves in the same graph. Let’s go to each case:
1) Separate graphs on the same screen
library(gridExtra)
p1 <- ggplot(mtcars, aes(mpg, cyl)) + geom_point()
p2 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line()
p3 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="blue")
p4 <- ggplot(mtcars, aes(mpg, cyl)) + geom_line(color="red")
grid.arrange(p1,p2, p3, p4)
2) To show the case using facets, I will use the data set tips, from the reshape2 package
library(tips)
library(ggplot2)
data(tips)
head(tips)
total_bill tip sex smoker day time size
1 16.99 1.01 Female No Sun Dinner 2
2 10.34 1.66 Male No Sun Dinner 3
3 21.01 3.50 Male No Sun Dinner 3
4 23.68 3.31 Male No Sun Dinner 2
5 24.59 3.61 Female No Sun Dinner 4
6 25.29 4.71 Male No Sun Dinner 4
Note that we have the amount of the bill, the tip, the sex, how was the day and etc. Suppose I want to see the relationship between the bill and the fraction of the tip in relation to the bill:
g1 <- ggplot(tips, aes(x=total_bill, y=tip/total_bill)) + geom_point(shape=1)
Let’s use the facets and present the same graphic, only separating by sex, that is, let’s see who tips more in relation to the value of the account, men or women.
g1 + facet_grid(. ~ sex)
3) Finally, let’s put all the curves on the same graph. Still using the same data set tips and using a smoothing function.
ggplot(tips, aes(x=total_bill, y=tip/total_bill, fill=sex, col=sex)) + geom_point(shape=1) + geom_smooth()
The strategy to be used, a 1), a 2) or a 3) depends on each particular case.
Oops, we were doing together ;-), Sorry!
– Flavio Barros