1

I have color for each variable (fishing strategy), however, if I put it in a facet grid like this, based on the years, I can't set up the colors accordingly. I want to have one color for each fishing strategy instead of one color for each year, but also need the legend for fishing strategies with color or just years without color. But I didn't manage to do that. Can someone help me with this?

With this code:

    spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year))) + geom_bar(stat="identity", position="dodge") 
+ theme_minimal()

    spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") 
+ labs(fill = "Year", x = "Fishing strategies", y = "Total REA", title = "Based on the REA") 
+ theme(text = element_text(size = 13)) 
+ theme(legend.position = "bottom")
+ theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 
+
      scale_fill_manual(values = c("GIL_COD" = "#004c6d",
                                   "GIL_FRS" = "#00ffff",
                                   "GIL_FLE" = "#00a1c1",
                                   "GIL_HER" = "#00cfe3",
                                   "PAS_FLA" = "#78ab63",
                                   "POL_FRS" = "#6efa75",
                                   "BST_MIX" = "#ffc334",
                                   "MPT_HER" = "#ff9509",
                                   "BPT_HER" = "#ffb6de",
                                   "BPT_COD" = "#cc0089"))

I get this

enter image description here

but if I removed the scale fill manual part, it looked like this

enter image description here

  • It looks like you're fairly new to SO; welcome to the community! If you want great answers quickly, it's best to make your question reproducible. This includes sample data like the output from `dput(head(dataObject)))`. Check it out: [making R reproducible questions](https://stackoverflow.com/q/5963269). – Kat Feb 24 '22 at 17:31
  • You are mapping year to fill (`fill= factor(year)`) but it sounds like you want strategy to be mapped to fill. – Jon Spring Feb 24 '22 at 17:32
  • You are using Year for the fill colour, then specifying the actual fill colours according to the fishing strategy. There is no year called `GIL_COD`, so the plot doesn't take this colour. If you want to fill according to fishing strategy, then set `fill = effort` – Allan Cameron Feb 24 '22 at 17:32
  • ^ that's right, but I think strategy is specified in a different variable than effort, which seems to be mapped to y already. – Jon Spring Feb 24 '22 at 17:33

1 Answers1

0

I think I've worked it out, but I don't have your data. (It's a bit messy.)

I used the dataset diamonds and renamed the fields. The first plot is supposed to represent your second plot, where you have removed the scale_color_manual.

The data first:

library(tidyverse)
# create variables
io1 <- diamonds %>% 
  mutate(year = cut,
         effort = x,
         clu_name2 = color,
         vessel_category = rep(c("Passive","Active"), nrow(diamonds)/2),
         geartype_clu2 = rep(LETTERS[1:3], nrow(diamonds)/3))
levels(io1$year) <- c(2019:2023)

Original plot as you've coded:

# grid faceting/color
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year))) + 
  geom_col(position = "dodge") +
  theme_minimal()

spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") +
  labs(fill = "Year", x = "Fishing strategies", y = "Total REA", 
        title = "Based on the REA") +
  theme(text = element_text(size = 13)) +
  theme(legend.position = "bottom") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) 

enter image description here

The primary differences are the arguments group = year and fill = clu_name2.

p2 <- ggplot(io1, aes(x = clu_name2, y = effort, group = year, fill = clu_name2)) +
  geom_col(position = "dodge") +
  theme_minimal()

p2 + facet_grid(vessel_category~geartype_clu2, scales = "free") +
  labs(fill = "", x = "Fishing strategies\ngrouped by years", 
       y = "Total REA", title = "Based on the REA") +
  theme(text = element_text(size = 13)) +
  theme(legend.position = "bottom") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

Keep in mind the legend here is fishing strategies. If you want the years shown, perhaps a second depth label with scale_fill_manual() would be a good approach.

enter image description here

Now as far getting the years and the strategies in the legend or as legends. You may be better off with adding a second scale axis and using the first version. This can be done with the package ggnewscale. You'll have to adjust the font size or expand or add to the margin, though.

# grid faceting/color
spaclu <- ggplot(io1, aes(y= effort, x=factor(clu_name2), fill= factor(year),
                          group = year)) + 
  geom_col(position = "dodge") +
  theme_minimal()

spaclu + facet_grid(vessel_category~geartype_clu2, scales = "free") +
  labs(fill = "Year", x = "Fishing strategies", y = "Total REA", 
        title = "Based on the REA") +
  theme(text = element_text(size = 13)) +
  theme(legend.position = "bottom") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  ggnewscale::new_scale("fill") +         # added scale here
  geom_tile(aes(fill = clu_name2, y = 1)) +
  scale_fill_discrete(name = "Strategies")

enter image description here

It doesn't quite work out when using it with the second option

p2 <- ggplot(io1, aes(x = clu_name2, y = effort, group = year, fill = clu_name2)) +
  geom_col(position = "dodge") + 
  theme_minimal()

p2 + facet_grid(vessel_category~geartype_clu2, scales = "free") +
  labs(fill = "", x = "Fishing strategies\ngrouped by years", 
       y = "Total REA", title = "Based on the REA") +
  theme(text = element_text(size = 13)) +
  theme(legend.position = "bottom") + 
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1)) +
  scale_fill_manual(values = c("D" = "#004c6d",
                               "E" = "#00ffff",
                               "F" = "#00a1c1",
                               "G" = "#00cfe3",
                               "H" = "#78ab63",
                               "I" = "#6efa75",
                               "J" = "#ffc334",
                               "K" = "#ff9509",
                               "L" = "#ffb6de",
                               "M" = "#cc0089")) +
  ggnewscale::new_scale("fill") +
  geom_tile(aes(fill = year, y = 1)) +
  scale_fill_viridis_d(name = "Years")

enter image description here

Kat
  • 15,669
  • 3
  • 18
  • 51