ggplot2: different alpha values for border and filling of geom_point

北城以北 提交于 2019-12-04 06:15:49

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

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)

To achieve the desired plot I will use position_nudge function.

First I create a nudge vector with a uniform probability distribution of the same length as the data points. This can be done as below

set.seed(10)
nudgeWidth = 0.5
nudgeVec <- (runif(nrow(comp24))-0.5)*nudgeWidth

Now I use the above nudge vector "nudgeVec" to get the desired plot

plot1 <- ggplot(data=comp24, aes(x=spatial, y=lnfit, backgroundColor="white", na.rm=T))
plot1 <- plot1 + geom_point(position=position_nudge(x = nudgeVec),size=10.75,aes(alpha=0.2, stroke=0.5,shape=(as.integer(comp24$spatial)+14)))
plot1 <- plot1 + scale_colour_identity()
plot1 <- plot1 + scale_shape_identity()
plot1 <- plot1 + geom_point(position = position_nudge(x = nudgeVec),size=10.75,aes(alpha=1, stroke=0.5, colour="black",shape=(as.integer(spatial)-1)))
plot1

The output looks like this

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!