Highlighting periods using a dummy variable in ggplot2

大憨熊 提交于 2019-12-13 03:48:29

问题


I have two time series yield and fx and a dummy. How can I plot the two series in ggplot and highlight (shade) the areas where the dummy is 1? The header of the data set below.

date    dummy   yield   fx
1/1/1990    0   10.029  1.261184049
1/2/1990    0   10.036  1.261008068
1/3/1990    0   10.119  1.258932591
1/4/1990    0   10.02   1.261410528
1/5/1990    0   10.013  1.261586847
1/6/1990    1   10.066  1.260255526
1/7/1990    1   10.057  1.260481006
1/8/1990    1   10.057  1.260481006
1/9/1990    1   10.067  1.260230488
1/10/1990   1   10.186  1.257272051

I tried it with rect similar to the code below but this did not work.

ggplot(dummies, aes(date)) + 
geom_line(aes(y = yield, colour = "yield")) + 
geom_line(aes(y = fx, colour = "fx")) +
geom_rect(aes(xmin=-Inf, xmax=Inf, ymin=0, ymax=1),
          colour=alpha("grey20", 0.5), fill.alpha=0.5)

Any help is much appreciated. Thank you.


回答1:


Give this a go:

library(tidyr)

dummies2<- gather(dummies, key="type", value="value", c("yield", "fx"))

ggplot(dummies2, aes(x=date, y=value, colour=type, group=type)) + 
  geom_segment(aes(y=-Inf, yend=Inf,x=date, xend=date, alpha=dummy),inherit.aes = F,colour="black", size=5)+
               scale_alpha_continuous(range=c(0,0.3))+
  guides(alpha=F)+
  geom_line()

(Note that the annotations are added as marks on discrete timepoints. Doing it otherwise, as interperiod shading is also possible.




回答2:


If you only have this one period that needs to be shaded, I would just use annotate. Otherwise, you could make use of geom_rect with some means of pulling just unique dates.

I converted your date column to Date objects to get better formatting and to be able to use functions like min and max. Then the dummy data is in a separate dataframe that's filtered for observations where dummy == 1 and where the date is either the first or last of the dummy == 1 dates. This makes the xmin and xmax of the annotation. I set the ymin for the annotation to 0, but you could set it to anything that makes sense for your data.

I also saw your comments above about needing the gridlines to be visible, so I used theme_light instead. You could change the color of your gridlines if it's an issue.

library(tidyverse)

# main data frame
df <- "date    dummy   yield   fx
1/1/1990    0   10.029  1.261184049
1/2/1990    0   10.036  1.261008068
1/3/1990    0   10.119  1.258932591
1/4/1990    0   10.02   1.261410528
1/5/1990    0   10.013  1.261586847
1/6/1990    1   10.066  1.260255526
1/7/1990    1   10.057  1.260481006
1/8/1990    1   10.057  1.260481006
1/9/1990    1   10.067  1.260230488
1/10/1990   1   10.186  1.257272051" %>% 
    read_table2() %>%
    mutate(date2 = lubridate::mdy(date)) %>%
    gather(key = measure, value = value, yield, fx)

head(df)
#> # A tibble: 6 x 5
#>   date     dummy date2      measure value
#>   <chr>    <int> <date>     <chr>   <dbl>
#> 1 1/1/1990     0 1990-01-01 yield    10.0
#> 2 1/2/1990     0 1990-01-02 yield    10.0
#> 3 1/3/1990     0 1990-01-03 yield    10.1
#> 4 1/4/1990     0 1990-01-04 yield    10.0
#> 5 1/5/1990     0 1990-01-05 yield    10.0
#> 6 1/6/1990     1 1990-01-06 yield    10.1

# dummy data frame: dummy == 1, only min & max dates
dummy <- df %>% 
    filter(dummy == 1) %>% 
    filter(date2 %in% c(min(date2), max(date2))) %>%
    select(dummy, date2) %>%
    unique()

ggplot(df, aes(x = date2, y = value, color = measure)) +
    annotate(geom = "rect", xmin = min(dummy$date2), xmax = max(dummy$date2), ymin = 0, ymax = Inf, fill = "gray", alpha = 0.4) +
    geom_line() +
    theme_light() 

Created on 2018-04-18 by the reprex package (v0.2.0).



来源:https://stackoverflow.com/questions/49900518/highlighting-periods-using-a-dummy-variable-in-ggplot2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!