Home > Software design >  ggplot2, Ordering y axis descendent
ggplot2, Ordering y axis descendent

Time:07-02

I have a data frame called tb like this one

   country station taxa scientific_name     var1
1      USA    GA01    A               a 23.42532
2      USA    GA02    A               b 23.10565
3      USA    GA03    A               c 23.88142
4      USA    GA04    B               d 23.18996
5      USA    GA05    B               e 23.32707
6      USA    GA06    B               f 23.78226
7      USA    GA07    C               g 23.11350
8      USA    GA08    C               h 23.67787
9      USA    GA09    F               i 23.69604
10     USA    GA10    F               l 23.68882
11     USA    GA11    F               m 23.90722
12     USA    GA12    F               n 23.23886
13     USA    GA13    F               o 23.53097
14     FRA    XC01    A               a 23.62910
15     FRA    XC02    A               b 23.08866
16     FRA    XC03    C               c 23.06928
17     FRA    XC04    C               d 23.72957
18     FRA    XC05    C               e 23.27950
19     FRA    XC06    D               f 23.37991
20     FRA    XC07    D               g 23.42608
21     FRA    XC08    D               h 23.54862
22     FRA    XC09    D               i 23.85336
23     FRA    XC10    D               l 23.93866
24     FRA    XC11    F               m 23.69664
25     FRA    XC12    F               n 23.72080
26     FRA    XC13    F               o 23.27925
27     FRA    XC14    F               p 23.38820
28     ITA    BV01    A               a 23.57040
29     ITA    BV02    B               b 23.10689
30     ITA    BV03    B               c 23.05586
31     ITA    BV04    C               d 23.98265
32     ITA    BV05    C               e 23.40625
33     ITA    BV06    C               f 23.93994
34     ITA    BV07    F               g 23.51695
35     ITA    BV08    F               h 23.48211
36     ITA    BV09    F               i 23.21569
37     ITA    BV10    F               l 23.08469

I am plotting the data using ggplot but I do not manage to order the y-axis as I desire.

ggplot_df <- tb %>%
  group_by(country) %>%
  arrange(country,taxa,scientific_name)

ggplot_df$taxa_species_ordered <- paste(ggplot_df$taxa," - ",ggplot_df$scientific_name)

plot_species_zones <-
  ggplot(data = ggplot_df,
         aes(fill = country,
             x = taxa_species_ordered))  
  geom_point(aes(y=var1),
             size = 3,
             shape = 21) 
  theme_bw()  
  guides(fill=FALSE, color=FALSE) 
  coord_flip()


show(plot_species_zones)

Here, alphabets on Y-axis follow a bottom-up descending order. I want a top-down descending order, where A is on the top.

I tried to arrange the table as following:

ggplot_df2 <- ggplot_df %>%
  arrange(taxa_species_ordered)

and plotting again according to ggplot_df2 but I still do not get want I want.

CodePudding user response:

Simply arranging your dataset has no effect on the order of the categories. Instead you have to convert your taxa_species_ordered column to a factor and set order of the levels in your desired order.

To this end you could first arrange your data, then set the order via forcats::fct_inorder. Afterwards, reverse the order via forcats::fct_rev:

Note: Additionally I switched to role of x and y to get rid of coord_flip and used e.g. fill = "none" instead of fill = FALSE` as the latter is deprecated and will result in a warning.

library(dplyr)
library(forcats)
library(ggplot2)

ggplot_df <- tb

ggplot_df$taxa_species_ordered <- paste(ggplot_df$taxa, " - ", ggplot_df$scientific_name)

ggplot_df <- ggplot_df %>%
  arrange(taxa, scientific_name) |> 
  mutate(taxa_species_ordered = fct_rev(fct_inorder(taxa_species_ordered)))

ggplot(
  data = ggplot_df,
  aes(
    fill = country,
    y = taxa_species_ordered
  )
)  
  geom_point(aes(x = var1),
    size = 3,
    shape = 21
  )  
  theme_bw()  
  guides(fill = "none", color = "none")

enter image description here

CodePudding user response:

Use scale_x_discrete(limits=rev)

library(tidyverse)
df %>%
  group_by(country) %>%
  arrange(country,taxa,scientific_name) %>% 
  ggplot(aes(fill = country,
             x = taxa))  
  geom_point(aes(y=var1),
             size = 3,
             shape = 21) 
  scale_x_discrete(limits=rev)  
  theme_bw()  
  guides(fill=FALSE, color=FALSE) 
  coord_flip()

enter image description here

CodePudding user response:

base R option using factor where you define the levels=c(sort(unique())) of your alphabetic order:

library(ggplot2)
library(dplyr)
ggplot_df <- tb %>%
  group_by(country) %>%
  arrange(country,taxa,scientific_name)

ggplot_df$taxa_species_ordered <- paste(ggplot_df$taxa," - ",ggplot_df$scientific_name)

ggplot_df$taxa_species_ordered <- factor(ggplot_df$taxa_species_ordered, levels = c(sort(unique(ggplot_df$taxa_species_ordered), decreasing = TRUE)))

plot_species_zones <-
  ggplot(data = ggplot_df,
         aes(fill = country,
             x = taxa_species_ordered))  
  geom_point(aes(y=var1),
             size = 3,
             shape = 21) 
  theme_bw()  
  guides(fill=FALSE, color=FALSE) 
  coord_flip()
#> Warning: `guides(<scale> = FALSE)` is deprecated. Please use `guides(<scale> =
#> "none")` instead.


show(plot_species_zones)

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

  • Related