繁体   English   中英

在ggplot2中使用geom_smooth()和geom_point()时显示带有日期的x轴标签

[英]Display x-axis labels with dates when using geom_smooth() and geom_point() in ggplot2

我有以下 data.frame:

prv <- data.frame(mese = as.Date(c( "2020-09-01", "2020-10-01" ,"2020-11-01","2020-12-01" ,
                                    "2021-01-01" ,"2021-02-01", "2021-03-01" ,"2021-04-01")),
                  cumulato = c(150  ,200  ,280  ,400, 550 ,800 ,1200 , 2000),
                  x_value = 1:8) 

当我将 geom_smooth() 与 geom_point() 一起使用并希望在 xlabels 中显示日期时出现问题:

g <- ggplot(data =  prv, mapping = aes(x= mese, y = cumulato )) + 
    geom_smooth(data =  prv, mapping = aes(x= x_value, y = cumulato), method="lm", formula= (y ~ exp(x))) +
    geom_point( size = 3) 

我得到了错误:
as.Date.numeric(value) 中的错误:必须提供“origin”

即使我将 geom_smooth() 与公式参数一起使用,如何获取带有日期的 x 轴标签(即 mese 变量)?
当然,由于公式,我不能在 geom_smooth Aesthetic 中使用x= mese

也许,当将mese作为因子与级别 1 到 8 时,您可能会得到所需的结果,就像x_value

library(ggplot2)
ggplot(data =  prv, mapping = aes(x = as.factor(mese), y = cumulato )) + 
  geom_point(size = 3) +
  geom_smooth(mapping = aes(x = x_value, y = cumulato), method = "lm", formula = y ~ exp(x))

数据

prv <-
  data.frame(
    mese = as.Date(c("2020-09-01", "2020-10-01", "2020-11-01", "2020-12-01",
                     "2021-01-01", "2021-02-01", "2021-03-01", "2021-04-01")),
    cumulato     = c(150  , 200  , 280  , 400, 550 , 800 , 1200 , 2000),
    x_value = 1:8
  ) 

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM