Dr. Fabian Habersack
Dr. Fabian Habersack

Reputation: 1141

How to adjust distances between years on x-axis and adjust line of geom_line(), in ggplot, R-studio?

I would like to create a line plot using ggplot's geom_line() where all distances between years are equal independent of the actual value the year-variable takes and where the dots of geom_point() are connected if there are only two years in between but not if the temporal distance is more than that.

Example:

my.data<-data.frame(
  year=c(2001,2003,2005,NA,NA,NA,NA,NA,NA,2019),
  value=c(runif(10)))

As for the plot I have tried two different things, both of which are not ideal:

  1. Plotting year as continuous variable with breaks=year and minor_breaks=F, where, obviously the distances between the first three observations are much smaller than the distance between 2005 and 2019, and where, unfortunately, all dots are connected:

library(ggplot2)
library(dplyr)

my.data %>%
  ggplot(aes(x=year,y=value)) + 
  geom_line() + 
  geom_point() + 
  scale_x_continuous(breaks=c(2001,2003,2005,2019), minor_breaks=F) +
  theme_minimal()
  1. Removing NAs and plotting year as factor which yields equal spacing between the years, but obviously removes the lines between data points:

my.data %>%
  filter(!is.na(year)) %>%
  ggplot(aes(x=factor(year),y=value)) + 
  geom_line() + 
  geom_point() + 
  theme_minimal()

Are there any solutions to these issues? What am I overlooking?

First attempt:

First attempt

Second attempt:

Second attempt

What I need (but ideally without the help of Paint):

What I need

Upvotes: 1

Views: 1148

Answers (2)

Mojoesque
Mojoesque

Reputation: 1318

I came to a bit convoluted and not super clean solution, but it might get the job done. I am checking if one year should be connected to the next one with lead(). And "remove" the appropriate connections by turning them white. The dummy column is there to put all years in one line and not two.

my.data = data.frame(year=c(2001,2003,2005,2008,2009,2012,2015,2016,NA,2019),
                     value=c(runif(10))) %>%
  filter(!is.na(year)) %>%
  mutate(grouped = if_else(lead(year) - year <= 2, "yes", "no")) %>%
  fill(grouped, .direction = "down") %>%
  mutate(dummy = "all")

my.data %>%
  ggplot(aes(x = factor(year),y = value)) +
  geom_line(aes(y = value, group = dummy, color = grouped), show.legend = FALSE) +
  geom_point() +
  scale_color_manual(values = c("yes" = "black", "no" = "white")) +
  theme_classic()

enter image description here

Upvotes: 1

Yazid
Yazid

Reputation: 113


my.data %>%
  ggplot(aes(x=year)) + 
  geom_line(aes(y = ifelse(year <= 2005,value,NA))) + 
  geom_point(aes(y = value)) + 
  scale_x_continuous(breaks=c(2001,2003,2005,2019), minor_breaks=F) +
  theme_minimal()

maybe something like this would work

Upvotes: 1

Related Questions