0

I used "scale_shape_manual" and "scale_color_manual" to color and shape 5 different variables, they are not on the same column. I want the legend to be just one merged, not separated, like the image.

Reference

I want the legend to be the shape colored by the color assigned. Any idea of how to do this?

I'm using pipelines. Here is the code:

underlag3 %>% 
  mutate_at(c('fjarrvarme', 'oljeeldning','ovrigt','fjarrvarme_komb',
              'gas','elvarme','bergvarme','bio'), as.numeric) %>% 
  ggplot(aes(x=ar)) +
  geom_line(aes(y=fjarrvarme), color="#000080") +
  geom_point(aes(y=fjarrvarme, shape="Fjarrvarme",color="Fjarrvarme")) +
  geom_line(aes(y=fjarrvarme_komb), color="#363636") +
  geom_point(aes(y=fjarrvarme_komb, shape="FjarrVarme Komb", color="FjarrVarme Komb")) +
  geom_line(aes(y=elvarme), color="#544C4A") +
  geom_point(aes(y=elvarme, shape="elvarme", color="elvarme")) +
  geom_line(aes(y=bio), color="black") +
  geom_point(aes(y=bio, shape="bio", color="bio")) +
  geom_line(aes(y=bergvarme), color="#5097A4") +
  geom_point(aes(y=bergvarme, shape="bergvarme", shape="color")) +
  geom_line(aes(y=oljeeldning), color="#787276") +
  geom_point(aes(y=oljeeldning, shape="oljeeldning", color="oljeeldning")) +
  geom_line(aes(y=gas), color="#4682b4") +
  geom_point(aes(y=gas, shape="gas", color="gas")) +
  geom_line(aes(y=ovrigt), color="#222021") +
  geom_point(aes(y=ovrigt, shape="ovrigt", color="ovrigt")) +
  scale_x_continuous(n.breaks = 41) +
  scale_shape_manual(values = c('Fjarrvarme' = 17, 'FjarrVarme Komb' = 18,
                                'elvarme' = 4, 'bio' = 16,
                                'bergvarme' = 16, 'oljeeldning' = 15,
                                'gas' = 16, 'ovrigt' = 16)) +
  scale_color_manual(values = c('Fjarrvarme' = "#000080", 'FjarrVarme Komb' = "#363636",
                                'elvarme' = "#544C4A", 'bio' = "black",
                                'bergvarme' = "#5097A4", 'oljeeldning' = "#787276",
                                'gas' = "#4682b4", 'ovrigt' = "#222021"))

2
  • Does it resolve if you assign the same name to both scales? e..g scale_shape_manual(name = "my legend", ....
    – Jon Spring
    Commented Nov 26, 2023 at 17:16
  • 2
    This also looks like a prime example where your life will be made simpler if you reshape your data longer and assign the column to color & shape.
    – Jon Spring
    Commented Nov 26, 2023 at 17:18

2 Answers 2

2
library(tidyverse)
mtcars |>
  pivot_longer(disp:qsec) |>
  ggplot(aes(mpg, value, color = name, shape = name)) +
  geom_line() +
  geom_point() +
  scale_shape_manual(values = c('disp' = 0, 'drat' = 1, 'hp' = 2,
                                'qsec' = 3, 'wt' = 4),
                     name = "my legend") +
  scale_color_manual(values = c('disp' = "blue", 'drat' = "red", 'hp' = "green",
                                'qsec' = "gray20", 'wt' = "gray80"),
                     name = "my legend")

enter image description here

2

This type of problems generally has to do with reshaping the data. The format should be the long format and the data is in wide format. See this post on how to reshape the data from wide to long format.

I will first create named vectors for the legend labels, colors and shapes.

nms <- c('fjarrvarme', 'fjarrvarme_komb', 'elvarme', 'bio',
         'bergvarme', 'oljeeldning', 'gas', 'ovrigt')
labls <- setNames(c('Fjarrvarme', 'Fjarrvarme Komb', 'elvarme', 'bio',
                    'bergvarme', 'oljeeldning', 'gas', 'ovrigt'), nms)
clrs <- setNames(c("#000080", "#363636", "#544C4A", "black", "#5097A4", 
                   "#787276", "#4682b4", "#222021"), nms)
shps <- setNames(c(17, 18, 4, 16, 16, 15, 16, 16), nms)

Created on 2023-11-26 with reprex v2.0.2

Now plot the data with the data example in the end of this post.

suppressPackageStartupMessages({
  library(tidyverse)
})

underlag3 %>%
  pivot_longer(-ar, names_to = "Variable") %>%
  ggplot(aes(x = ar, y = value, color = Variable, shape = Variable)) +
  geom_line() +
  geom_point() +
  scale_shape_manual(labels = labls, values = shps) +
  scale_color_manual(labels = labls, values = clrs) +
  theme_bw()

Created on 2023-11-26 with reprex v2.0.2


Data example

set.seed(2023)
underlag3 <- sapply(seq_along(labls), \(mu) cumsum(rnorm(20, mean = mu))) %>%
  as.data.frame() %>%
  mutate(ar = seq_along(V1)) %>%
  setNames(c(nms, 'ar'))

Created on 2023-11-26 with reprex v2.0.2

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