【问题标题】:How to add a point or marker for the last date in a time series如何为时间序列中的最后一个日期添加点或标记
【发布时间】:2020-10-15 08:55:03
【问题描述】:

在下面的代码中,我试图将每个变量的最后一个数据点转换为一个点或不同的标记。我应该做哪些修改来实现这一点?

library(ggplot2)
ggplot(economics_long, aes(date, value))+ geom_line() + facet_wrap(~variable, scales = "free_y", ncol = 1)

【问题讨论】:

    标签: r ggplot2 facet


    【解决方案1】:

    只需添加一个geom_point 层,您可以在其中使用为最后日期过滤的数据集,例如

    library(ggplot2)
    library(dplyr)
    
    ggplot(economics_long, aes(date, value)) + 
      geom_line() +
      geom_point(data = group_by(economics_long, variable) %>% filter(date == last(date))) +
      facet_wrap(~variable, scales = "free_y", ncol = 1)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多