1

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

RPO
  • 129
  • 5
  • 1
    I am not totally sure of what you want to produce, could you do a quick draft (a paint draft would do) just to be sure? – Rhesous Aug 06 '20 at 12:51
  • Its done ! If it was possible to have the sp actual names on x axis labels it would be great ! – RPO Aug 06 '20 at 12:54
  • I am currently thinking that there is maybe something to do with 'mutate' function ; but I am still stucked with it... – RPO Aug 06 '20 at 12:55

1 Answers1

1

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()

stefan
  • 90,330
  • 6
  • 25
  • 51