rggplot2plotfillpoints

ggplot2: lines + points with white fill in plot and legend?


I want to create a plot with the ggplot2 package, which combines lines and points. The points should have colors and shapes according to a group indicator. A legend should be created, which displays colors and shapes according to the plot.

This part worked fine. However, all points should have a white fill and I cannot find the right code for that.

A google search suggests to use fill = "white", but this is not working.

Consider the following example data and plot:

library("ggplot2")

# Example data
df <- data.frame(y = 1:100,
                 x = 1:100,
                 group = as.factor(c(rep(1, 33), rep(2, 33), rep(3, 34))))

# Create plot --> fill = "white" doesnt work
ggplot(df, aes(x = x, y = y)) + 
  geom_line(aes(colour = factor(group, labels = c("a", "b", "c")))) +
  geom_point(aes(colour = factor(group, labels = c("a", "b", "c")),
                 shape = factor(group, labels = c("a", "b", "c"))),
             fill = "white") +              ##### This line is not working #####
  theme(legend.title = element_blank())

enter image description here

Question: How could I fill the points of this plot with white (both in the plot and the legend)?


Solution

  • You can use scale_shape_discrete to set solid = FALSE:

    ggplot(df, aes(x = x, y = y)) + 
      geom_line(aes(colour = factor(group, labels = c("a", "b", "c")))) +
      scale_shape_discrete(solid = F) +
      geom_point(aes(colour = factor(group, labels = c("a", "b", "c")),
                     shape = factor(group, labels = c("a", "b", "c")))) +              
    theme(legend.title = element_blank())
    

    enter image description here