ggplot2: different alpha values for border and filling of geom_point

Anne picture Anne · Sep 23, 2016 · Viewed 7.4k times · Source

I want to plot my data as a dotplot using geom_point. My datapoints are overlapping, so I want to use jitter and transparency to increase visibility. Now I would also like to add a border to every datapoint to make it even more easier for the reader to see each datapoint. However, due to the alpha, it looks like my datapoints have halos around them. That's why I would like to use alpha only for the filling and alpha=0 for the border. But so far I haven't found a solution. I could plot two geom_points one being slightly larger than the other to create a border around each point with alpha=0. But becuase I need jitter, the same datapoints won't lie on top of each other. Does anyone has an idea how to solve this problem?

Here is my code:

ggplot(data=comp24, aes(x=spatial, y=lnfit, colour=spatial, fill=spatial, shape=spatial, backgroundColor="white", na.rm=T))+
geom_point(position=position_jitter(w=0.5), size=1.75, alpha=0.2, stroke=0.3)+
scale_colour_manual(name="spatial structure", values = c("black", "black", "black"))+
scale_fill_manual(name="spatial structure", values = c("black","black", "black"))

And some data:

spatial focal competitor       lnfit
low   pco        pch -1.79175947
low   pco        pch -1.49165488
low   pco        pch -0.98082925
low   pco        pch -1.97716269
intermediate   pco        pch -0.84729786
intermediate   pco        pch -0.48379695
intermediate   pco        pch -0.64574494
intermediate   pco        pch -0.51082562
intermediate   pco        pch  1.43693809
high   pco        pch  0.89608802
high   pco        pch  0.04879016
high   pco        pch -2.20625398
high   pco        pch  0.31003898
high   pco        pch -0.01694956

Here is a detail of the graph, that shows the halo I am talking about. I guess it comes from filling and border overlapping a bit. That's why I see this draker line within the grey area. Changing the stroke value unfortunately only increases the halo effect.

I save my graphs as .tif with:

tiff('C:/_..._..._.tif', bg = "white", res = 1600, width = 115, height = 160, units = "mm", compression="lzw")

Looking forward to your suggestions.

Cheers Anne

Answer

R_SOF picture R_SOF · Sep 23, 2016

This should works for you :

ggplot(data=comp24, aes(x=spatial, y=lnfit, colour=spatial, fill=spatial, shape=spatial, backgroundColor="white", na.rm=T))+
geom_point(position=position_jitter(w=0.5), size=1.75, stroke=0.3)+
scale_colour_manual(name="spatial structure", values = c("black", "black", "black"))+
scale_fill_manual(name="spatial structure", values =alpha(c("black","black", "black"),0.2))

Let me know