Take the following straightforward plot of two time series from the economics{ggplot2}
dataset
require(dplyr)
require(ggplot2)
require(lubridate)
require(tidyr)
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
mutate(Y2K = year(date) >= 2000) %>%
group_by(indicator, Y2K) %>%
ggplot(aes(date, percentage, group = indicator, colour = indicator)) + geom_line(size=1)
I would like to change the linetype
from "solid" to "dashed" (and possibly also the line size
) for all points in the 21st century, i.e. for those observations for which Y2K
equals TRUE
.
I did a group_by(indicator, Y2K)
but inside the ggplot
command it appears I cannot use group =
on multiple levels, so the line properties only differ by indicator
now.
Question: How can I achieve this segmented line appearance?
UPDATE: my preferred solution is a slight tweak from the one by @sahoang:
economics %>%
gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
ggplot(aes(date, percentage, colour = indicator)) +
geom_line(size=1, aes(linetype = year(date) >= 2000)) +
scale_linetype(guide = F)
This eliminates the group_by
as commented by @Roland, and the filter
steps make sure that the time series will be connected at the Y2K point (in case the data would be year based, there could be a visual discontinuity otherwise).
group
at all. You simply need an additional grouping variable thatlinetype
is mapped to. Possibly you need to duplicate points where the switch occurs. Can't help you more sincedplyr
isn't to my taste. – Fink