Search code examples
rplotggplot2facet-grid

How do I add a line graph from another dataset to an existing line graph where facet grid has been used?


I am plotting a graph in RStudio using ggplot2. My R codes for the plot stand as follows:

g1 <- ggplot(mydata1, aes(x=month(date), y=rn)) + 
      geom_line() +
      geom_point(color="red", size=3) +
      geom_smooth(se=FALSE) +
      facet_grid(market ~ year(date))

g1 + theme_solarized() +
     scale_colour_solarized("blue")

The above codes give the following output:

plot1

I need to add another line graph into the above plot and the data will come from another dataset (let's call it mydata2), which is in the same format structure as mydata1. This second line graph won't need to have the smoothed line as in the existing graph. However. I will need the line graph to be of a different color than the existing line graph.

Here is how mydata2 looks:

> head(mydata2)

     date    rn   market
1 1/1/2015  100    OTA
2 2/1/2015  127    OTA
3 3/1/2015  100    OTA
4 4/1/2015  100    OTA
5 5/1/2015   89    OTA

How can I achieve this?


Solution

  • You can pass mydata2 as the data= argument in a separate geom_line() call:

    + geom_line(data = mydata2, aes(x = month(date) , y = rn))