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

时间:2016-05-10 18:04:20

标签: r ggplot2

ggplot2的geom_rect存在问题。我想用浅蓝色遮挡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) 
             )

结果图:

enter image description here

数据如下:

> 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

我想将vline左侧的区域绘制为浅蓝色

1 个答案:

答案 0 :(得分:5)

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

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

,因为

  • 您无需重新指定data arg和
  • 你不想要红色/绿色边框。

图上总是有一点填充,所以请确保你的xstart远远超出了图中显示的数据限制

xstart <- as.POSIXct("2016-04-23 20:36:21 IST")

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

lims <- with(df, range(resolutiondate))

接下来我们需要使用它。如果使用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")

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

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

请注意,如果您想在此处左侧绘制,则xend需要resolutiondate[300]

现在添加geom_rect()图层并设置x-limits

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

我得到了:

enter image description here

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