Error: Discrete value supplied to continuous scale ggplot in R
Error: Discrete value supplied to continuous scale ggplot in R
当我 运行 这段代码时,我一直收到这个错误,我不明白为什么。我认为这可能是我的数据有问题?
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)
显然 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)
得到
当我 运行 这段代码时,我一直收到这个错误,我不明白为什么。我认为这可能是我的数据有问题?
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)
显然 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)
得到