1

I am having trouble figuring out what arguments to use in geom_text.

I have the following data frame:

head(dataset)
# A tibble: 6 x 2
  diameter trip 
  <fct>    <fct>
1 17.5     0    
2 17.5     1    
3 17.5     0    
4 14.75    0    
5 17.5     1    
6 16       0    

levels(dataset$diameter)
[1] "14.75" "16"    "16.5"  "17.5" 
levels(dataset$trip)
[1] "0" "1"

I call ggplot and it returns the following graphic.

image before labeling:

enter image description here

dataset %>% 
  ggplot(aes(x = diameter,
             fill = trip)) +
  geom_bar(position = 'fill') +
  ylab("relative frequencies")

I would like to know how to add labels that show the count of samples within each diameter, separated by trip. Something like this:

image I want to build:

enter image description here

I tried a lot finding it on google, without success.

I saw some people showing this post as a duplicate of the post below. Isn't there a way, with the code I have, to just add a geom_text and get what I want? It isn't exactly the same problem since his label values are from the same type shown in his y axis, while in my case, the y axis just show percentages and I want the labels to show counts.

Showing data values on stacked bar chart in ggplot2

daniellga
  • 1,142
  • 6
  • 16
  • Isn't there a way, with the code I have, to just add a geom_text and get what I want? It isn't exactly the same problem since his label values are from the same type shown in his y axis, while in my case, the y axis just show percentages and I want the labels to show counts. – daniellga Jun 11 '19 at 17:38
  • There are a lot of posts already covering different aspects of this type of task—put "[ggplot2] label stack bars" in the search bar. Your best bet if you want specific formatting (counts vs percentages) is to do the summary statistics yourself instead of trying to get `geom_bar` to do them. You need something to supply as the label in `geom_text`, and right now you don't seem to have that – camille Jun 11 '19 at 18:48

0 Answers0