I have a dataframe where I have colored points from one location black and from another location white. In ggplot, the white points show properly; they have a black margin with a white fill. However, in the legend, the white point no longer has a black margin. How can I get the white point in the legend to have a black margin?
Example code:
library(ggplot2)
set.seed(321)
dat <- data.frame(matrix(ncol = 3, nrow = 6))
x <- c("Location","Month", "Value")
colnames(dat) <- x
dat$Location <- c("North","North","North","South","South","South")
dat$Month <- rep(c(1,2,3),2)
dat$Value <- rnorm(6,20,5)
cols <- c("South" = "black", "North" = "white")
ggplot(data = dat, aes(x = Month, y = Value, group = Location))
geom_point(aes(color = Location, fill = Location), size = 3)
geom_point(size = 3, shape = 21, color = "black")
scale_color_manual(values = cols)
theme_bw()
theme(panel.grid = element_blank(),
text = element_text(size = 16),
axis.text.x = element_text(size = 14, color = "black"),
axis.text.y = element_text(size = 14, color = "black"),
legend.title = element_blank(),
legend.text = element_text(size = 16, color = "black"),
legend.position = c(0.1,0.1),
legend.key = element_blank(),
legend.background = element_blank())
I could add another point onto the plot with something like geom_point(aes(x=1.1,y=16),size = 3.8, shape = 21, color = 'black')
but this is sloppy and does not play well when reformatting figure height or width.
CodePudding user response:
If you use one of the filled-in shapes like shape = 21
, you can separately control the border (with color
, black by default) and the fill (with fill
). So to get a combination of solid black points and white points with black margin, you can use one geom layer and should get the appropriate legend:
...
geom_point(aes(fill = Location), shape = 21, size = 3)
scale_fill_manual(values = cols)
...