2017-01-13 11 views
-1

既存のggplotの上に一連の網掛けされた長方形を追加するのは苦労しています。あなたの助けを前にありがとう。geom_rectを使用して既存のプロットに網掛けの矩形を追加する

私はgeom_lineを使って時系列の折れ線グラフを作成しました。私は "unemp_table" のデータフレームを使用し、X上で "月" をプロットし、 "NYC.Employment" Y.に

このようなunemp_tableルックス:

Month  Sample  NYC.Employment  
    (date)  (int)   (int)           
1976-01-01  1    2771        
1976-02-01  2    2770          
1976-03-01  3    2769           
1976-04-01  4    2768           

マイggplotコード(作品)

unemp_graph <- ggplot(data=unemp_table, aes(x=Month,y=NYC.Employment)) 
+geom_line() 

次に、いくつかの影付きの不況バーをプロットしたいと思います。

私は、それぞれの関連する不況の開始と終了で異なるデータフレーム(Reces_table)を読み込むことから始めました。私がしようとすると影の長方形が、私はエラーを取得し、これらの点をプロットするとき、

Start  End 
    (date)  (date) 
1 1980-01-01 1980-07-01 
2 1981-07-01 1982-11-01 
3 1990-07-01 1991-03-01 
4 2001-03-01 2001-11-01 
5 2007-12-01 2009-06-01 

しかし:

Reces_tableは次のようになります。

このコード:

unemp_graph + geom_rect(Reces_table, aes(xmin=Start, xmax=End, 
ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2) 

は私に、このエラーを与える:

Error: ggplot2 doesn't know how to deal with data of class uneval 

私はgeom_rect関数に "データを=" 追加する必要が別のスレッドで読みます。しかし、更新されたコード:

unemp_graph + geom_rect(data=Reces_table, 
aes(xmin=Start, xmax=End, ymin=-Inf, ymax=+Inf), fill='pink', alpha=0.2) 

は私に、このエラーを与える:

Error in eval(expr, envir, enclos) : object 'Month' not found 

任意のヒントは、あなたの助けを事前に感謝をいただければ幸いです!

答えて

1

これは1発を与えることができます。 geom_rectコールにinherit.aes引数を追加します。

マニュアルに従って
unemp_table <- read.table(text = "Month  Sample  NYC.Employment  
        1976-01-01  1    2771        
        1976-02-01  2    2770          
        1976-03-01  3    2769           
        1976-04-01  4    2768", header = TRUE, 
        stringsAsFactors = FALSE) 
    unemp_table$Month <- as.Date(unemp_table$Month) 


    Reces_table <- read.table(text = " Start  End 
           1 1980-01-01 1980-07-01 
           2 1981-07-01 1982-11-01 
           3 1990-07-01 1991-03-01 
           4 2001-03-01 2001-11-01 
           5 2007-12-01 2009-06-01", header = TRUE, 
           stringsAsFactors = FALSE) 
    Reces_table$Start <- as.Date(Reces_table$Start) 
    Reces_table$End <- as.Date(Reces_table$End) 

    unemp_graph <- ggplot(data=unemp_table, aes(x=Month,y=NYC.Employment)) + 
     geom_line() + geom_rect(data= Reces_table, inherit.aes = FALSE, 
           aes(xmin=Start, xmax=End, ymin=-Inf, ymax=+Inf), 
           fill='pink', alpha=0.2) 

enter image description here

inherit.aes If FALSE, overrides the default aesthetics, rather than combining with them. This is most useful for helper functions that define both data and aesthetics and shouldn't inherit behaviour from the default plot specification, e.g. borders.

ggplotを呼び出すとき、あなたはaes(x = Month, y = NYC.Employmentとしてaes引数を設定します。 geom_rectへの電話でがなければ、aesを組み合わせようとするとgeom_rectにデフォルトaes(最初の呼び出しではggplotに設定されています)と入力します。 MonthはあなたのReces_tableに含まれていないため、これはできません。

+0

これは完璧に動作します。ご協力いただきありがとうございます。私は本当にinherit.aes = FALSEが何であるかは分かりませんが、読み上げます。再度、感謝します! – user3475967

+0

@ user3475967、私は、編集された答えでこのコンテキストで 'inherit.aes = FALSE'がどのように機能するか説明するために最善を尽くしました。 –

+0

それは、あなたの助けをもう一度感謝します。 – user3475967

関連する問題