I am relatively new to ggplot2 and was wondering if there was a simple way to change legend plots. I have added a dummy variable (AEmpty in the example below) to my dataframe that accounts for spacing and bar widths for uneven number of factors after a previous answer Control column widths in a ggplot2 graph with a series and inconsistent data. This seemed like the easiest way - although a comment or answer potentially addressing an alternative way is welcome!
So in the example below, I get a plot output that looks like this.
My questions are (1) Is there a way to select the grey scale from 'after', thus maximising color contrast between the bars displayed? (2) In the legend, how can I remove AEmpty? I saw a similar question here How do I manually change the key labels in a legend in ggplot2 but the answers here do not work because I have already applied a fill function elsewhere and scale_fill_discrete does not work.
Also if anyone would know of an easier way to scale the x-axis factors, thus eliminating the need for this 'dummy' variable, an answer would be greatly appreciated. Ultimately, the desired output is a barplot whereby each bar has equal width and each 'site' on the x-axis are equally spaced apart, with only relevant factors being displayed in the key.
library(ggplot2)
#Create data
when <- as.factor(c(rep(c("Before","After"),each = 3),
rep("During", 2),"AEmpty"))
site <- as.factor(c(rep(c("a","b","c"), 2),
"b","a","c"))
mean <- c(6,4.5,4.5,1.75,3.25,2.5,3,0,0)
std_error <- c(2.31,1.04,0.5,0.63,1.70,1.32,1.53,NA,NA)
df <- data.frame(when,site,mean,std_error)
#Plot
limits <- aes(ymax = mean + std_error, ymin=mean-std_error)
g <- ggplot(df, aes(site,mean,fill=when)) + geom_bar(stat = "identity", position = position_dodge()) + geom_errorbar(limits, width = 0.25, position = position_dodge(width = 0.9)) + scale_fill_grey()
g