我正在尝试绘制组随时间的累积总和,并为每个组添加相应的线性预测线。情节很好,但是,我无法读取 x 轴上预测斜率的日期,因为这些是数字。
如何更改下面的代码以stat_smooth
返回日期而不是数字?
g <- ggplot(aes(x = created_at_day, y = sum, color = groups), data= df) +
geom_line() +
stat_smooth(aes(x = as.Date(created_at_day), y = sum, color = groups), method = "lm", fullrange = TRUE, se = FALSE, size = 0.1) +
xlab('Date') +
ylab('cumulative sum of patients') +
expand_limits(x = as.Date(c("2017-11-13", "2018-04-01"))) +
ggtitle('Number of patients included per practice') +
theme_bw()
g
ggplotly(g)
编辑:这里有一些代码可以生成上面的图。
set.seed(101)
created_at_day= sample(seq(as.Date('2017-01-01'), as.Date('2018-01-01'), by = "day"), 300)
set.seed(101)
groups= sample(1:3,300, replace= TRUE)
df = data.frame(created_at_day, groups)
group1= seq(1, table(groups)[[1]]*1.5, 1.5)
group2= seq(0.8, table(groups)[[2]]*0.8, 0.8)
group3= seq(1, table(groups)[[3]]*1.8, 1.8)
df_g1= subset(df, groups == 1)
df_g1 = arrange(df_g1, created_at_day)
df_g1= cbind(df_g1, sum= group1)
df_g2= subset(df, groups == 2)
df_g2 = arrange(df_g2, created_at_day)
df_g2= cbind(df_g2, sum= group2)
df_g3= subset(df, groups == 3)
df_g3 = arrange(df_g3, created_at_day)
df_g3= cbind(df_g3, sum= group3)
df= rbind(df_g1, df_g2, df_g3)
df$groups= as.factor(df$groups)