0

I have a ggplot bar plot with grouped bars. There are 4 bars per x value, and the bar fill, color, and alpha are determined by variables in the data. I would like my legend to exactly reflect the appearance of the bars in the plot. Unfortunately I don't have enough reputation to post a picture of my plot, but here is my code:

ggplot(all_frame_freq, aes(x = Frame, y = prop_type, group = interaction(speaker,Type), fill = Type, color = Type, alpha = speaker)) +  
  geom_bar(position = "dodge", stat="identity") +
  scale_fill_manual(values = c("#E41A1C","#377EB8"), name="Type", labels=c("A","B")) +
  scale_color_manual(values = c("#E41A1C","#377EB8"), guide=FALSE) +
  scale_alpha_manual(values = c(.2, 1), name="speaker", labels=c("1", "2"))

This creates a line around all of the bars, but the line only shows around the bars where the alpha is <1. So the plot shows up as light bars with an outline and dark bars without an outline. (I found this easier to look at than simply light and dark bars.) I'd like the legend to reflect this, so that the legend box for speaker 1 appears with an outline like the corresponding bars. But currently the speaker legend just shows a pale grey box for 1 and dark grey box for 2, and I cannot figure out how to add a line around the light grey box only. Is there any way at all to do this? The closest I got was to add

theme(legend.key = element_rect(color = 'black', size = 0.5))

but that adds a line around all of the legend boxes. Is there a way to add it to just one box in the alpha legend, or (probably easier) to just the alpha legend, where I could set the line color to match the darker box? I've been trying various methods and searching around for ideas for quite awhile and haven't yet found the right solution.

A sample of my data - properly formatted this time:

structure(list(Type = structure(c(1L, 1L, 2L, 2L, 1L, 1L, 2L, 
2L), .Label = c("A", "B"), class = "factor"), Frame = structure(c(1L, 
2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("V CP", "V NP"), class = "factor"), 
prop_type = c(0.209513024, 0.138731597, 0.017167382, 0.387528402, 
0.437998087, 0.144086707, 0.042695836, 0.398376853), speaker = structure(c(1L, 
1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("Speaker1", "Speaker2"
), class = "factor")), .Names = c("Type", "Frame", "prop_type", 
"speaker"), class = "data.frame", row.names = c(NA, -8L))
Ed2122
  • 37
  • 1
  • 1
  • 7
  • 2
    You know, we don't have your data all_frame_freq so we cannot recreate your picture. Please show us the data by typing `dput(all_frame_freq)` and pasting the output into your question – G5W Jan 17 '17 at 18:43
  • My bad - just added. – Ed2122 Jan 17 '17 at 18:51
  • Please provide a reproducible example, the "added data" is not reproducible. For help with that please see [here](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – Cyrus Mohammadian Jan 17 '17 at 19:07
  • 1
    Sorry, I think I've added it correctly now. – Ed2122 Jan 17 '17 at 19:28

1 Answers1

1

[C]urrently the speaker legend just shows a pale grey box for 1 and dark grey box for 2, and I cannot figure out how to add a line around the light grey box only.

You could do

ggplot(all_frame_freq, aes(x = Frame, y = prop_type, group = interaction(speaker,Type), fill = Type, color = Type, alpha = speaker)) +  
  geom_bar(position = "dodge", stat="identity") +
  scale_fill_manual(values = c("#E41A1C","#377EB8"), name="Type", labels=c("A","B")) +
  scale_color_manual(values = c("#E41A1C","#377EB8"), guide=FALSE) +
  scale_alpha_manual(values = c(.2, 1), name="speaker", labels=c("1", "2")) + 
  guides(alpha = guide_legend(override.aes = list(color = c("red", NA))))

enter image description here

lukeA
  • 53,097
  • 5
  • 97
  • 100
  • This works exactly as I need it to if I set color to black instead of red. (Also, my plot doesn't have stacked bars, but that doesn't matter.) I had tried the override.aes at some point but couldn't figure out the right syntax. Thanks! – Ed2122 Jan 17 '17 at 21:14