I'm trying to do a plot that has most data points drawn normally, but one set of data points with a different sized symbol. I want the legend to show the same: most points shown normally, but the exception drawn with a different sized symbol. Here is a short bit of code:
library(ggplot2)
x = c(1,2,1,2,3)
y = c(1,2,3,4,3)
vendor = c("x", "x", "y", "y", "z")
df = data.frame(x,y,vendor)
p <- ggplot(df) +
aes_string(x="x", y="y", color="vendor") +
geom_point(size=3, data=subset(df, vendor!="z")) +
geom_point(size=5, data=subset(df, vendor=="z"))
ggsave("foo.pdf")
The problem is that in the resulting legend, all points are drawn with the larger (size=5
) symbol, not just those with vendor z. I want vendor z drawn with the larger point in the legend, and the others drawn with size=3
.
(Bonus question: What I really want is a larger thick outlined symbol: instead of a circle, I want a donut. I realize that shape=2
will draw an outlined circle, but it is very thin. I'd rather have a thicker outlined circle. I want to do the same with a triangle. Any easy way to do this?)
Maybe I applied it wrong, but following this advice:
ggplot2: Making changes to symbols in the legend
with the addition of the "guides" line did not help:
guides(size = guide_legend(override.aes = list(shape = 1)))
i.e. same output, with size=5
symbols for all three vendors in the legend.
EDITED: Fantastic answer, which I quickly implemented. Now I've added lines:
library(ggplot2)
x = c(1,2,1,2,3)
y = c(1,2,3,4,3)
vendor = c("x", "x", "y", "y", "z")
df = data.frame(x,y,vendor)
df$vendor_z <- df$vendor=="z" # create a new column
ggplot(df) +
aes_string(x = "x", y = "y", color = "vendor", size = "vendor_z") +
geom_point() +
geom_line(size=1.5) + # this is the only difference
scale_size_manual(values = c(3, 5), guide = FALSE)
guides(colour = guide_legend(override.aes = list(size = c(3, 3, 5))))
ggsave("foo.pdf")
and now the size of the legend is back down to 3 again for all dots, including the ones with vendor z. Any ideas on how to fix this?