I am trying to plot a bar chart where generic names of each group could be changed as desired by the user and plotted with a specific color in the defined order. I tried the following code but could not plot the chart as expected (i.e., veh = black, trtA = red, trtA = blue, trtAB = purple).
I need help with figuring out what am I missing? I checked the various posts a, b, c, d, etc., in this forum but, could not fix the issue.
library(ggplot2)
GroupA <- "Vehicle"
GroupB <- "Treatment-A"
GroupC <- "Treatment-B"
GroupD <- "Treatment-AB"
z <- data.frame(group = c("trtA", "trtAB", "trtB", "veh"),
Mean = c(42.990, 50.955, 34.235, 31.992),
sd = c(15.541, 18.325, 9.737, 12.463))
z$group[z$group == "veh"] <- GroupA
z$group[z$group == "trtA"] <- GroupB
z$group[z$group == "trtB"] <- GroupC
z$group[z$group == "trtAB"] <- GroupD
# group - color mapping
# veh = black, trtA = red, trtB = blue, trtAB = purple
colorVals = c("black", "red", "blue", "purple")
ggplot(z, aes(x=group, y=Mean, fill = group )) +
geom_bar(stat="identity", position=position_dodge()) +
geom_errorbar(aes(ymin=Mean-sd, ymax=Mean+sd), width=.4) +
geom_text(aes(label=round(Mean,2), fontface = "bold"), vjust = -0.5) +
labs(x = "Treatment Group", y = "Signal Value") +
theme_classic(base_size = 14) +
theme(legend.position='none') +
theme(axis.text.x = element_text(angle = 20, hjust = 1)) +
scale_fill_manual("legend", values = colorVals)