2016-05-10 4 views
-2

ggplot2のgeom_rectに問題があります。私は、vlineの左側を薄い青で覆いたい。しかし、それは面白いです。 (多分日付の列が関係しているので)。日付のためにGeom緯線が正しく機能しない

コード:

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 

Iが左に領域をプロットしたかっvlineのライトブルー

+0

@bouncyballエラー:無効な入力:time_transはPOSIXctのみのオブジェクトで動作します( 'xmin'を-Infに設定します):( – Dawny33

+1

df < - read.csv("〜/ Desktop/dataset – dww

+0

@dwwデータセットのチャンクが追加されました – Dawny33

答えて

4

あなたgeom_rect()コールは、おそらくになりたい:あなたはdata引数を再指定する必要はありません

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

  • ので、と
  • あなたは赤のことをしたくありません/緑の国境。

ありプロット上のパディングの少しが常になのでその後、すべてを行う必要があるあなたもプロット

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

に表示されるデータの範囲を超えているXSTARTを持っていることを確認してくださいx軸の制限をデータの限界に設定するには:

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

次に、これを使用する必要があります。 xlim()を使用してx軸の制限を設定した場合、これらの制限を外れたもの、つまり長方形の開始点であるgeomがスローされます。今すぐ開始appropropriate設定

## 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") 

とあなたが描きたい場合はresolutiondate[300]をするxendが必要

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

お知らせを終了します。あなたが使用したいことはちゃんとDateオブジェクトとしての限界を取るcoord_cartesian()、ありますここまでの左に

enter image description here

キーラインがcoord_cartesian()一部です:

今、私が手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) 

を設定します。これは結果として得られる画像をそれらの限界にクリップするように考えることができますが、xlim()はデータをこれらの限界にクリッピングして残ったものを描画するようなものです。

+1

元のデータセットの各行に対して1つの矩形を取得しないように、明示的に 'data = NULL'を指定する必要があります(これは' alpha = 0.2'のように見えません) – hadley

関連する問題