我尝试使用此Add vline to existing plot and have it appear in ggplot2 legend?
中提到的解决方案但是,部分代码对我的情况不起作用,尤其是vline部分不起作用。我想知道是否还有其他的把戏? 我有两个数据集,第一个是主要数据集:
str(sf)
$ date : chr "2020-02-02" "2020-02-03" "2020-02-04" "2020-02-05" ...
$ School_Closure_Date: chr "2020-03-16" "2020-03-16" "2020-03-16" "2020-03-16" ...
$ variable : Factor w/ 5 levels "cases","deaths",..: 1 1 1 1 1 1 1 1 1 1 ...
$ value : num 2 2 2 2 2 2 2 2 2 2 ...
$ mindate : Date, format: "2020-01-24" "2020-01-24" …
$ maxdate : Date, format: "2020-03-30" "2020-03-30" ...
$ closure : logi FALSE FALSE FALSE FALSE FALSE FALSE …
str(sfs)
'data.frame': 1 obs. of 4 variables:
$ fips: int 6075
$ City: chr "San_Francisco"
$ date: chr "2020-03-16"
$ type: chr "school closure"
我的目的是在ggplot上放置一条vline。但是,我收到了错误信息:
y <- ggplot(sf,aes(date,value)) + geom_line(aes(group=variable,color=variable))
x <- geom_vline(data=sfs,xintercept=date,show.legend=TRUE)
y+x
Error in UseMethod("rescale") :
no applicable method for 'rescale' applied to an object of class "factor"
In addition: Warning message:
Removed 33 rows containing missing values (geom_path).
y部分起作用,但是在添加x部分之后,该图消失了。任何想法?感谢您的帮助!