I am trying to find a way to display the means level of agreement (with strongly agree = 5 and strongly disagree = 1), without losing my labels on the legend. In other words, I would like to keep the dataframe the same but still be able to report the mean onto the histogram, like this (I manually did this in sheets. Would like to do it automatically in r):
Here is my dataframe and code:
Year2_likert_exhibits <-structure(list(Item = c("Were informative and educational", "Were easy to read",
"Covered topics I liked", "Were introduced in an informative way by staff",
"Were informative and educational", "Were easy to read", "Covered topics I liked",
"Were introduced in an informative way by staff", "Were informative and educational",
"Were easy to read", "Covered topics I liked", "Were introduced in an informative way by staff",
"Were informative and educational", "Were easy to read", "Covered topics I liked",
"Were introduced in an informative way by staff", "Were informative and educational",
"Were easy to read", "Covered topics I liked", "Were introduced in an informative way by staff"
), Evaluation = c("Strongly agree", "Strongly agree", "Strongly agree",
"Strongly agree", "Somewhat agree", "Somewhat agree", "Somewhat agree",
"Somewhat agree", "Neither agree nor disagree", "Neither agree nor disagree",
"Neither agree nor disagree", "Neither agree nor disagree", "Somewhat disagree",
"Somewhat disagree", "Somewhat disagree", "Somewhat disagree",
"Strongly disagree", "Strongly disagree", "Strongly disagree",
"Strongly disagree"), Value = c(125, 131, 134, 94, 37, 30, 28,
34, 4, 1, 3, 33, 4, 4, 2, 4, 2, 3, 3, 6)), class = c("tbl_df",
"tbl", "data.frame"), row.names = c(NA, -20L))
####Impressions of Exhibits (Year 2) ####
Year2_likert_exhibits %>%
ggplot()
geom_bar(data = Year2_likert_exhibits, aes(x = Item, y=Value, fill=reorder(Evaluation, -Value)), position="stack", stat="identity")
coord_flip()
ggtitle("Year 2 - Impressions of Exhibits")
ylab("Count")
xlab("The Exhibits...")
theme_minimal()
theme(legend.position="right",
plot.title = element_text(size=22, hjust = 1),
axis.title.y = element_text(size=14,face="bold"),
axis.text.y = element_text(size = 12),
axis.title.x = element_text(size = 12),
axis.text.x = element_text(size = 12),
panel.grid.major = element_blank(), #remove x axis grid
panel.grid.minor = element_blank(),
axis.line = element_line(colour = "black"))
guides(fill=guide_legend(title="Evaluation"))
scale_fill_brewer(palette="Spectral", direction = -1)
scale_x_discrete(labels = function(x) str_wrap(x, width = 17))
## if needed - scale_x_discrete(labels=str_wrap(c('Covered topics that I liked', 'Were easy to read', 'Were informative and educational', 'Were introduced in an informative way by staff'), width = 17)) #MAKE SURE TO RUN CODE 377 - 395 BEFORE RUNNING THIS TO MAKE SURE THE LABELS CORRESPOND TO THE CORRECT DATA
CodePudding user response:
You can create a data frame with the means:
library(dplyr)
library(glue)
df_mean <- Year2_likert_exhibits |> group_by(Item) |>
summarise(mean = mean(Value))
and refer to that in a geom_text call, using glue to join the text and value:
geom_text(data = df_mean, aes(x = Item, y = mean, label = glue("mean = {mean}"), hjust = 0), color = "black")
This preserves your labels and keeps your data frame the same. I added the geom_text call to the end of your plot script, and the mean calc just before your plot script.