James White
James White

Reputation: 815

Changing legend properties in ggplot2

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. enter image description here

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 

Upvotes: 0

Views: 519

Answers (1)

Thomas K
Thomas K

Reputation: 3311

I'm not entirely sure what you mean by: "I have already applied a fill function elsewhere". Since fill can appear just once per graph, you could incorporate breaks and labels into your "other fill function", which you are not displaying here.

The code below does what you want for the example you provided:

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)

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_manual(values = c("AEmpty" = "grey1",
                               "After" = "grey15",
                               "Before" = "grey55",
                               "During" = "grey75"),
                    breaks = c("After", "Before", "During"),
                    labels = c("After", "Before", "During")) 

To choose shades of grey, consult this list of R-colours.

Upvotes: 1

Related Questions