Add percentages into ggplot with grouped data

I have this data about PM10-measurements in Germany. I worked with it a little bit and now it looks like this:

# A tibble: 20 x 5
   state                   surrounding           n_type total    pct
   <fct>                   <chr>                  <int> <int>  <dbl>
 1 Baden-Württemberg, (41) ländlich regional          2    41 0.0488
 2 Baden-Württemberg, (41) ländlich stadtnah          1    41 0.0244
 3 Baden-Württemberg, (41) städtisches Gebiet        22    41 0.537 
 4 Baden-Württemberg, (41) vorstädtisches Gebiet     16    41 0.390 
 5 Bayern, (32)            ländlich regional          3    32 0.0938
 6 Bayern, (32)            ländlich stadtnah          1    32 0.0312
 7 Bayern, (32)            städtisches Gebiet        22    32 0.688 
 8 Bayern, (32)            vorstädtisches Gebiet      6    32 0.188 
 9 Berlin, (11)            ländlich stadtnah          2    11 0.182 
10 Berlin, (11)            städtisches Gebiet         8    11 0.727 
11 Berlin, (11)            vorstädtisches Gebiet      1    11 0.0909
12 Brandenburg, (23)       ländlich regional          3    23 0.130 
13 Brandenburg, (23)       städtisches Gebiet         9    23 0.391 
14 Brandenburg, (23)       vorstädtisches Gebiet     11    23 0.478 
15 Bremen, (8)             ländlich regional          1     8 0.125 
16 Bremen, (8)             städtisches Gebiet         7     8 0.875 
17 Hamburg, (11)           städtisches Gebiet        11    11 1     
18 Hessen, (33)            ländlich regional          1    33 0.0303
19 Hessen, (33)            ländlich stadtnah          1    33 0.0303
20 Hessen, (33)            ländliches Gebiet          6    33 0.182 

What I would like to do now is to make a geom_col-plot with the states on the y-axis and the n_type-variable on the x-axis. I want to fill it by the sourrouding (up to here I manage to do it. See plot below). But I would like to add the percentage to each n_type of each station. I think there must be some way with a geom_text, but I'm not really sure. Thanks for any help and happy christmas:)

The first plot below shows the beginning of adding the percent values as labels. I think it will be difficult to make the labels legible and avoid them overlapping. Making a separate plot of the percentages, as shown in the second plot, might be a cleaner way to show the information.

library(ggplot2)
DF <- read.csv2("~/R/Play/Dummy.csv",dec=".")
ggplot(DF,aes(State, n_type, fill = Surrounding)) + geom_col() +
  geom_text(aes(label = round(percent,2)),position="stack",size=2, angle = -90) +
  coord_flip()


ggplot(DF,aes(State, percent, fill = Surrounding)) + geom_col() +
  coord_flip()

Created on 2020-12-25 by the reprex package (v0.3.0)

1 Like

Thank you very much!! The second plot indeed makes much more sense:)

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.