简体   繁体   中英

Adding a shaded rectangle to an existing plot using geom_rect

I am having a hard time adding a series of shaded rectangles on top of an existing ggplot. Thanks in advance for your help.

I started by making a time series line graph using geom_line. I used the dataframe "unemp_table", and plotted "Month" on the X, and "NYC.Employment" on the Y.

unemp_table looks like this:

   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                                         

My ggplot code (which works) looks like this:

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

Next, I want to plot some shaded recession bars.

I started by loading a different data frame (Reces_table) with the start and end of each relevant recession.

Reces_table looks like this:

  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

But, when I try and plot these points as shaded rectangles, I get errors.

This code:

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

Gives me this error:

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

I read in a different thread that I need to add "data=" into the geom_rect function. However, the updated code:

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

Gives me this error:

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

Any tips would be appreciated, thanks in advance for your help!

You can give this a shot. I add the inherit.aes argument to the geom_rect call.

   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)

在此处输入图片说明

Per the documentation:

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, eg borders.

When calling ggplot , you set the aes argument as aes(x = Month, y = NYC.Employment . Without the inherit.aes = FALSE in your call to geom_rect , ggplot tries to combine the aes you enter into geom_rect with the default aes (that you set in the initial call to ggplot ). Since Month isn't in your Reces_table , this can't be done.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM