I am having a problem with a ggplot code. I am trying to generate a coloured barplot somewhat similar to those generated in PRISM. All is working well, but as soon as I try to introduce colour with the fill argument in aes, running the add_pvalue() function returns the following error:
Error: ! Problem while computing aesthetics. i Error occurred in the 3rd layer. Caused by error in
FUN()
: ! object 'Group' not found Runrlang::last_trace()
to see where the error occurred.
The data is organised in two columns: Group (grouping variable) and Luminescence (continuous variable).
The code itself looks like this:
# 1 - Make data frame
df <- as.data.frame(Data3)
# 2 - Calculate standard deviations for each group
# 2.1 - Function to calculate mean and SD
data_summary <- function(data, varname, groupnames) {
require(plyr)
summary_func <- function(x, col) {
c(
mean = mean(x[[col]], na.rm = TRUE),
sd = sd(x[[col]], na.rm = TRUE)
)
}
data_sum <- ddply(data, groupnames,
.fun = summary_func,
varname
)
data_sum <- rename(data_sum, c("mean" = varname))
return(data_sum)
}
# 2.2 - Summarise data to get means and SDs
df3 <- data_summary(Data3,
varname = "Luminescence",
groupnames = c("Group")
)
# 3 - Create and call bar plot. Note sig levels: ns, * = p<0.05, ** = p<0.01, *** = p<0.001
df_p_val <- rstatix::t_test(Data3, Luminescence ~ Group, ref.group = "OSCC alone") %>%
rstatix::add_xy_position()
p <- ggplot(df3, aes(x = Group, y = Luminescence, fill = Group)) +
geom_bar(stat = "identity", position = position_dodge(), width = 0.5) +
geom_errorbar(aes(ymin = Luminescence - sd, ymax = Luminescence + sd),
width = 0.1, position = position_dodge(.9)
) +
theme_prism() +
theme(axis.text.x = element_text(size = 8), axis.text.y = element_text(size = 8), axis.title = element_text(size = 10)) +
scale_y_continuous(expand = c(0, 0), limits = function(x) {
c(0, ceiling(max(x) * 1.1))
}) +
scale_x_discrete(labels = function(x) str_wrap(x, width = 10)) +
add_pvalue(df_p_val, label = "p.adj.signif")
p
Note that if I run the code without fill=Group
, the barplot is generated but in black and white, while if I run the code as is without add_pvalue
, it does generate coloured bars.
Thanks for any help everyone!
Described in que question