1

我尝试使用此Add vline to existing plot 中提到的解决方案并将其显示在 ggplot2 图例中?

但是,部分代码不适用于我的情况,尤其是 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 后,该数字消失了。任何想法?谢谢你的帮助!

4

0 回答 0