簡體   English   中英

Geom rect填充不適用於日期

[英]Geom rect fill not working properly for dates

geom_rect的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) 
             )

結果圖:

在此處輸入圖片說明

數據如下:

> 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左側的區域繪制為淺藍色

您的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軸限制,則超出這些限制的任何內容(即矩形幾何的起點)都將被丟棄。 您要使用的是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])

請注意,如果要繪制到此點的左側,則需要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)

這樣我得到:

在此處輸入圖片說明

關鍵是coord_cartesian()部分。 您可以認為這就像將結果圖像裁剪到那些限制,而xlim()更像是將數據裁剪到那些限制,然后繪制剩下的內容。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM