Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ggplot2: different alpha values for border and filling of geom_point

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

like image 929
Anne Avatar asked Sep 23 '16 12:09

Anne


People also ask

What does Alpha do in geom_point?

What does Alpha do in Geom_point? Alpha refers to the opacity of a geom. Values of alpha range from 0 to 1, with lower values corresponding to more transparent colors.

What does geom_point () do in R?

The function geom_point() adds a layer of points to your plot, which creates a scatterplot. ggplot2 comes with many geom functions that each add a different type of layer to a plot.

What does the Geom_jitter () function do?

Description. The jitter geom is a convenient shortcut for geom_point(position = "jitter") . It adds a small amount of random variation to the location of each point, and is a useful way of handling overplotting caused by discreteness in smaller datasets.

What is fill in ggplot2?

Generally, fill defines the colour with which a geom is filled, whereas colour defines the colour with which a geom is outlined (the shape's "stroke", to use Photoshop language).


2 Answers

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

like image 131
R_SOF Avatar answered Sep 19 '22 08:09

R_SOF


A simpler way, available here because you want the same alpha and color for all, is to specify the fill in the geom_point command using alpha. Alternatively one could also "old-fashioned" method of specifying transparency with two additional hex codes, like this, #00000044.

ggplot(data=comp24, aes(x=spatial, y=lnfit, shape=spatial)) +
   geom_point(position=position_jitter(width=0.5), size=1.75, 
              fill=alpha("black", 0.2), stroke=0.3)
like image 36
Aaron left Stack Overflow Avatar answered Sep 19 '22 08:09

Aaron left Stack Overflow