对于日期,Geom rect填充不正常

Daw*_*y33 -1 r ggplot2

geom_rectggplot2 存在问题.我想用浅蓝色遮挡vline左边的区域.但是,这很有趣.(也许是因为涉及的日期栏).

代码:

library(dplyr)
library(ggplot2)
library(scales)

df <- read.csv("~/Desktop/dataset.csv")

# df <- df[!duplicated(df$caseid),]

df$createdat <- as.numeric(as.character(df$createdat))
df$resolutionat <- as.numeric(as.character(df$resolutionat))



df <- df[df$resolutionat != 0,]

df <- mutate(df, age = (resolutionat - createdat))
df <- mutate(df, counts = assigneechangecount + teamchangecount)
df <- mutate(df, isbreached = rbinom(388, 1, 0.2))
df<- mutate(df, resolutiondate = as.POSIXct(df$resolutionat, origin="1970-01-01"))

xstart <- as.POSIXct("2016-04-26 20:36:21 IST")
xend <- as.POSIXct("2016-04-28 12:00:38 IST")


print(ggplot(df, aes(resolutiondate, age, size = counts, color = factor(isbreached))) +
             geom_point(alpha = 0.4) +
             geom_point(shape = 21) +
             scale_y_continuous(labels = comma) +
             geom_vline(data=df, aes(xintercept = as.numeric(resolutiondate[300]), color = "blue")) +
             geom_rect(data = df, aes(xmin=xstart, xmax=xend, ymin=-Inf, ymax=Inf), fill = "light blue", alpha = 0.2) 
             )
Run Code Online (Sandbox Code Playgroud)

由此产生的情节:

在此输入图像描述

数据如下:

> head(df)
   caseid  createdat resolutionat assigneechangecount teamchangecount  age
1 2143843 1462892601   1462894326                   1               1 1725
2 2143840 1462892071   1462893544                   1               1 1473
3 2143839 1462892018   1462892466                   1               1  448
4 2143838 1462891887   1462893433                   1               1 1546
5 2143830 1462890910   1462893543                   1               1 2633
6 2143829 1462890812   1462892469                   1               1 1657
  counts isbreached      resolutiondate
1      2          0 2016-05-10 21:02:06
2      2          1 2016-05-10 20:49:04
3      2          0 2016-05-10 20:31:06
4      2          0 2016-05-10 20:47:13
5      2          1 2016-05-10 20:49:03
6      2          0 2016-05-10 20:31:09
Run Code Online (Sandbox Code Playgroud)

我想将vline左边的区域绘制成浅蓝色

Rei*_*son 5

你的geom_rect()电话可能想成为:

geom_rect(aes(xmin = xstart, xmax = xend, ymin = -Inf, ymax = Inf),
          fill = "light blue", alpha = 0.2, colour = NA)
Run Code Online (Sandbox Code Playgroud)

因为

  • 你不需要重新指定dataarg,和
  • 你不想要红色/绿色边框.

在图上总是有一点填充,因此请确保您的xstart远远超出了图中显示的数据限制

xstart <- as.POSIXct("2016-04-23 20:36:21 IST")
Run Code Online (Sandbox Code Playgroud)

然后,您需要做的就是将x轴限制设置为数据的限制:

lims <- with(df, range(resolutiondate))
Run Code Online (Sandbox Code Playgroud)

接下来我们需要使用它.如果您使用xlim()设置x轴限制,那么超出这些限制的任何东西,即矩形geom的开始,都将被抛弃.您想要使用的是coord_cartesian(),它作为Date对象受到限制:

## Clean up your plot
p <- ggplot(df, aes(resolutiondate, age, size = counts, color = factor(isbreached))) +
            geom_point(alpha = 0.4) +
            geom_point(shape = 21) +
            scale_y_continuous(labels = comma) +
            geom_vline(data=df, aes(xintercept = as.numeric(resolutiondate[300])),
                       color = "blue")
Run Code Online (Sandbox Code Playgroud)

现在设置适当的开始和结束

xstart <- as.POSIXct("2016-04-23 20:36:21 IST")
xend <- with(df, resolutiondate[300])
Run Code Online (Sandbox Code Playgroud)

请注意,您必须xendresolutiondate[300],如果你想提请的左边到这一点.

现在添加geom_rect()图层并设置x限制

p + geom_rect(aes(xmin = xstart, xmax = xend, ymin = -Inf, ymax = Inf),
              fill = "light blue", alpha = 0.2, colour = NA) +
    coord_cartesian(xlim = lims)
Run Code Online (Sandbox Code Playgroud)

我得到了:

在此输入图像描述

关键是coord_cartesian()部分.您可以将此视为将结果图像剪切到这些限制,而xlim()更像是将数据剪切到这些限制,然后绘制剩下的内容.

  • 您可能想要一个显式的“ data = NULL”,以便在原始数据集中的每一行都不会得到一个矩形(这就是为什么它当前看起来不像“ alpha = 0.2”)的原因 (2认同)