Search code examples
rggplot2stackdplyrgeom-area

GGPLOT2 : geom_area with ordered character variable as x axis


I have a dataset like the following :

dat <- data.frame(sp = c("a", "a", "b", "b", "b", "c", "c"),
nb = c(5, 44, 32, 56, 10, 1, 43),
gp = c("ds1", "ds2", "ds1", "ds2", "ds3", "ds1", "ds3"))

With sp = species ; nb = nb occurrences ; gp = sampling group

I want to make a geom_area graph where values for species (sp) are displayed on y axis, with species grouped on x axis and ordered by descending order based on their total sum.

Up to now I only managed to do that :

ggplot(dat, aes(x=as.numeric(factor(sp)), y=nb, fill=gp, colour = gp)) +
geom_area()

Which gives this output (please don't laugh ;)) output from geom_area

Could you help me to sort the x axis on descending order of the sum of stacked values ? And to fill the empty area ?

E.g. I try to do something like that (here in ascending order, but it no matters) : example


Solution

  • Try this. The gaps in your plot could be filled by filling the df with the missing combinations of gp and sp using tidyr::complete. To reorder the levels of sp I make use of forcats::fct_reorder:

    library(ggplot2)
    library(dplyr)
    library(tidyr)
    library(forcats)
    
    dat <- data.frame(sp = c("a", "a", "b", "b", "b", "c", "c"),
                      nb = c(5, 44, 32, 56, 10, 1, 43),
                      gp = c("ds1", "ds2", "ds1", "ds2", "ds3", "ds1", "ds3"))
    
    dat1 <- dat %>% 
      # Fill with missing combinations of gp and sp
      tidyr::complete(gp, sp, fill = list(nb = 0)) %>% 
      # Reorder according to sum of nb
      mutate(sp = forcats::fct_reorder(sp, nb, sum, .desc = TRUE),
             sp_num = as.numeric(sp))
    
    ggplot(dat1, aes(x=sp_num, y=nb, fill=gp, colour = gp)) +
      geom_area()