I'd like to transform the following ggplot boxplot into a barplot. The information about n, mean, and p should remain. I tried to change geom_boxplot to geom_barplot, which didn't work. Does anyone have an idea how to change it? Thank you very much in advance! I'm grateful for any hints.
p<-ggplot(data_stacked, aes(x = factor, y = attitude, fill = level)) +
geom_boxplot() +
scale_fill_manual(values = c("dimgrey", "lightgrey")) +
stat_compare_means(aes(group = level), label = "p.format", vjust = 5) +
stat_summary(
fun.data = function(x)
data.frame(y= 5, label = paste("mean=",round(mean(
x
), 2))),
geom = "text",
aes(group = level),
hjust = 0.5, vjust = 6.9,
position = position_dodge(0.9)
) +
stat_summary(
fun.data = function(x)
data.frame(y = Inf, label = paste("n=", length(x))),
geom = "text",
aes(group = level),
hjust = 0.5, vjust = 1.2,
position = position_dodge(0.9)
)
p +theme_bw(base_size = 12) + ylim("strongly disagree = 1", "disagree = 2", "neither agree nor disagree = 3", "disagree = 4", "strongly agree = 5")
I tried to change geom_boxplot to geom_bar, which didn't work. I tried the ggpubr package, which didn't let me compute both error bars and significance levels without error message
dput(data_stacked)
"high", "high", "high", "high", "high", "high", "high", "high", "high"low", "low", "low", "low", "low", "low", "low", "low", "low", "low", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", low", "low", "low", "low", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "high", "hiigh", "high", "high", "high", "high", "high", "high", "high", "high", "..... row.names = c(NA, -8592L), class = c("tbl_df", "tbl", "data.frame" ))
class(data_stacked)
[1] "tbl_df" "tbl" "data.frame"
> head(data_stacked)
A tibble: 6 × 3
attitude factor level
<dbl> <chr> <chr>
1 4 public_oversight high
2 4 public_oversight high
3 4 public_oversight high
4 4 authority high
5 5 authority low
6 4 fairness high
I need a barplot similar to this one: enter image description here