【问题标题】:Adding points to geom_hline向 geom_hline 添加点
【发布时间】:2021-03-29 00:46:57
【问题描述】:

我正在尝试使用 FL_Actions 和该线的点数据绘制带有 geom_hline 的时间序列。到目前为止,我已经能够添加 geom_hline,但是在添加带有 FL_Action 标签的 geom_point 时遇到问题,其中 1 等于关闭策略,2 等于开放策略。我正在使用这个数据集(参见下面的示例):

# 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

这是我当前的代码:

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()

但是,我不断收到一条错误消息:错误:输入无效:date_trans 仅适用于 Date 类的对象。

我想我需要添加 realdate(我使用 library(lubridate) 创建 realdate 变量),但只希望编码为 1 的日期在一个 geom_hline 上,而那些编码为 2 的日期在另一个 geom_hline 上。这看起来像:

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

或者类似的?

【问题讨论】:

    标签: r ggplot2


    【解决方案1】:

    geom_point(aes(col=8, 15000)) 行生成错误。我不确定那应该做什么。我也不明白为什么当 y 轴上的值在 0-36 范围内时,你的 yintercept 高达 15000,17000(至少对于显示的数据)。

    这可行,但我不知道你是否有这个想法。

    library(ggplot2)
    
    ggplot(MI_FL_Data, aes(realdate, FLday))+
      geom_line() + 
      geom_hline(aes(yintercept = 15), linetype=2) + 
      geom_hline(aes(yintercept=17), linetype=4) + 
      labs(x=NULL, y="Number of Reported Daily COVID Cases", 
           title="State of Florida") + 
      theme_classic()
    

    【讨论】:

    • 佛罗里达州的每日病例范围为 0-16,000。 sn-p 只有大流行初期的 36 行数据。
    • 我不太确定如何发布图片,但如果您点击此链接,您将看到当前迭代:drive.google.com/file/d/17aDicp2Ul2jOM-PvvkpSnlVME5cjUgpH/…。我要做的是在 geom_hlines 上的时间序列上方获取 1 和 2 标签。
    【解决方案2】:

    看来您的日期列实际上已格式化为字符。你能把它们改成最新的,看看是否有帮助?您可以mutate 他们并使用 `as.Date(Date, format = "%m/%d/%y") 覆盖它们。至于第二点,例如,您可以在各个调用中提供数据参数和过滤器。

    【讨论】:

    • 我有一个名为 realdate 的变量,我可以使用它是使用 lubridate 创建的,但我不确定在各个调用中提供数据参数和过滤器是什么意思?你的意思是 geom_point 线吗?它(realdate)已经在那行代码中。
    • 抱歉,我可能误读了您最初使用的列。但是对于 geom_point 线是可以的,例如geom_hline(data = MI_FL_data %&gt;% filter(FL_actions == 2), aes(yintercept = 15000), linetype=2)
    • 这是我的代码:ggplot(MI_FL_Data, aes(realdate, FLday))+ geom_line() + geom_hline(data = MI_FL_Data %&gt;% filter(FL_Actions == 1), aes(yintercept = 15000), linetype=2)+ geom_hline(data = MI_FL_Data %&gt;% filter(FL_Actions == 2), aes(yintercept = 17000), linetype=2) + geom_point(aes(realdate, 15000)) + geom_point(aes(realdate,17000)) + geom_text(aes(label=FL_Actions), check_overlap=TRUE)+geom_label(data=MI_FL_Data, aes(label=FL_Actions), nudge_x = 0.50, nudge_y=.25, size=2)+labs(x=NULL, y="Number of Reported Daily COVID Cases", title="State of Florida")+ theme_classic()
    猜你喜欢
    • 2018-12-22
    • 2019-08-21
    • 2014-01-21
    • 2020-05-13
    • 2013-06-10
    • 1970-01-01
    • 1970-01-01
    • 2017-12-04
    相关资源
    最近更新 更多