Reputation: 257
I have the following df:
df <- data.frame("id" = c("A1", "A1", "A1", "A2", "A2", "A2", "B1", "B1", "B1", "B2", "B2", "B2"),
"group" = c("A", "A", "A", "A", "A", "A", "B", "B", "B", "B", "B", "B"),
"version" = c("1", "1", "1", "2", "2", "2", "1", "1", "1", "2", "2", "2"),
"var_1" = 1:3,
"var_2" = 1:12)
I use the following code::
library(ggplot2)
ggplot(data = df, aes(x = var_1, y = var_2, by = "id")) +
stat_summary(fun = "mean", geom = "line", aes(color = factor(id), linetype = (version))) +
stat_summary(fun = "mean", geom = "point", aes(color = factor(id), shape = (group))) +
labs(color = "id", linetype = "version", shape = "group")
And get the following plot:
The problem is that in the 'id' legend, the shapes and linetypes are generic, and don't always match the correct shape/linetype.
What I would like to do is make the legend for 'id' show the correct shape and linetype, while preserving the separate, original legends for 'group' and 'version'.
This means that my goal is for the 'group' and 'version' legends to remain the same, but for the entries in the 'id' legend to have their shape and linetype modified as necessary.
Upvotes: 0
Views: 176
Reputation: 13843
You can do this through guide_legend
. Within that, you can override the default aes()
specified via the other ggplot2
commands to match what you want:
p + guides(color=guide_legend(
override.aes = list(linetype=c(1,3,1,3), shape=c(16,16,17,17))))
Upvotes: 1