How to get dashed border lines around points in ggplot2?


I need to create an x-y plot where some observations are depicted as points and others are as lineranges (as in my dataset I have different observations of either a point estimate or just a range of the y-value with an upper and lower limit).
Importantly, I have two grouping variables that I would like to have mapped in the plot.
One grouping variable can be mapped through color-coding. But a second one is tricky, given the limited shape opportunities of a linerange.
So, my idea was to map the second grouping variable through the linetype of the geom_linerange (i.e. solid / long-dashed / short-dashed) and match the border lines of the geom_point objects to these linetypes.

I have tried to generate a reproducible example:

Data <- mtcars[1:6,] %>% 
  rename(xone = drat, xtwo = wt, y = mpg, lower = hp, upper = disp, groupone = cyl, grouptwo = carb) %>% 
  mutate(groupone = as.factor(groupone), grouptwo = as.factor(grouptwo), y = y*8)

ggplot(Data) +
  geom_point(aes(x = xone,
                 y = y,
                 shape = groupone,
                 fill = grouptwo,
                 size = 1.3,
                 stroke = 1.2)) +
  scale_shape_manual(values = 21:24) +
  geom_linerange(aes(ymin = lower, 
                     ymax = upper, 
                     x = xtwo, 
                     color = groupone, 
                     linetype = grouptwo, 
                     lwd = 1.1))

This gives me:

That means, in this concrete example I want the borders of my points to be either solid, long-dashed or short-dashed, mapped to the grouping factor "grouptwo".

After a long search, I did not come across any solutions for this problem. Any help is very much appreciated!

1 Like