CMAP-REPOS / cmapplot

Set of templates and themes to apply CMAP graphics standards to R products.
https://cmap-repos.github.io/cmapplot/
Other
13 stars 1 forks source link

add recession outlines to time series #11

Closed matthewstern closed 4 years ago

matthewstern commented 4 years ago

https://r-graphics.org/recipe-annotate-rect

matthewstern commented 4 years ago

There's some sample code on the BLS API package page: https://github.com/mikeasilva/blsAPI

Challenge will be to accept the x axis bounds of the primary dataset. Maybe this involves filtering the recessions table? Or, maybe annotate_rect rather than geom_rect solves this problem?

## Beginning and end dates for the Great Recession (used in shaded area)
gr.start <- as.POSIXct(strptime('1December2007', '%d%B%Y'))
gr.end <- as.POSIXct(strptime('1June2009', '%d%B%Y'))

## Plot the data
ggplot(df) + 
    geom_rect(aes(xmin = gr.start, xmax = gr.end, ymin = -Inf, ymax = Inf), alpha = 0.4, fill="#DDDDDD") + 
    geom_line(aes(date, unemployment.rate*100)) + ylab('Percent of labor force')  + 
    xlab('Great Recession shaded in gray') + 
    ggtitle('Unemployment Rate for Manhattan, NY (Jan 2007 to Dec 2010)') + 
    theme_bw()
matthewstern commented 4 years ago

As a reminder, the goal is to replicate this:

image

This feature is a work in progress in branch "add_recessions." basic functionality is up.

Before I do a pull request, I'm trying to solve a few more problems:

  1. Is there a way to inherit text formatting from theme_cmap() or adjust theme_cmap to change the appearance of the text written in geom_text here?
  2. Is there a way to place recessions behind the primary geom being graphed, even if it is added to ggplot after the primary geom?
  3. Is there an easy way to place the recession bars behind the horizontal grid lines? My best ideas right now are coming from here but there's a layering concern - we would want the order to be recessions / grid lines / primary geom.

Does anyone else on the team have a chance to take a look at this before we meet next week (@nmpeterson or others)? Feel free to experiment and commit with new ideas.

nmpeterson commented 4 years ago

@tallishmatt:

  1. As far as I know, theme_cmap() cannot modify fonts associated with geoms. However, I plan to modify the font-loading code to override Arial with Whitney Medium as the default "sans" font, so any text that is not explicitly set to one of the other weights. So, you can just ignore fonts for now. (Update: ggplot2::theme has a text parameter that we can use to set a default font, which will be inherited by all text objects within the plot.)

  2. I'm pretty sure the layering order is 100% controlled by the order of commands/geoms, with each subsequent geom being placed in front of all preceding ones. We will just have to specify a recommended order in the cookbook/documentation.

  3. Not sure about this, but one simple solution would be to use a much darker gray for the recessions and set the dafault alpha to, like, 0.25 instead of 1.

matthewstern commented 4 years ago

Thanks Noel. Will summit pull request soon.

Note to self: need to add legend handling, move annotations into if statement.

matthewstern commented 4 years ago

This is done, although there are some outstanding enhancements I'd like to add separately.