Help for a stacked barplot

Hi I'm new to R and RStudio and I have a problem with my stacked barplot.

Here is some code I did:

library(tidyverse)

setwd("C:/Users/Moritz Röttgen/Uni OneDrive/OneDrive - uni-oldenburg.de/Bachelorarbeit/Statistik")

Loeffler <-   read_delim("Loeffler_Gesamt1.CSV",";", escape_double = FALSE, trim_ws = TRUE)%>%   
  gather(key = Kategorie, value = Anzahl, -Kolonie, -Nestnummer,-Nestteil, -Art, -Probe)   


kormoran <-  read_delim("Kormoran_Gesamt1.CSV", ";", escape_double = FALSE, trim_ws = TRUE)%>%  
  gather(key = Kategorie, value = Anzahl, -Nestnummer,-Nestteil, -Art)   

Gesamt <- bind_rows(Loeffler, kormoran, .id = NULL)

barplot <- Gesamt%>%
  group_by(Art, Nestteil)%>%
  summarise(Anzahl = sum(Anzahl))%>%
  filter(Nestteil %in% c('Oben','Mitte','Unten'))%>%
  ungroup(Nestteil)%>%
  group_by(Art)%>%
  mutate(Anteil = (100/sum(Anzahl))*Anzahl)

barplot
#> # A tibble: 6 x 4
#> # Groups:   Art [2]
#>   Art      Nestteil Anzahl Anteil
#>   <chr>    <chr>     <dbl>  <dbl>
#> 1 Kormoran Mitte        75  65.2 
#> 2 Kormoran Oben         31  27.0 
#> 3 Kormoran Unten         9   7.83
#> 4 Loeffler Mitte       617  71.5 
#> 5 Loeffler Oben        204  23.6 
#> 6 Loeffler Unten        42   4.87

plot_stacked <- ggplot(barplot, aes(x = Art, y = Anteil , fill = Nestteil))+
  geom_bar(stat = 'identity' , position = 'fill', width = 0.3)+
  geom_text(aes(x= 2, y= 1.1, label = '100% = n863'))+
  geom_text(aes(x= 1, y= 1.1, label = '100% = n115'))+
  scale_y_continuous(breaks = c(0, .25, .5, .75, 1))+
  scale_fill_brewer(palette = 'Greys')+
  labs(x = '', y = expression('Prozentuale Verteilung der gefundenen Müllteile'))+
  theme(panel.background = element_rect(fill = NA),
        panel.grid.major.y = element_line(color='grey', linetype = 20, size = 0.5),
        axis.line.x = element_line(linetype = 'solid'),
        axis.line.y = element_line(linetype = 'solid'),
        strip.background = element_blank(),
        strip.text = element_text(size = 12, face = 'bold'), 
        axis.text.x = element_text(size = 15,   family = 'sans', colour = 'black'),
        axis.title.y = element_text(size = 15, family = 'sans', colour = 'black', hjust = 0.8))

plot_stacked

Created on 2019-06-13 by the reprex package (v0.3.0)

Now what I want to change is that the part with the highest amount (in my case the part "Mitte") is plottet at first and than the lower parts ("Oben","Unten") on top of it. And is there a way to scale the y axis in percent instead of the numeric (0, .25, .5 , .75. 1)? Thanks!

Moritz

Hi,

Welcome.

How about:

library(tidyverse)
barplot <- structure(list(Art = c("Kormoran", "Kormoran", "Kormoran", "Loeffler", 
"Loeffler", "Loeffler"), Nestteil = c("Mitte", "Oben", "Unten", 
"Mitte", "Oben", "Unten"), Anteil = c(65.2, 27, 7.8, 71.5, 23.6, 
4.87)), row.names = c(NA, -6L), class = c("tbl_df", "tbl", "data.frame"
))

# convert Nestteil to a factor specifying levels in desired order
plot_stacked <- ggplot(barplot, aes(x = Art, y = Anteil , fill = factor(Nestteil, levels = c('Unten', 'Oben', 'Mitte'))))+
   # remove postion = 'fill', which was causing the scaling to 0-1
   geom_bar(stat = 'identity', width = 0.3)+
   # change y values to reflect that the y-axis is going to be 0-100
   geom_text(aes(x= 2, y= 110, label = '100% = n863'))+
   geom_text(aes(x= 1, y= 110, label = '100% = n115'))+
   scale_y_continuous(breaks = c(0, 25, 50, 75, 100))+
   # As I did the factor declaration in the ggplot call, I name the legend
   scale_fill_brewer(name= 'Nestteil', palette = 'Greys')+
   labs(x = '', y = expression('Prozentuale Verteilung der gefundenen Müllteile'))+
   theme(panel.background = element_rect(fill = NA),
         panel.grid.major.y = element_line(color='grey', linetype = 20, size = 0.5),
         axis.line.x = element_line(linetype = 'solid'),
         axis.line.y = element_line(linetype = 'solid'),
         strip.background = element_blank(),
         strip.text = element_text(size = 12, face = 'bold'), 
         axis.text.x = element_text(size = 15,   family = 'sans', colour = 'black'),
         axis.title.y = element_text(size = 15, family = 'sans', colour = 'black', hjust = 0.8))
plot_stacked

Created on 2019-06-13 by the reprex package (v0.2.1)

Ron.

1 Like

I guess that was easier than I thought.

Thank you very much for the quick Help!

No problem, happy to help,