【问题标题】:Error: Discrete value supplied to continuous scale when using ggplot错误:使用 ggplot 时提供连续刻度的离散值
【发布时间】:2019-02-04 04:26:19
【问题描述】:

我绘制了一个散点图,y 轴为频率量,x​​ 轴为日期(例如 2012-07)。此外,我在绘图中添加了垂直线(矢量 r1)。绘图后,我收到错误消息

错误:提供给连续刻度的离散值

我的数据框 df1 如下所示:

date         amount
2012-07          2
2012-08          4
2012-09          4
2012-10          3
2012-11          2
2012-12          3
2013-01          5 
2013-02          4
2013-03          3
2013-04          2
2013-05          1
2013-06          4
2013-07          3

我的垂直线在向量 r1 中,看起来像这样:

1
2
4
6
7
9

数字 1 等于我的第一个条目 df1$date,数字 2 是第二个条目,依此类推。

ggplot(df1, aes(x=date,y=amount, color=gr)) +    
            geom_vline(xintercept=as.numeric(r1), alpha=0.5) +    
            geom_point()

我猜错误来自这样一个事实,即 r1 具有 1、2、5 之类的值,但我的 x 轴是日期格式,不适合在一起。所以数字 1 代表我在向量 r1 中的第一个日期,2 代表第二个日期等等....有什么办法可以将这个 r1 向量更改为我的相应日期?谢谢!

【问题讨论】:

  • 请添加一些示例数据以使您的问题可重现。
  • @beetroot 我刚刚编辑了我的数据的样子
  • 你的意思是你的x轴是日期格式的吗?请使用示例 df 中的列名更新 ggplot-call
  • @kath 对错误深表歉意,我希望现在一切都正确
  • 对我来说,如果你切换 geom_pointgeom_vline 就可以了:ggplot(df1, aes(date, amount)) + geom_point() + geom_vline(xintercept = r1, alpha = 0.5)

标签: r date ggplot2 plot format


【解决方案1】:

正如kath 提到的,最好将日期和行更改为日期格式。这里我使用lubridate

library(ggplot2)
library(dplyr)
library(lubridate)
df1 <- read.table(header = T, stringsAsFactors = FALSE, text = 
"date         amount
2012-07          2
2012-08          4
2012-09          4
2012-10          3
2012-11          2
2012-12          3
2013-01          5 
2013-02          4
2013-03          3
2013-04          2
2013-05          1
2013-06          4
2013-07          3"
)

r1 <- c(1,2,4,6,7,9)

df2 <- df1 %>% mutate(line = ifelse(row_number(date) %in% r1, date, NA)) %>% 
  mutate(date = ymd(date, truncated = 2), line = ymd(line, truncated = 2))

ggplot(df2, aes(x = date,y = amount, color = amount)) +    
  geom_vline(aes(xintercept = line), alpha=0.5) +    
  geom_point()

注意:我将col 参数更改为amount,因为gr 不在样本数据中。

【讨论】:

    【解决方案2】:

    您可以首先使用as.Date 将您的日期列转换为适当格式的日期。由于只有飞蛾可用,我将日期设置为每月的第一天,但​​您也可以指定 15 号。

    df1$date <- as.Date(paste0(df1$date, "-01"))
    

    当我们现在绘制数据时,x 轴的格式很好:

    library(ggplot2)
    
    ggplot(df1, aes(date, amount)) +
      geom_point()
    

    对于垂直线,从 data.frame 中提取所需的日期,然后将它们添加到绘图中:

    ggplot(df1, aes(date, amount)) +
      geom_point() + 
      geom_vline(xintercept = df1$date[r1], alpha = 0.5)
    

    如果你切换调用顺序,它现在也可以工作

    ggplot(df1, aes(date, amount)) + 
      geom_vline(xintercept = df1$date[r1], alpha = 0.5) +
      geom_point()
    

    数据

    df1 <- 
      structure(list(date = c("2012-07", "2012-08", "2012-09", "2012-10", "2012-11", 
                              "2012-12", "2013-01", "2013-02", "2013-03", "2013-04", 
                              "2013-05", "2013-06", "2013-07"), 
                     amount = c(2L, 4L, 4L, 3L, 2L, 3L, 5L, 4L, 3L, 2L, 1L, 4L, 3L)), 
                class = "data.frame", row.names = c(NA, -13L))
    
    r1 <- c(1, 2, 4, 6, 7, 9)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2015-01-08
      • 1970-01-01
      • 2016-07-26
      • 2014-07-06
      • 2019-01-04
      • 2023-01-31
      • 2015-09-26
      • 1970-01-01
      相关资源
      最近更新 更多