0

I'm creating a simple point and line plot in ggplot similar to the example below:

dat <- iris %>%                               # dummy data
       select(Sepal.Length) %>% 
       mutate(Type = "Sepal.Length")

ggplot() +
geom_point(data = dat, aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), colour = "orange") +
scale_shape_manual(values =  10) +
geom_hline(aes(yintercept = 6, linetype = 'C Red line'), colour = "red", size = 0.5) +
geom_hline(aes(yintercept = 5, linetype = 'A Blue line'), colour = "darkblue", size = 0.5) +
geom_hline(aes(yintercept = 7, linetype = 'B Green line'), colour = "forestgreen", size = 0.5) +
scale_linetype_manual(values = c('solid', 'dashed', 'dotted')) +
labs(linetype = "Line legend") +
labs(shape = "Point legend")

Example plot

I'ev worked out that the alphabetical order of the 'names' associated with each line controls the order in the legend and I can match a desired line style to those lines using scale_linetype_manual. However, I cannot work out how to get the colours of the lines on the plot to be matched by the colours in linetype legend, which just uses the last specifed line colour?

2
  • 1
    This is exactly why I prefer the crystal clear logic of low level plotting, try clr <- c('orange', 'red', 'darkblue', 'forestgreen'); plot(dat$Sepal.Length, pch=20, col=clr[1]); abline(h=c(6, 5, 7), lty=c(3, 1, 2), col=clr[-1]); legend('topleft', legend=c('Sepal.Length', 'Blue', 'Green', 'Red'), pch=c(20, rep(NA, 3)), col=clr, lty=c(NA, c(3, 1, 2))), servus!
    – jay.sf
    Commented Jun 1, 2022 at 5:35
  • 1
    I agreed that the base R is much more intuitive but I'm trying to learn how to do things in GGPLOT to increase my skill
    – Markm0705
    Commented Jun 1, 2022 at 6:06

1 Answer 1

2

Here is what I would do.

  1. Instead of 3 individual geom_hline calls, let ggplot2 automatically plot horizontal lines based on data from a vert_data data.frame.
  2. Combine legends for linetype and colour.
vert_data <- data.frame(
    yintercept = c(6, 5, 7),
    name = c("C Red line", "A Blue line", "B Green line"),
    linetype = c("dotted", "solid", "dashed"),
    colour = c("red", "darkblue", "forestgreen"))

ggplot() +
    geom_point(
        data = dat, 
        aes(x = as.numeric(row.names(dat)), y = Sepal.Length, shape = Type), 
        colour = "orange") +
    scale_shape_manual(values =  10) +
    geom_hline(
        data = vert_data,
        aes(yintercept = yintercept, linetype = name, colour = name),
        size = 0.5) +
    scale_linetype_manual(
        "Line legend", values = setNames(vert_data$linetype, vert_data$name)) +
    scale_colour_manual(
        "Line legend", values = setNames(vert_data$colour, vert_data$name)) +
    labs(shape = "Point legend")

enter image description here

1
  • I like your solution to the problem but I would still be interested if it is possible to get GGPLOT a solution to the case of adding a line at a time
    – Markm0705
    Commented Jun 1, 2022 at 6:08

Not the answer you're looking for? Browse other questions tagged or ask your own question.