我正在尝试复制nytimes covid案例条形图,但我想使其成为堆叠的条形图。我的问题是7天滚动平均趋势线被我的堆积变量“状态”弄乱了。当然,这种可视化并不理想,但是现在我无法弄清楚它正在使我发疯。如果您不按州分组并删除“ color = states”,则效果很好,如下图所示。
library(dplyr)
library(readr)
library(ggplot2)
library(zoo)
data_url <- "http://covidtracking.com/api/states/daily.csv"
corona <- read_csv(data_url)
corona <- corona %>%
mutate(date=lubridate::parse_date_time(date, "ymd"))
total<-corona %>%
group_by(date,state)%>%
summarise_at(vars(positiveIncrease),sum)%>%mutate(seven_avg= rollmean(positiveIncrease, 7,
align="left",
fill=0))
ggplot(total,aes(x=date,
y=positiveIncrease,fill=state)) +
geom_col()+
geom_line(aes(y = seven_avg),
color = "red",
size = .75)
问题是geom_line
不会为您汇总数据。取而代之的是,您获得一条趋势线,将状态级别上的所有观察结果连接起来,而没有一条总体的趋势汇总线。
简单的解决方案是使用汇总数据集来获取汇总趋势线:
library(dplyr)
library(readr)
library(ggplot2)
library(zoo)
data_url <- "http://covidtracking.com/api/states/daily.csv"
corona <- read_csv(data_url)
corona <- corona %>%
mutate(date = lubridate::parse_date_time(date, "ymd"))
total <- corona %>%
group_by(date, state) %>%
summarise_at(vars(positiveIncrease), sum) %>%
mutate(seven_avg = rollmean(positiveIncrease, 7,
align = "left",
fill = 0
))
overall <- total %>%
group_by(date) %>%
summarise_at(vars(positiveIncrease), sum) %>%
mutate(seven_avg = rollmean(positiveIncrease, 7,
align = "left",
fill = 0
))
ggplot(total, aes(
x = date,
y = positiveIncrease
)) +
geom_col(aes(color = state)) +
geom_line(data = overall, aes(y = seven_avg),
color = "red",
size = .75,
)
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句