Legend adjustement

Hello,

I would need to modify the aspect of my legend.
I have this R code and this plot :

rr <- tribble(
  ~zone, ~type, ~station, ~species, ~number,
  'A1', 'Adult', 1, 'Atlanticus', 2, 
  'A1', 'Adult', 1, 'Olrikii', 1, 
  'A1', 'Larvae', 2, 'Medius', 5, 
  'A2', 'Larvae', 1, 'Glacialis', 7, 
  'A2', 'Larvae', 2, 'Unidentified', 3,
  'A2', 'Adult', 2, 'Glacialis', 2, 
  'A2', 'Larvae', 2, 'Medius', 4, 
  'A3', 'Zoo', 1, 'Capilatta', 17, 
  'A3', 'Adult', 3, 'Olrikii', 1
)

rr %>% group_by(zone) %>% mutate(Percent = 100 * number/sum(number)) %>% 
  ggplot(aes(zone, y = Percent, fill = species)) + geom_col(position = "dodge")

Created on 2022-06-07 by the reprex package (v2.0.1)

What I want to do it's :

  • Change the display order of my legend to have a descending order, i.e. have my most abundant species first etc.
  • Finally I would also like to be able to display for example only the 3 most abundant species (and always in descending order).

And a little bonus if anyone ever knows how to do it, it would be to color my station names (in x) with respect to the Area column

I've searched several forums and couldn't find anything to do this.

Thanks !

Maybe this is the plot that you need.

rr %>% group_by(zone) %>% mutate(Percent = 100 * number/sum(number)) %>% 
  ggplot(zone, mapping= aes(x = reorder(zone, -Percent), y = Percent, fill=species)) +
         geom_bar(stat = "identity",position = "dodge")

Im suggest you think in this other plot, because you y-axis is a percent. Maybe is better for present the data.

rr %>% group_by(zone) %>% mutate(Percent = 100 * number/sum(number)) %>% 
  ggplot(zone, mapping= aes(x = reorder(zone, -Percent), y = Percent, fill=species)) +
         geom_bar(stat = "identity")

Thanks a lot for your answer !
Actually it's not really what I expected haha
I would really like the species in my legend (fill = Genus_Sp) to be arranged in descending order with respect to the proportion they represent in total (Percent)

And moreover, in my original data file I have a lot of species, hence my wish to plot only the top 10

Do you see what I mean? I don't want it to be my zones that are in ascending order but just in the aspect of my legend

What I'm looking for would look more like your last graph but with the legend organized so that it has first:

  • Capilatta
  • Middle
  • Glacialis
    Etc

And the best would be to represent only these 3 species (top3)

Thank you so much !
Hersh

Like this?

# generate order
legend_order <- rr %>% 
  group_by(species) %>% 
  summarise(number = sum(number)) %>% 
  mutate(order = rank(number)) %>% 
  arrange(desc(order)) %>% 
  pull(species)

rr %>% group_by(zone) %>% mutate(Percent = 100 * number/sum(number)) %>% 
  ggplot(aes(zone, y = Percent, fill = species)) + 
  geom_col(position = "dodge") +
  scale_fill_discrete(breaks = legend_order) # control the order

2 Likes

Hi !
Thanks a lot ! That's pretty much what I wanted, I managed to adapt it to my dataset!
How would you do to represent only the top3 of my species?

Do you mean having the top three then replace the others with 'other'? Or do you mean something else?

Yes it what I mean, having the top 3 of my species and add the others species in an "other" category. But I still want to keep legend_order you did

Thanks for your time !

So like this?

library(tidyverse)
# new dataset
rr2 <- rr %>% 
  mutate(species = fct_lump_n(species, 3, w = number))

# generate order
legend_order <- rr2 %>% 
  group_by(species) %>% 
  summarise(number = sum(number)) %>% 
  mutate(order = rank(number)) %>% 
  arrange(desc(order)) %>% 
  pull(species)

# graph from new dataset
rr2 %>% 
  group_by(zone) %>% 
  mutate(Percent = 100 * number/sum(number)) %>% 
  ggplot(aes(zone, y = Percent, fill = species)) + 
  geom_col(position = "dodge") +
  scale_fill_discrete(breaks = legend_order) # control the order

This is a better way of doing it. A lot less code with the same result.

library(tidyverse)

rr <- tribble(
  ~zone, ~type, ~station, ~species, ~number,
  'A1', 'Adult', 1, 'Atlanticus', 2, 
  'A1', 'Adult', 1, 'Olrikii', 1, 
  'A1', 'Larvae', 2, 'Medius', 5, 
  'A2', 'Larvae', 1, 'Glacialis', 7, 
  'A2', 'Larvae', 2, 'Unidentified', 3,
  'A2', 'Adult', 2, 'Glacialis', 2, 
  'A2', 'Larvae', 2, 'Medius', 4, 
  'A3', 'Zoo', 1, 'Capilatta', 17, 
  'A3', 'Adult', 3, 'Olrikii', 1
)

rr %>% group_by(zone) %>% 
  mutate(Percent = 100 * number/sum(number)) %>% 
  ggplot(aes(zone, y = Percent, 
             fill = fct_reorder(fct_lump_n(species, 3, w = number), desc(number)))) +
  geom_col(position = "dodge") +
  labs(fill = "species")

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.