0

I'd like to add text to an existing time series plot. The code I used to create the chart is as follows:

gplot(MI_FL_Data, aes(x=realdate, y=FLday)) + 
  geom_area(fill="blue") + 
  labs(x=NULL, y="Number of Daily COVID Cases", title="Florida ")

where x is the date and y is the number of cases each day. Now I'd like to add points where the governor declared actions (this variable is called FL_ClosingActions) and is text. I believe the correct way to do this is to add to the above code

+layer (geom_text)(aes(x=FL_ClosingActions, colour="red")

but I get an Error message: Attempted to create a layer with no stat. Does this mean I need to add (geom_point) somewhere in the code? I'm at a loss and this seems like it should be fairly simple. Any assistance is appreciated.

Phil
  • 7,287
  • 3
  • 36
  • 66
  • 3
    Without knowing the structure of your data it is hard to advice, but I'm pretty sure that in the second code block the syntax is incorrect. I would have expected something like `+ geom_text(aes(x = FL_ClosingActions, y = ???, label = ???), colour = "red")`. – teunbrand Mar 27 '21 at 22:20

1 Answers1

0

I was able to get this to work using the following code:

ggplot(MI_FL_Data, aes(x=realdate, y=FLday))+geom_area(fill="blue")+labs(x=NULL, y="Number of Daily COVID Cases", title="State of Florida")+geom_text(aes(label=FL_Actions), vjust=0, hjust=10)

The only issue I have now is figuring out the legend. I also wish I could reset the x and y axis, but we have 365 dates and the cases range from 0- 15,200 so I'm not quite sure how to do that part. Hmm...enter image description here

  • Davia, welcome to SO. You're much more likely to get help when you help us help you... Please read https://stackoverflow.com/help/how-to-ask and https://stackoverflow.com/help/minimal-reproducible-example and https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. The important bit is to make your question reproducible. One often doesn't even need the entire data, you can also use one of the plenty inbuilt data sets. I find myself often trying to reproduce the problem and then I come to the solution all by myself. – tjebo Mar 28 '21 at 11:44
  • If your question about legends is still open, the first step is to do a thorough google. There are many many questions about legends here on stackoverflow, and beyond, and it's very likely that you will find an answer to your question. For example try google "add legend to ggplot". For the axis try google "scale axis in ggplot". It can be sometimes daunting to find the right information, but with a bit experience you will find it quicker, you will see :) – tjebo Mar 28 '21 at 11:47
  • And now a more direct suggestion for the axis - try a logarithmic scale. – tjebo Mar 28 '21 at 11:49