0

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!

mccurcio
  • 1,294
  • 5
  • 25
  • 44
  • 1
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. You should only use `ggplot()` once per plot. You should be able to add multiple `geoms` though and each geom can have a different `data=` parameter. Though it's usually better to combine data before plotting. – MrFlick Mar 27 '21 at 18:11

2 Answers2

0

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)
Pablo
  • 39
  • 1
  • 7
0

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

  • Now I have a separate question, I'd like to add data points from another variable showing policy changes passed on certain dates. I know I need to add +layer (geom_point) but I'm not sure where to put the new variable name from which the data points are coming from. Any ideas? – Davia Downey Mar 27 '21 at 18:52
  • You should open a new post if you have a new question. Each post on Stack Overflow should be one question/answer. This is not a general discussion forum. If that's something you want, maybe try https://community.rstudio.com/ instead. – MrFlick Mar 27 '21 at 20:42