In attempting to place a border around geom_point()
s in ggplot2
, this question was an extremely helpful starting point. I ended up opting for the approach outlined by @joran (the second answer) because it allows me to get smaller outlines (this is preferable since these points will end up being quite small in the final product).
However, I am having trouble employing a slight variation of this technique. I have two different shape varieties to outline and this is causing me some problems. Here is a reproducible example:
require('ggplot2')
values <- rnorm(n = 10, mean = 1, sd = 0.5) + c(1:10)
df <- data.frame(id = rep(c('hq', 'lq'), each = 5),
values = values,
period = rep(c(1:5), 2))
plot <-
ggplot(df, aes(x = period,
y = values,
group = id)) +
geom_line(color = 'gray40') +
geom_point(aes(shape = id,
color = id),
size = 3) +
geom_point(aes(shape = id),
color = 'gray70',
size = 3,
show_guide = FALSE) +
scale_color_manual(values = c('lightskyblue1', 'lightpink'),
labels = c('HQ', 'LQ')) +
scale_shape_manual(values = c(15, 16, 0, 1),
labels = c('HQ', 'LQ')) +
theme_bw()
This ends up giving a plot that looks like this:
I cannot understand why the points become solid gray (this is the color I'd like the border around the blue and pink points to be). Any help in retaining the blue and pink fill with a gray border matching the shape type would be greatly appreciated!
EDIT: In case it was not clear, I know that I can use pch 21-25
to achieve a border. However, these borders are too large and their thickness is not modifiable (as far as I know). As a result I am trying to layer two geom_points()
s, one filled and the other unfilled, to achieve the appearance of a border. I can sort of accomplish this by layering another set of geom_point()
s like this:
plot <-
ggplot(df, aes(x = period,
y = values,
group = id)) +
geom_line(color = 'gray40') +
geom_point(aes(shape = id,
color = id),
size = 3) +
geom_point(shape = 1,
color = 'gray70',
size = 3,
show_guide = FALSE) +
scale_color_manual(values = c('lightskyblue1', 'lightpink'),
labels = c('HQ', 'LQ')) +
scale_shape_manual(values = c(15, 16),
labels = c('HQ', 'LQ')) +
theme_bw()
The problem here is that each "border" does not match the corresponding shape, and the resulting image looks like this: