Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine 2 plots (ggplot) into one plot?

By using R, is it possible to place 2 ggplot together (i.e., on the same plot)? I wish to show a trend from 2 different data frames and instead of putting them one next to the other, I'd like to integrate them together in one plot and only to change the color of one of them (the black dot).

To be more specific, I have the following 2 visuals:

ggplot(visual1, aes(ISSUE_DATE,COUNTED)) + geom_point() + geom_smooth(fill="blue", colour="darkblue", size=1) 

and

ggplot(visual2, aes(ISSUE_DATE,COUNTED)) + geom_point() + geom_smooth(fill="red", colour="red", size=1) 

They look like this (both have black dots and I'll need to change one of them to something different):

enter image description here

and

enter image description here

like image 987
adhg Avatar asked Jan 17 '14 17:01

adhg


People also ask

How do I put Ggplot together?

To arrange multiple ggplot2 graphs on the same page, the standard R functions - par() and layout() - cannot be used. The basic solution is to use the gridExtra R package, which comes with the following functions: grid. arrange() and arrangeGrob() to arrange multiple ggplots on one page.

How do I overlay two plots in R?

Overlaying is a technique that is used to draw multiple plots on a single frame. To draw multiple plots in the R Language, we draw a basic plot and add an overlay line plot or scatter plot by using the lines() and the points() function.

How do I save multiple Ggplots?

For this function, we simply specify the different ggplot objects in order, followed by the number of columns (ncol) and numebr of rows (nrow). This function is awesome at aligning axes and resizing figures. From here, we can simply save the arranged plot using ggsave() .


2 Answers

Creating a single combined plot with your current data set up would look something like this

p <- ggplot() +       # blue plot       geom_point(data=visual1, aes(x=ISSUE_DATE, y=COUNTED)) +        geom_smooth(data=visual1, aes(x=ISSUE_DATE, y=COUNTED), fill="blue",         colour="darkblue", size=1) +       # red plot       geom_point(data=visual2, aes(x=ISSUE_DATE, y=COUNTED)) +        geom_smooth(data=visual2, aes(x=ISSUE_DATE, y=COUNTED), fill="red",         colour="red", size=1) 

however if you could combine the data sets before plotting then ggplot will automatically give you a legend, and in general the code looks a bit cleaner

visual1$group <- 1 visual2$group <- 2  visual12 <- rbind(visual1, visual2)  p <- ggplot(visual12, aes(x=ISSUE_DATE, y=COUNTED, group=group, col=group, fill=group)) +       geom_point() +       geom_smooth(size=1) 
like image 125
Stuples Avatar answered Sep 19 '22 22:09

Stuples


Dummy data (you should supply this for us)

visual1 = data.frame(ISSUE_DATE=runif(100,2006,2008),COUNTED=runif(100,0,50)) visual2 = data.frame(ISSUE_DATE=runif(100,2006,2008),COUNTED=runif(100,0,50)) 

combine:

visuals = rbind(visual1,visual2) visuals$vis=c(rep("visual1",100),rep("visual2",100)) # 100 points of each flavour 

Now do:

 ggplot(visuals, aes(ISSUE_DATE,COUNTED,group=vis,col=vis)) +     geom_point() + geom_smooth() 

and adjust colours etc to taste.

enter image description here

like image 28
Spacedman Avatar answered Sep 19 '22 22:09

Spacedman