错误:提供给R中连续缩放ggplot的离散值

时间:2015-03-11 20:41:23

标签: r ggplot2

我运行此代码时遇到此错误,我无法弄清楚原因。我认为这可能与我的数据有关吗?

d=data.frame(date=as.Date(c("1971-09-01", "1991-12-01", "1994-12-01",       
"2000-01-01", "2002-08-01", "2005-08-01")), 
event=c("birth", "entered college", "BS", "entered grad school", "MS",    
"PhD"))

ggplot() +
scale_x_date(limits=as.Date(c("1970-1-1", "2010-12-31"))) +
scale_y_continuous(name="", breaks=NA, limits=c(0,1)) +
geom_vline(data=d, mapping=aes(xintercept=date), color="blue") +
geom_text(data=d, mapping=aes(x=date, y=0, label=event), size=4,     
angle=90, vjust=-0.4, hjust=0)

1 个答案:

答案 0 :(得分:0)

显然geom_vline不喜欢xintercept=参数的日期值。如果你明确地转换为数字似乎工作。尝试

ggplot() +
scale_x_date(limits=as.Date(c("1970-1-1", "2010-12-31"))) +
scale_y_continuous(name="", breaks=NULL, limits=c(0,1)) +
geom_vline(data=d, mapping=aes(xintercept=as.numeric(date)), color="blue") +
geom_text(data=d, mapping=aes(x=date, y=0, label=event), size=4,     
angle=90, vjust=-0.4, hjust=0)

获取

enter image description here