【发布时间】:2017-08-25 18:18:58
【问题描述】:
我正在尝试在日期轴上的特定日期添加一条垂直线。基于this SO post,我似乎需要将日期转换为数字,但这对我不起作用。我做错了什么?
我的错误:
Error: ggplot2 doesn't know how to deal with data of class uneval
我的代码
library(lubridate)
trump_score<-NULL
trump_score$Date <-parse_date_time(c("2017-01-01","2017-01-24","2017-01-25"), orders="ymd")
trump_score$powerSentimentScore<-c(10,25,10)
denyTPP<-parse_date_time("2017-01-23", orders="ymd ")
require(ggplot2)
ggplot( aes(trump_score$Date))+
geom_line(aes(y=trump_score$powerSentimentScore),colour="green")+
geom_vline(aes(xintercept = as.POSIXct(as.Date(denyTPP))), linetype="dotted", color = "blue", size=1.5)
【问题讨论】:
-
ggplot2 旨在与 data.frames 一起使用,
data参数是ggplot中的第一个参数。如果您真的不想使用 data.frames ,则需要将参数命名为:ggplot(mapping = aes(trump_score$Date) )这样您就不会将映射传递给data参数。有了情节后,geom_vline中日期的as.numeric解决方案将起作用。 -
使用 ggplot2 的
annotate函数绘制非表格的东西。