我有一个带有x轴日期的数据框,想将计数绘制为条形图(垂直),将均值,lower_conf和upper_conf绘制为水平线。以下是我的数据框。
Date Count Mean X95_lower_conf X95_upper_conf
0 2018-11-01 1 5.225806 1.494764 8.956849
1 2018-11-02 0 5.225806 1.494764 8.956849
2 2018-11-03 21 5.225806 1.494764 8.956849
3 2018-11-04 1 5.225806 1.494764 8.956849
4 2018-11-05 0 5.225806 1.494764 8.956849
5 2018-11-06 0 5.225806 1.494764 8.956849
我尝试了许多不同的方法来实现此目的,但是我遇到了问题,这是我尝试过的一种方法:
plot_ly(df1, x = ~Date, y = ~Mean, name = 'Mean') %>%
add_trace(y = ~X95_upper_conf, name = 'Upper Confidence', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~X95_lower_conf, name = 'Lower Confidence', type = 'scatter', mode = 'lines') %>%
add_trace(y = ~Count, name = 'Count', type = 'bar',mode = 'lines')
任何对此进行调整以获取所需输出的帮助将不胜感激!
答案 0 :(得分:3)
使用ggplot2
:
library(ggplot2)
ggplot(df1) +
geom_col(aes(Date, Count)) +
geom_hline(aes(yintercept = Mean)) +
geom_hline(aes(yintercept = X95_lower_conf)) +
geom_hline(aes(yintercept = X95_upper_conf))