Reputation: 37
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))
Upvotes: 0
Views: 1047
Reputation: 54287
[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))))
Upvotes: 1