Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change line properties in ggplot2 halfway in a time series?

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)

enter image description here

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).

like image 236
TemplateRex Avatar asked Apr 15 '15 12:04

TemplateRex


People also ask

How do I change the line type in ggplot2?

Change manually the appearance of linesscale_linetype_manual() : to change line types. scale_color_manual() : to change line colors. scale_size_manual() : to change the size of lines.

What does geom_ line() do?

geom_line() connects them in order of the variable on the x axis. geom_step() creates a stairstep plot, highlighting exactly when changes occur. The group aesthetic determines which cases are connected together.

How do I change the thickness of a line in ggplot2?

Line width in ggplot2 can be changed with argument size= in geom_line() .


Video Answer


2 Answers

Even easier than @Roland's suggestion:

economics %>%
    gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
    mutate(Y2K = year(date) >= 2000) %>%
    group_by(indicator, Y2K) -> econ

ggplot(econ, aes(date, percentage, group = indicator, colour = indicator)) + 
  geom_line(data = filter(econ, !Y2K), size=1, linetype = "solid") + 
  geom_line(data = filter(econ, Y2K), size=1, linetype = "dashed")

enter image description here

P.S. Alter plot width to remove spike artifacts (red line).

like image 58
tonytonov Avatar answered Oct 16 '22 18:10

tonytonov


require(dplyr)
require(ggplot2)
require(lubridate)
require(tidyr)


economics %>%
  gather(indicator, percentage, c(4:5), -c(1:3, 6)) %>%
  mutate(Y2K = year(date) >= 2000) %>%
  ggplot(aes(date, percentage, colour = indicator)) + 
  geom_line(size=1, aes(linetype = Y2K))
like image 29
sahoang Avatar answered Oct 16 '22 20:10

sahoang