0

I gave shapes and colors manually to 4 curves and points using ggplot, but when I am trying to get the legend, everything is in black. How do I color the legend shapes?

This is my code

ggplot() +
  geom_line(data = combined_data, aes(x = number_of_compactions, y = anhydrate_100),
            linetype = 5, size = 1, color = "red") +
  geom_point(data = combined_data, aes(x = number_of_compactions, y = anhydrate_100,
                                       shape = "CBZ Anhydrate 100 MPa"), 
             size = 4, color = "red") +
  geom_line(data = combined_data, aes(x = number_of_compactions, y = anhydrate_250),
            size = 1, color = "blue") + 
  geom_point(data = combined_data, aes(x = number_of_compactions, y = anhydrate_250,
                                       shape = "CBZ Anhydrate 250 MPa"), 
             size = 4, color = "blue") +
  geom_line(data = combined_data, aes(x = number_of_compactions, y = dihydrate_100),
            linetype = 5, size = 1, color = "purple") +
  geom_point(data = combined_data, aes(x = number_of_compactions, y = dihydrate_100,
                                       shape = "CBZ Dihydrate 100 MPa"), 
             size = 4, color = "purple") +
  geom_line(data = combined_data, aes(x = number_of_compactions, y = dihydrate_250),
            size = 1, color = "black") +
  geom_point(data = combined_data, aes(x = number_of_compactions, y = dihydrate_250,
                                       shape = "CBZ Dihydrate 250 MPa"), 
             size = 4, color = "black") +
  labs(x = "Number of Compactions", y = TeX("\\textbf{Sticking Mass (\\textbf{$\\mu}g)}")) +
  scale_x_continuous(breaks = seq(0, 100, by = 10),
                     expand = c(0,0),
                     limits = c(0, 105)) +
  scale_y_continuous(breaks = seq(0, 160, by = 20),
                     expand = c(0,0),
                     limits = c(0, 150)) +
  scale_shape_manual(values = c(1, 16, 2, 17),
                     labels = c("CBZ Anhydrate 100 MPa", "CBZ Anhydrate 250 MPa",
                                "CBZ Dihydrate 100 MPa", "CBZ Dihydrate 250 MPa")) +
  scale_fill_manual(values = c("red", "blue", "purple", "black"),
                    labels = c("CBZ Anhydrate 100 MPa", "CBZ Anhydrate 250 MPa",
                               "CBZ Dihydrate 100 MPa", "CBZ Dihydrate 250 MPa")) +
  theme(panel.background = element_rect(fill = NA), 
        panel.border = element_rect(fill = NA, colour = "black", size = 1.2),
        axis.text.y = element_text(color = "black", size = 12, face = "bold"),
        axis.text.x = element_text(colour = "black", size = 12, face = "bold"),
        axis.title.x = element_text(size = 18, face = "bold"),
        axis.title.y = element_text(size = 18),
        legend.title = element_blank(),
        legend.background = element_rect(fill = NA),
        legend.box.background = element_rect(fill = NA),
        legend.key = element_rect(fill = NA),
        legend.position = "top")

enter image description here

Things I tried are in the code.

Vikram
  • 1
  • 1
    It might have something to do with using `scale_fill_manual` instead of `scale_color_manual`. Unfortunately without your data it's tough to reproduce the problem. [How to make a great R reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) – Harrison Jones Apr 11 '23 at 15:40

1 Answers1

1

Mapping the same variable to each aesthetic makes it easier to get the legend you are looking for. Without seeing the original data frame, this may require more upstream manipulation of your data than I show here. I made up some data to use for this example.

Please note there is no need to add a fill scale since neither geom you've added is using this aesthetic.

library(tidyverse)

combined_data <- tibble(compactions = rep(c(10,20,40,60,80,100), times = 4),
                        mass = c(5,15,15,40,40,40,
                                 25,50,55,70,90,100,
                                 40,35,50,40,35,45,
                                 100,150,135,150,140,140),
                        cbz = c(
                          rep('anhydrate', times = 6),
                          rep('dihydrate', times = 6),
                          rep('anhydrate', times = 6),
                          rep('dihydrate', times = 6)
                          ),
                        pressure = c(rep(250, times = 6),
                                     rep(250, times = 6),
                                     rep(100, times = 6),
                                     rep(100, times = 6)))

combined_data %>%
  mutate(pressure = factor(pressure),
         group = paste(cbz, pressure)) %>%
  ggplot(aes(compactions, mass, color = group, shape = group, linetype = group)) +
  geom_point() +
  geom_line() +
  scale_linetype_manual(values = c(5,1,5,1),
                        labels = c("CBZ Anhydrate 100 MPa", "CBZ Anhydrate 250 MPa",
                                   "CBZ Dihydrate 100 MPa", "CBZ Dihydrate 250 MPa")) +
  scale_shape_manual(values = c(1,16,2,17),
                     labels = c("CBZ Anhydrate 100 MPa", "CBZ Anhydrate 250 MPa",
                                "CBZ Dihydrate 100 MPa", "CBZ Dihydrate 250 MPa")) +
  scale_color_manual(values = c("red", "blue", "purple", "black"),
                     labels = c("CBZ Anhydrate 100 MPa", "CBZ Anhydrate 250 MPa",
                                "CBZ Dihydrate 100 MPa", "CBZ Dihydrate 250 MPa")) +
  labs(linetype = '', shape = '', color = '') +
  theme(panel.background = element_rect(fill = NA), 
        panel.border = element_rect(fill = NA, colour = "black", linewidth = 1.2),
        axis.text.y = element_text(color = "black", size = 12, face = "bold"),
        axis.text.x = element_text(colour = "black", size = 12, face = "bold"),
        axis.title.x = element_text(size = 18, face = "bold"),
        axis.title.y = element_text(size = 18),
        legend.title = element_blank(),
        legend.background = element_rect(fill = NA),
        legend.box.background = element_rect(fill = NA),
        legend.key = element_rect(fill = NA),
        legend.position = "top")

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

Seth
  • 1,659
  • 1
  • 4
  • 11