在ggplot2中使用虚拟变量突出显示时段 [英] Highlighting periods using a dummy variable in ggplot2
问题描述
我有两个时间序列收益率和fx,还有一个假人.如何在ggplot中绘制两个序列并突出显示(遮蔽)虚拟对象为1的区域?下面的数据集的标题.
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
我用类似于下面的代码的rect尝试过,但这没用.
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.
推荐答案
如果只有这一时期需要阴影处理,我将使用annotate
.否则,您可以使用geom_rect
并使用某些方法来提取唯一的日期.
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.
我将date列转换为Date对象,以获得更好的格式并能够使用min
和max
之类的功能.然后,虚拟数据位于单独的数据框中,该数据框已过滤以供观察,其中dummy == 1
和日期为dummy == 1
日期的第一个或最后一个日期.这将成为注释的xmin
和xmax
.我将注释的ymin
设置为0,但是您可以将其设置为对数据有意义的任何内容.
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.
我上面也看到了您关于需要使网格线可见的评论,因此我改用了theme_light
.如果有问题,可以更改网格线的颜色.
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()
由 reprex软件包(v0.2.0)创建于2018-04-18.
Created on 2018-04-18 by the reprex package (v0.2.0).
这篇关于在ggplot2中使用虚拟变量突出显示时段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!