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