gpt4 book ai didi

r - 带日期的 geom_vline() 给出错误 : Discrete value supplied to continuous scale

转载 作者:行者123 更新时间:2023-12-03 23:55:51 24 4
gpt4 key购买 nike

获取后Error: Discrete value supplied to continuous scaleggplot 的消息和 geom_vline()我做了一些实验,发现了以下惊喜。

这是一个以一些数据开头的可重现示例:

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)

Illustration of ggplot, date and vline issue

但是,如果我们更改 geoms 的顺序:
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

最佳答案

只需将日期转换为日期类并添加 scale_x_date() ,像这样:

df$date <- as.Date(df$date)

ggplot(data=df, aes(x=date, y=value)) + geom_line() +
geom_vline(xintercept = as.numeric(as.Date(dmy("3/6/2014"))), linetype=4) +
scale_x_date()

关于r - 带日期的 geom_vline() 给出错误 : Discrete value supplied to continuous scale,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23997475/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com