0

I would like to have different panels of colors depending on the species class. I managed to change the legend correctly to have the right color panel for each class.

The problem is: the colors in the graph are not identical to the ones in the legend. Do you have any idea to fix it?

enter image description here

I did this code :

ggplot(ABUNDANCE, aes(x = Season, y = meanSEQ, fill = Scientific.name)) +
  geom_bar(stat = "identity", position = "dodge", width = .6) +
  labs(x = "", y = expression("Log10 (Mean number of sequences) ")) +
  facet_wrap(~Data.type, ncol = 1, strip.position = "top") +
  scale_fill_manual(
    values = c(
      brewer.pal(3, "Blues")[-1], brewer.pal(9, "Reds")[-1],
      brewer.pal(6, "Oranges")[-1], brewer.pal(5, "OrRd")[-1],
      brewer.pal(6, "BuGn")[-2], brewer.pal(6, "Greens")[-1]
    ),
    drop = FALSE, name = "Plankton species",
    labels = c(
      "Mnemiopsis leidyi", "Obelia sp.", "Calanus finmarchicus",
      "Calanus helgolandicus", "Centropages hamatus", "Centropages sp.",
      "Centropages typicus", "Evadne nordmanni", "Euterpina acutifrons",
      "Luidia sarsi", "Metridia lucens", "Metridia sp.", "Microcalanus sp.",
      "Oithona similis", "Oithona sp.", "Paracalanus parvus", "Penilia avirostris",
      "Podon leuckartii", "Temora longicornis", "Chaetoceros curvisetus",
      "Chaetoceros danicus", "Chaetoceros sp.", "Coscinodiscus wailesii",
      "Ditylum brightwellii", "Protoperidinium divergens", "Rhizosolenia setigera",
      "Skeletonema marinoi", "Tripos sp."
    )
  ) +
  scale_x_discrete(
    expand = c(0, .6),
    limits = c("Summer", "Fall", "Winter", "Spring"),
    labels = c("Summer 2021", "Fall 2021", "Winter 2022", "Spring 2022")
  ) +
  scale_y_log10() +
  theme_bw() +
  theme(legend.title.align = 0.5, legend.text = element_markdown())
2
  • Welcome to SO! It would be easier to help you if you provide a minimal reproducible example including a snippet of your data or some fake data so that others can run your code. To share your data run e.g. dput(ABUNDANCE) and copy the output into your post.
    – stefan
    Commented May 17, 2023 at 23:10
  • 1
    Can you please draw our attention to one of the most obvious spots where the colors aren't matching? Without the data, we don't know which chart bars are meant to match which legend entries.
    – Jon Spring
    Commented May 17, 2023 at 23:11

1 Answer 1

0

Not sure if I understand correctly, but I checked the colours in the legend and the colours in the bars with a colour picker tool and all the colours I checked 'matched up' between the plot and the legend. I'm guessing that your problem is that you have the wrong colours allocated to your species i.e. the 'blue' species aren't all coloured blue, and ggplot is not respecting the order of the species in the scale_fill_manual(values = ...).

If that's the case, a potential workaround is to specify each colour for each species 'manually', e.g.

library(tidyverse)
library(RColorBrewer)

colours_to_choose_from <- c(
  brewer.pal(3, "Blues")[-1], brewer.pal(9, "Reds")[-1],
  brewer.pal(6, "Oranges")[-1], brewer.pal(5, "OrRd")[-1],
  brewer.pal(6, "BuGn")[-2], brewer.pal(6, "Greens")[-1]
  )

colours_to_choose_from
#>  [1] "#9ECAE1" "#3182BD" "#FEE0D2" "#FCBBA1" "#FC9272" "#FB6A4A" "#EF3B2C"
#>  [8] "#CB181D" "#A50F15" "#67000D" "#FDD0A2" "#FDAE6B" "#FD8D3C" "#E6550D"
#> [15] "#A63603" "#FDCC8A" "#FC8D59" "#E34A33" "#B30000" "#EDF8FB" "#99D8C9"
#> [22] "#66C2A4" "#2CA25F" "#006D2C" "#C7E9C0" "#A1D99B" "#74C476" "#31A354"
#> [29] "#006D2C"

# Then, in your scale_fill_manual(), use a named vector:
scale_fill_manual(
  drop = FALSE, name = "Plankton species",
  values = c("Mnemiopsis leidyi" = "#9ECAE1", "Obelia sp." = "#3182BD",
             "Calanus finmarchicus" = "#FEE0D2", ... etc
  ))

# Or, alternatively:
scale_fill_manual(
  drop = FALSE, name = "Plankton species",
  values = c("Mnemiopsis leidyi" = colours_to_choose_from[1],
             "Obelia sp." = colours_to_choose_from[2],
             "Calanus finmarchicus" = colours_to_choose_from[3], ... etc
  ))

Created on 2023-05-18 with reprex v2.0.2

Would this approach solve your problem? Or have I completely misunderstood?

0

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