Home > front end >  Barplots of percentages with raw counts and confidence intervals
Barplots of percentages with raw counts and confidence intervals

Time:02-08

I have frequencies of occurrence of certain particle combinations in English and their respective percentages:

df <- structure(list(partcl = c("yeah well", "well yeah", "yeah yeah well", 
                                "yeah well well", "yeah well yeah", "well yeah well", "well yeah yeah", 
                                "well yeah yeah yeah", "yeah yeah yeah well", "well yeah well well"
), Freq = c(1515L, 408L, 52L, 20L, 17L, 11L, 8L, 2L, 2L, 1L), 
Pct = c(74.3011280039235, 20.0098087297695, 2.55026974006866, 
        0.980872976949485, 0.833742030407062, 0.539480137322217, 
        0.392349190779794, 0.0980872976949485, 0.0980872976949485, 
        0.0490436488474743)), class = c("tbl_df", "tbl", "data.frame"
        ), row.names = c(NA, -10L))

I'd like to plot the percentages (in Pct), while at the same time including the raw counts (in Freq) as well as confidence intervals if that's possible at all. Here's how I plot the barplots:

library(ggplot2)
ggplot(df,
  aes(x = partcl, y = Pct))   
  geom_bar(stat = "identity")  
  # rotate x-axis labels by 90°:
  theme(axis.text.x = element_text(angle = 90))

enter image description here

CodePudding user response:

You could add a label to each bar with the frequency, though Pct and Freq aren't on the same scale.

  geom_text(aes(x = partcl, y = Pct, label = Freq)) 

You have exact counts for each bar, so there wouldn't be a confidence interval as your confidence is 100% that what you have is the correct value.

  •  Tags:  
  • Related