0

I have following barplot (the data frame is much larger IRL):

group <- c('a', 'a', 'a', 'b', 'b', 'b', 'c', 'c', 'c', 'd', 'd', 'e', 'e', 'f', 'f')
condition_a <- c('occasionally', 'often', 'often', 'often', 'occasionally', 'often', 'often', 
'occasionally', 'often', 'often', 'occasionally', 'occasionally', 'often', 'often', 
'occasionally')
condition_c <- c('yes', 'yes', 'no', 'no', 'no', 'no', 'yes', 'yes', 'yes', 'yes', 'yes', 
'no', 'yes', 'no', 'no')
tiff("test.tiff", units="in", width=15, height=5, res=300)
ggplot(data.frame, aes(x = condition_a, fill = condition_c)) +
scale_y_continuous(labels = scales::percent_format()) +
geom_bar(position = position_fill()) +
facet_grid(~ group)
dev.off()

I have problems to adding p-values and number of observations (not percentages). First I thought that I would add number of observations (n=) on top of each colum using geom_text (like this). It didn't work (gave me Error: geom_text requires the following missing aesthetics: y). I didn’t come up with a way to solve this

BUT: I would like to also add p-values (either an asterix or directly a p value) on top of each column (like this). for this reason, the number of observations would be better placed below the columns (under each text "occasionally" and "often"). the graph may otherwise become confusing.

I was unable to add the information described above to the plot. The percentages and facet_grid caused problems. Anyone have good ideas / solutions? Thanks a lot for the help! Graphs really are not my strongest points.

EDIT: I tried to annotate p-values using this quide. There was a problem: I was using geom_signif function, and it gave an Error: stat_signif requires the following missing aesthetics: y

I could do manually the dataframe, that includes percent values (as in quide above). Then the geom_signif works. In this case, the y-axis has only numbers (0, 10, 20, 30...). I would like, as in the figure below, for the y-axis to have percentages (0 %, 10 %, 20 %, 30 %...)

barplot

arto
  • 41
  • 3

0 Answers0