为什么我的geom_vline对象没有显示在我的绘图上?

时间:2018-07-27 11:44:44

标签: r ggplot2 geom-vline

我已经研究了解决与相同问题相关的其他人问题的多种解决方案,但是到目前为止,对我来说没有任何帮助。

我希望垂直线出现在“ 2018-07-23”上,并且此代码是我所获得的最接近的代码(因为它不会产生错误):

ggplot(grouped) +
  geom_line(aes(x = date, y = sitewide_opens, group = 1),
            linetype = "dashed",
            colour = "forestgreen",
            alpha = 0.5) +
  geom_line(aes(x = date, y = homepage_opens, group = 1),
            colour = "blue") +
  geom_vline(aes(xintercept = as.Date(grouped$date[8])),
             linetype = 4, colour = "black")

grouped$date的格式是字符,这就是为什么我将其转换为日期。请注意,as.POSIXct也得到相同的(非)结果。

我要去哪里错了?

我的数据框:

grouped <- structure(list(date = c("2018-07-16", "2018-07-17", "2018-07-18", 
"2018-07-19", "2018-07-20", "2018-07-21", "2018-07-22", "2018-07-23", 
"2018-07-24", "2018-07-25", "2018-07-26", "2018-07-27", "2018-07-28", 
"2018-07-29", "2018-07-30", "2018-07-31"), homepage_opens = c(5L, 
0L, 0L, 3L, 1L, 2L, 0L, 1L, 0L, 2L, 5L, 0L, 0L, 0L, 0L, 0L), 
    sitewide_opens = c(39L, 34L, 19L, 62L, 46L, 44L, 16L, 51L, 
    25L, 66L, 75L, 0L, 0L, 0L, 0L, 0L), chats_started = c(10L, 
    16L, 9L, 8L, 13L, 13L, 5L, 13L, 4L, 8L, 11L, 0L, 0L, 0L, 
    0L, 0L), chats_completed = c(7L, 13L, 8L, 4L, 5L, 9L, 6L, 
    13L, 2L, 7L, 5L, 0L, 0L, 0L, 0L, 0L)), class = c("tbl_df", 
"tbl", "data.frame"), row.names = c(NA, -16L))

我的图表:

enter image description here

2 个答案:

答案 0 :(得分:1)

使用示例复制您的情节,并将typedef void(^SDCacheQueryCompletedBlock)(UIImage * _Nullable image, NSData * _Nullable data, SDImageCacheType cacheType); as.Date在所有实例中都转换为日期,并且效果很好:

grouped$date

情节出来了: https://i.imgur.com/Km8UvaX.jpg

答案 1 :(得分:0)

如何将xintercept = as.Date(grouped$date[8])更改为xintercept = 8

ggplot(grouped) +
  geom_line(aes(x = date, y = sitewide_opens, group = 1),
            linetype = "dashed",
            colour = "forestgreen",
            alpha = 0.5) +
  geom_line(aes(x = date, y = homepage_opens, group = 1),
            colour = "blue") +
  geom_vline(aes(xintercept = 8),
             linetype = 4, colour = "black")

enter image description here

相关问题