barplot with a specific order within missing values are represented by gaps

Hello, I've tried to create a grouped barplot with ggplot. I was quite sucessfull, but there's an issue, I'm unable to fix: I want to have the categories in the fill-argument been shown in a specific order (default alphabetical order is ok) and missing values represented by gaps. Also all bars should have the same width. In my case: I have the percentage of damaged trees within different species height classes and countries. In some contries not all species ocurred in all height classes, therefore they couldn't have been damaged. I was sucessfull in fixing the problem with the different width of the bars in these cases, but not representing missing values by gaps. Also the errorbars were spread all over the place (I used the argument position=position_dodge2(width = 0.9, preserve = "single")). I know that missing values could be confused with zero, so I've labelled each bar with the sample size. In case you are wondering, I posted a similar question before:) with a similar dataset. Has anybody an idea? I have attached the code below, maybe it becomes more understandable what I'm struggling with by running it.


library(tidyverse)

#creating suitable data
data <- data.frame(species = factor(c("beech","maple","spruce","beech","maple","spruce","beech","maple","spruce","beech","maple","beech","spruce","beech","maple")),
                   Heightclass = factor(c("H1","H1","H1","H1","H1","H1","H2","H2","H2","H2","H2","H3","H3","H3","H3")),
                   country=factor(c("Austria", "Austria","Austria","Germany","Germany","Germany","Austria","Austria","Austria","Germany","Germany","Austria","Austria","Germany","Germany")),
                   mean=c(28,30,10,3,50,70,40,10,14,90,6,70,12,68,89),
                   se=c(4,5,8,2,10,7,9,5,7,2,4,9,10,8,2),
                   ntext=c("n=5","n=5","n=5","n=5","n=4","n=5","n=5","n=4","n=5","n=5","n=5","n=5","n=4","n=3","n=3"))
data

#Attemting plotting
data%>%
  ggplot(aes(x = country, y = mean,fill=species)) +
  geom_bar(stat="identity",position="dodge",colour="white")+
  geom_errorbar( aes(x=country, ymin=mean-se, ymax=mean+se),colour="black",  position=position_dodge(.9))+
  labs(x = "", y = "damaged individuals (%)") +
  scale_fill_manual("Species", values = c("beech"="brown","maple"="green","spruce"="blue"))+
  geom_text(aes(label = ntext,y=-2),position=position_dodge(.9),colour="red",fontface="bold",size=3) +
  ylim(-2,100)+
  facet_wrap(~Heightclass,nrow = 3)+
  theme(axis.text.x = element_text(color = "black", size = 20))

Are you looking for something like this?

library(tidyverse)
data <- data.frame(species = factor(c("beech","maple","spruce","beech","maple","spruce","beech","maple","spruce","beech","maple","beech","spruce","beech","maple")),
                   Heightclass = factor(c("H1","H1","H1","H1","H1","H1","H2","H2","H2","H2","H2","H3","H3","H3","H3")),
                   country=factor(c("Austria", "Austria","Austria","Germany","Germany","Germany","Austria","Austria","Austria","Germany","Germany","Austria","Austria","Germany","Germany")),
                   mean=c(28,30,10,3,50,70,40,10,14,90,6,70,12,68,89),
                   se=c(4,5,8,2,10,7,9,5,7,2,4,9,10,8,2),
                   ntext=c("n=5","n=5","n=5","n=5","n=4","n=5","n=5","n=4","n=5","n=5","n=5","n=5","n=4","n=3","n=3"))
data
#>    species Heightclass country mean se ntext
#> 1    beech          H1 Austria   28  4   n=5
#> 2    maple          H1 Austria   30  5   n=5
#> 3   spruce          H1 Austria   10  8   n=5
#> 4    beech          H1 Germany    3  2   n=5
#> 5    maple          H1 Germany   50 10   n=4
#> 6   spruce          H1 Germany   70  7   n=5
#> 7    beech          H2 Austria   40  9   n=5
#> 8    maple          H2 Austria   10  5   n=4
#> 9   spruce          H2 Austria   14  7   n=5
#> 10   beech          H2 Germany   90  2   n=5
#> 11   maple          H2 Germany    6  4   n=5
#> 12   beech          H3 Austria   70  9   n=5
#> 13  spruce          H3 Austria   12 10   n=4
#> 14   beech          H3 Germany   68  8   n=3
#> 15   maple          H3 Germany   89  2   n=3
complete(data, species, Heightclass, country) |> 
  mutate(ntext = ifelse(is.na(mean), "n=0", ntext)) |> 
  ggplot(aes(x = country, y = mean,fill=species)) +
  geom_bar(stat="identity",position="dodge",colour="white")+
  geom_errorbar( aes(x=country, ymin=mean-se, ymax=mean+se),colour="black",  position=position_dodge(.9))+
  labs(x = "", y = "damaged individuals (%)") +
  scale_fill_manual("Species", values = c("beech"="brown","maple"="green","spruce"="blue"))+
  geom_text(aes(label = ntext,y=-2),position=position_dodge(.9),colour="red",fontface="bold",size=3) +
  ylim(-2,100)+
  facet_wrap(~Heightclass,nrow = 3)+
  theme(axis.text.x = element_text(color = "black", size = 20))
#> Warning: Removed 3 rows containing missing values (`geom_bar()`).

Created on 2024-03-04 with reprex v2.0.2

Oh yes, thank you very much!!! <3

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.