【发布时间】:2014-06-02 14:54:20
【问题描述】:
在收到带有ggplot 和geom_vline() 的Error: Discrete value supplied to continuous scale 消息后,我做了一些实验,发现了以下惊喜。
这是一个以一些数据开头的可重现示例:
require(lubridate)
require(ggplot2)
df <- data.frame(
date=dmy(c("2/6/2014", "3/6/2014", "4/6/2014", "5/6/2014")),
value=1:4
)
让我们用一条垂直线穿过"3/6/2014":
ggplot(data=df, aes(x=date, y=value)) +
geom_line() +
geom_vline(xintercept = as.numeric(dmy("3/6/2014")), linetype=4)
但是,如果我们改变 geom 的顺序:
ggplot(data=df, aes(x=date, y=value)) +
geom_vline(xintercept = as.numeric(dmy("3/6/2014")), linetype=4) +
geom_line()
产生以下错误消息:
Error: Discrete value supplied to continuous scale
【问题讨论】:
-
我同意 ggplot 决定训练尺度的方式有点奇怪。如果你真的需要
geom_vline成为第一个(例如,如果你需要在上面绘制很多其他东西)你总是可以在ggplot之后立即添加geom_blank。