2021年3月28日星期日

Adding points to geom_hline

I am trying to plot a timeseries with a geom_hline using the FL_Actions and the point data for that line. So far, I've been able to add the geom_hline but I am having problems adding the geom_point with the labels for FL_Action where 1 is equal to closing down policies and 2 is equal to opening up policies. I am using this dataset (see sample below):

# A tibble: 22 x 10     Date    Date2   Date3   FLORIDA FLday MICHIGAN MIday FL_Actions MI_Actions realdate       <chr>   <chr>   <chr>     <dbl> <dbl>    <dbl> <dbl>      <dbl>      <dbl> <date>       1 3/6/20  3/6/20  3/6/20        3     0        0     0         NA         NA 2020-03-06   2 3/7/20  3/7/20  3/7/20        7     4        0     0         NA         NA 2020-03-07   3 3/8/20  3/8/20  3/8/20       10     3        0     0         NA         NA 2020-03-08   4 3/9/20  3/9/20  3/9/20       13     3        0     0          1         NA 2020-03-09   5 3/10/20 3/10/20 3/10/20      15     2        0     0         NA          1 2020-03-10   6 3/11/20 3/11/20 3/11/20      24     9        2     2          1         NA 2020-03-11   7 3/12/20 3/12/20 3/12/20      30     6        3     1         NA         NA 2020-03-12   8 3/13/20 3/13/20 3/13/20      45    15       22    19         NA         NA 2020-03-13   9 3/14/20 3/14/20 3/14/20      64    19       35    13         NA         NA 2020-03-14  10 3/15/20 3/15/20 3/15/20     100    36       45    10         NA         NA 2020-03-15  # … with 12 more rows  

This is my current code:

ggplot(MI_FL_Data, aes(realdate, FLday))+  geom_line() +   geom_hline(aes(yintercept = 15000), data=MI_FL_Data, linetype=2) +  geom_hline(aes(yintercept=17000), data=MI_FL_Data, linetype=4) +   geom_point(aes(col=8, 15000)) +   geom_point(aes(col=8,17000)) +   labs(x=NULL, y="Number of Reported Daily COVID Cases", title="State of Florida") +   theme_classic()  

However, I keep getting an error which says: Error: Invalid input: date_trans works with objects of class Date only.

I think I need to add the realdate (I used library(lubridate) to create the realdate variable), but want only dates that are coded 1 to go on one geom_hline and those coded 2 to go on the other geom_hline. Would this look something like:

geom_point(aes(realdate, col=8 if.1) yintercept 17000)  

or something like that?

https://stackoverflow.com/questions/66847523/adding-points-to-geom-hline March 29, 2021 at 08:46AM

没有评论:

发表评论