使用ggplot2绘制条形图上的折线图

时间:2020-07-22 17:20:26

标签: r ggplot2 data-visualization

I would like to plot the line right on each bar, but there is strange reason that the line is inside the bars this is overview of dataset the numbers are the moving averages

我想展示包括折线图和条形图的数据可视化。 你们能告诉我我犯了什么样的错误吗? 我猜是否是因为移动加权平均值导致了此问题?

rollmean_covid  <- covid_ts1  %>% filter(Nation == "England") %>% select(date, daily_pos_num) %>% 
    mutate(pos_num01 = rollmean(daily_pos_num, k = 3, fill = NA),
           pos_num02 = rollmean(daily_pos_num, k = 5, fill = NA),
           pos_num03 = rollmean(daily_pos_num, k = 7, fill = NA),
           pos_num04 = rollmean(daily_pos_num, k = 10, fill = NA),
           pos_num05 = rollmean(daily_pos_num, k = 14, fill = NA))

rollmean_covid_metric <- rollmean_covid %>% gather(metric, number, pos_num01:pos_num05)
rollmean_covid_metric %>% filter(metric == "pos_num01") %>% 
  ggplot() + 
  geom_line(aes(date, number), col = "Blue") +
  geom_col(aes(date, number), fill = "orange", alpha = 0.7)

edited1:下面提供了数据集概述。

# A tibble: 10 x 7
   date       daily_pos_num pos_num01 pos_num02 pos_num03 pos_num04 pos_num05
   <date>             <dbl>     <dbl>     <dbl>     <dbl>     <dbl>     <dbl>
 1 2020-01-30             2    NA          NA      NA          NA      NA    
 2 2020-01-30             0     0.667      NA      NA          NA      NA    
 3 2020-01-31             0     0           0.4    NA          NA      NA    
 4 2020-01-31             0     0           0       0.286      NA      NA    
 5 2020-02-01             0     0           0       0           0.3    NA    
 6 2020-02-01             0     0           0       0.143       0.1    NA    
 7 2020-02-02             0     0           0.2     0.143       0.1     0.286
 8 2020-02-02             0     0.333       0.2     0.143       0.2     0.143
 9 2020-02-03             1     0.333       0.2     0.143       0.2     0.143
10 2020-02-03             0     0.333       0.2     0.286       0.2     0.143

1 个答案:

答案 0 :(得分:2)

您对一个日期有多个观察结果,默认情况下position = "stack"的{​​{1}}就是这样,这就是条形图更高的原因。您可能想要geom_col,这应该可以工作:

position = "identity"

编辑以添加:正如@ chemdork123在注释中指出的那样,如果一个日期具有多个值,则可能不会给出期望的结果。通常,针对这些类型问题的最佳解决方案是在将数据传送到rollmean_covid_metric %>% filter(metric == "pos_num01") %>% ggplot(aes(date, number)) + geom_line(color = "blue") + geom_col(position = "identity", fill = "orange", alpha = 0.7) 调用之前将数据整理为正确的形状。