Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

geom_line across multiple linetype and colour levels with a factor x-variable

Here’s some data with x and y values, along with two grouping variables level1 and level2. I’d like to plot the grouping variables with colour and linetype aesthetics, like this:

library(ggplot2)
library(dplyr)

set.seed(100)

dat <- data.frame(
    x = rep(2010:2014, each = 6),
    level1 = rep(letters[1:3], times = 10),
    level2 = rep(rep(c("y", "z"), each = 3), times = 5),
    y = rnorm(30)
)

dat %>%
    ggplot(aes(x = x, y = y, colour = level1, linetype = level2)) +
        geom_line()

plot1

This plot is actually correct. The problem is when I change the x-axis to a factor (or character):

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

dat %>%
    mutate(x = factor(x)) %>%
    ggplot(aes(x = x, y = y, colour = level1, linetype = level2)) +
        geom_line()

geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?

That seems fair enough, I never actually specified the group aesthetic, it just seemed to work out of the box when x was numeric. My first thought was to specify group = level1, but that leads to

Error: geom_path: If you are using dotted or dashed lines, colour, size and linetype must be constant over the line

Which also seems reasonable, because there are actually six lines, and they’re defined by the combination of level1 and level2, not just level1. But I’m not sure it’s possible to specify a multi-level line grouping, e.g.,

dat %>%
    mutate(x = factor(x)) %>%
    ggplot(aes(x = x, y = y, colour = level1, linetype = level2)) +
        geom_line(aes(group = c(level1, level2)))

Error: Aesthetics must be either length 1 or the same as the data (30): group

The desired output is a reproduction of the first plot, but with the x-variable as a factor rather than numeric. Any insight into why the plot seems to work when x is numeric would be interesting as well. Thanks.

>Solution :

You had generally the right idea trying to use two grouping variables. To do this you need to use the interaction between level1 and level2 in the group argument.

library(ggplot2)
library(dplyr)

dat %>%
  mutate(x = factor(x)) %>%
  ggplot(aes(x = x, y = y, colour = level1, linetype = level2, group = interaction(level1, level2))) +
  geom_line()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading