我想知道是否有可能根据另一个dataframe的内容动态地为ggplot生成geom_vlines和注释。
我目前有以下结构的数据,用于绘制时间序列:
$ Date : Date, format: "2021-07-02" "2021-07-05" "2021-07-06" "2021-07-07" ...
$ type : chr "25 25" "25 25" "25 25" "25 25" ...
$ dec : num -0.193 -0.189 -0.176 -0.195 -0.219然后,我使用这个df绘制一个图表:
ggplot(df)+geom_line(aes(x=Date, y=type, color=dec))+geom_point(aes(x=Date, y=dec, color=type))+
geom_hline(yintercept = 0)+geom_vline(xintercept = as.numeric(ymd("2022-08-25")), linetype="dashed", color = "red")+
geom_vline(xintercept = as.numeric(ymd("2022-07-19")), linetype="dashed", color = "red")+geom_vline(xintercept = as.numeric(ymd("2022-04-19")), linetype="dashed", color = "red")+annotate(x=as.Date(ymd("2022-08-25")), y=+Inf,vjust=1,hjust=2.1,size=3,label="25/08/22 No:1.6", geom="text", angle=90)+
annotate(x=as.Date(ymd("2022-07-19")), y=+Inf,vjust=1,hjust=2.1,size=3,label="19/07/22 No:2.1", geom="text", angle=90)+annotate(x=as.Date(ymd("2022-04-19")), y=+Inf,vjust=1,hjust=2,size=3,label="19/04/22 No:2.4", geom="text", angle=90)目前,我必须手动输入geom_vlines和相应的注释,如果需要大量注释,这将成为一个越来越难以完成的练习。我想知道是否有可能从另一个dataframe动态生成这些注释和vline?
我已经收集并整理了这些数据,现在将其保存在这样的数据框架中:
$ Date : Date, format: "2022-08-25" "2022-07-19" "2022-04-19" ...
$ Amount : num 1.6 2.4 2.1 ...意识到这可能有点“存在”,但任何建议/帮助都是非常感谢的。
发布于 2022-08-31 09:54:19
多亏了Jon,它现在正在按预期工作。使用:
ggplot(df)+geom_line(aes(x=Date, y=type, color=dec))+geom_point(aes(x=Date, y=dec, color=type))+
geom_hline(yintercept = 0)+
geom_vline(data = df2, aes(xintercept = Date), linetype="dashed", color = "red")+geom_text(data = df2, aes(x = Date, y = Inf, label = Date), vjust=1,hjust=2.1,size=3,angle=90)https://stackoverflow.com/questions/73545950
复制相似问题