Home > Net >  Cannot change the order of my legend components in ggplot
Cannot change the order of my legend components in ggplot

Time:06-18

Thanks a lot in advance for any help

This is my data:

Samples S Singletons Doubletons Chao1 Jack1
1 43.92 32.04 8.88 101.8 44.1
2 63.8 33.08 17.87 96.43 84.71
3 76.51 32.78 18.42 106.6 102.78
4 86.25 34.27 17.5 120.16 117.02
5 94.4 36.13 16.44 134.01 128.17

I'm trying to change the order of legend in a graphic that represents species accumulation curve with data shown above. The code that I am using for this is this:

p2 <- ggplot(data = data, aes(x = Samples))  
  geom_point(aes(y = S.est.), shape = 22, fill = "turquoise") 
  geom_point(aes(y = Singletons.Mean), shape = 17, fill = "purple") 
  geom_point(aes(y = Doubletons.Mean)) 
  geom_point(aes(y = Chao.1.Mean), shape = 21, fill = "red") 
  geom_point(aes(y = Jack.1.Mean)) 
  geom_line(aes(y = S.est., colour = "S (est)"), size = 1)   
  geom_line(aes(y = Singletons.Mean, colour="Singletons"), size = 1)  
  geom_line(aes(y = Doubletons.Mean, colour="Doubletons"), size = 1)  
  geom_line(aes(y = Chao.1.Mean, colour = "Chao 1"), size = 1)   
  geom_line(aes(y = Jack.1.Mean, colour = "Jack 1"), size = 1)  
  labs(x = "Sampling days", y = "Number of species")  
  scale_x_continuous(limits = c(1, 12), breaks = c(0, 2, 4, 6, 8, 10, 12))  
  scale_y_continuous(limits = c(0, 200), breaks = c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200))  
  scale_color_manual(name = "Richness estimators ", 
                     values=c("#CC0000","#006600", "#669999", "#00CCCC", 
                              "#660099")) 
  theme_bw()

I have try on doing this with many commands as scale_color_manual, scale_fill_discrete and others but nothing works. It's just doesn't change nothing. The order I want is S, Singl, Doubl, Chao and Jack.

enter image description here

Also, any suggestion that can help me make this graph more aesthetic will be very well received.

Thanks a lot and good night.

CodePudding user response:

For the ordering part of the question, the typical approach is to reshape your data long and make the series be an ordered factor.

Given this data:

my_data <- data.frame(
     Samples = c(1L, 2L, 3L, 4L, 5L),
           S = c(43.92, 63.8, 76.51, 86.25, 94.4),
  Singletons = c(32.04, 33.08, 32.78, 34.27, 36.13),
  Doubletons = c(8.88, 17.87, 18.42, 17.5, 16.44),
       Chao1 = c(101.8, 96.43, 106.6, 120.16, 134.01),
       Jack1 = c(44.1, 84.71, 102.78, 117.02, 128.17)
)

We can reshape the data, define the order of the factor, and plot:

library(tidyverse)
my_data %>%
  pivot_longer(-Samples, names_to = "estimator") %>%
  mutate(estimator = factor(
    estimator,
    levels = c("S", "Singletons", "Doubletons", "Chao1", "Jack1"))) %>%
  ggplot(aes(Samples, value, color = estimator))  
  geom_line()   
  theme_bw()

enter image description here

As a factor, we can further manipulate the estimator order if we want. For instance, adding %>% fct_rev at the end of the "estimator" formula will reverse the order:

enter image description here

... or %>% fct_reorder(value) %>% fct_rev to get them in order of value (median, by default):

enter image description here

CodePudding user response:

If you persist to do it in wide format (I really recommend to do it as @Jon Spring suggest), then we have to change scale_color_manual:

First create two vectors, one with your names in order you like, and then the colors, then add to scale_color_manual:

Example data taken from Jon Spring and adapted a little:

library(ggplot2)

  my_colors <- c("#CC0000","#006600", "#669999", "#00CCCC", "#660099")
  my_labels <- c("S", "Singletons", "Doubletons", "Chao1", "Jack1")

ggplot(data = data, aes(x = Samples))  
  geom_point(aes(y = S), shape = 22, fill = "turquoise") 
  geom_point(aes(y = Singletons), shape = 17, fill = "purple") 
  geom_point(aes(y = Doubletons)) 
  geom_point(aes(y = Chao1), shape = 21, fill = "red") 
  geom_point(aes(y = Jack1)) 
  geom_line(aes(y = S, colour = "S (est)"), size = 1)  
  geom_line(aes(y = Singletons, colour="Singletons"), size = 1)  
  geom_line(aes(y = Doubletons, colour="Doubletons"), size = 1)  
  geom_line(aes(y = Chao1, colour = "Chao 1"), size = 1)   
  geom_line(aes(y = Jack1, colour = "Jack 1"), size = 1)  
  labs(x = "Sampling days", y = "Number of species")  
  scale_x_continuous(limits = c(1, 6), breaks = c(0, 2, 4, 6))  
  scale_y_continuous(limits = c(0, 200), breaks = c(0, 20, 40, 60, 80, 100, 120, 140, 160, 180, 200))  
  scale_color_manual(name = "Richness estimators ",
                     labels = my_labels, 
                     values= my_colors) 
  theme_bw()

enter image description here

  • Related