Adding a geom_area plot on top of an existing geom_plot

231 Views Asked by At

I have a dataset with two time series variables showing the number of COVID cases in two states and am trying to stack this data on top of each other in the same graphic. I have the first layer just fine:

ggplot(MI_FL_Data, aes(x=realdate, y=FLday))+geom_area(fill="blue") 

but when I add the second command I get an error:

ggplot(MI_FL_Data, aes(x=realdate,y=FLday))
    + geom_area(fill="blue")
    + ggplot(MI_FL_Data, aes(x=realdate,y=MIday))
    + geom_area(fill="red")  

Error: Can't add 'ggplot(MI_FL_Data, aes(x=realdate, y=MIday))' to a ggplot object.*

I assume I don't need ggplot after (fill="blue") but I'm not sure. Any help with code here would be great!

2

There are 2 best solutions below

0
Pablo On

From what I understood, I would try something like this, but I am not sure if it is your desired output.

ggplot(MI_FL_Data, aes(x=realdate, y=FLday))+geom_area(color=State)
2
Davia Downey On

I figured the out with the following code:

ggplot(MI_FL_Data, aes(x=realdate, y=FLday))+geom_area(fill="blue")+geom_area(aes(y=MIday, fill="red")) but the ended up being really busy in the resulting chart. I do have a separate question however. Going back to my original timeseries chart created by using ggplot(MI_FL_Data, aes(x=realdate, y=FLday))+geom_area(fill="blue")+labs(x=NULL, y="Number of Daily COVID Cases", title="Florida COVID Cases") I get the following chart. enter image description here