When you have both a linetype and shape aesthetic (with geom_path and geom_point, respectively), it can sometimes be difficult to make out the points in the legend because they overlap with the line.
Here's a working example. I have a weighted linear model and want to plot predicted data along with the observed data.
iris$wts <- runif(nrow(iris), min = 0.2, max = 3)
fitw <- lm(Petal.Length ~ Petal.Width * Species, data = iris,
weights = wts)
newdat <- data.frame(
Petal.Width = rep(seq(from = min(iris$Petal.Width),
to = max(iris$Petal.Width), length.out = 100),
3),
Species = c(rep("setosa", 100), rep("versicolor", 100), rep("virginica", 100))
)
newdat$Petal.Length <- predict(fitw, newdata = newdat)
library(ggplot2)
ggplot(data = newdat, aes(y = Petal.Length, x = Petal.Width,
colour = Species)) +
geom_path(aes(linetype = Species), size = 1.1) +
geom_point(data = iris, aes(x = Petal.Width, y = Petal.Length, shape = Species,
size = wts), show.legend = TRUE) +
scale_shape_discrete(name = "Species") +
scale_size_identity(guide = "none")
Gives:
It's a little hard to tell how the symbols map to the factor levels. I could make the line less thick, but of course I might not want to do that. If I weren't mapping size to wts
, I could just use the size
argument in geom_point
. But since size is mapped to wts
, the legend's representation of the point shape seems to fall back to the default, regardless of whether the line size has been changed.
Is there any way to change the size of the point in the legend for the shape/color aesthetics?
override.aes
in some of your scales, although I'm having a hard time getting the point one size and the line another within the same legend. Or, if you want just the points to show up in the legend, you could useshow.legend = F
in thegeom_line
/geom_path
– camille