transparent points with pch=21

1,445 views
Skip to first unread message

Adrian

unread,
Jun 10, 2011, 11:47:43 AM6/10/11
to ggplot2
Hi All,
I want to control the fill and line color of the point using
geom_point, but also use the alpha argument to make the point
transparent. This will allow me to plot points that overlap and see
where ponts stack up. Transparency works with pch =16, but not with
pch=21. Here's an example

set.seed(1001)
z=data.frame(rnorm(50,10),rnorm(50,10))
colnames(z)=c("x","y")


#transparency works here
ggplot(z,aes(x=x,y=y))+
geom_point(aes(pch=16,alpha=.5),size=6,colour="blue")

#but not when i use pch=21
ggplot(z,aes(x=x,y=y))+
geom_point(aes(pch=21,alpha=.
5),size=6,fill="lightblue",colour="black")

Cheers,
Adrian

Ista Zahn

unread,
Jun 10, 2011, 12:12:28 PM6/10/11
to Adrian, ggplot2
Hi Adrian
The alpha() function exists for this purpose. Also, you don't need ot
put the alpha or shape settings inside aes, as you are setting them to
a fixed value. Finally, you should use the shape aesthetic rather than
pch. So the final thing looks like

ggplot(z,aes(x=x,y=y))+
geom_point(shape=21,alpha=.5, size=6,fill=alpha("lightblue", 0.5),
colour="black")

Best,
Ista

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

--
Ista Zahn
Graduate student
University of Rochester
Department of Clinical and Social Psychology
http://yourpsyche.org

Ben Bolker

unread,
Jun 10, 2011, 12:22:09 PM6/10/11
to ggp...@googlegroups.com
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Ista,

your style points are helpful, but I think this doesn't actually solve
Adrian's problem. Here's a slightly extended example.

cheers
Ben Bolker

set.seed(1001)
z <- data.frame(x=rnorm(50,10),y=rnorm(50,10),z=runif(50))

## this works fine, fill is set to a constant


ggplot(z,aes(x=x,y=y))+
geom_point(shape=21,alpha=.5, size=6,fill=alpha("lightblue", 0.5),
colour="black")

## this tries to map fill to a third variable, doesn't work
## -- no transparency
ggplot(z,aes(x=x,y=y,fill=z))+
geom_point(shape=21,alpha=.5, size=6, colour="black")

## this maps colour (for pch=16) to a third variable, works
## -- but no lines around the edges of the points as we would like.
ggplot(z,aes(x=x,y=y,colour=z))+
geom_point(shape=16,alpha=.5, size=6)

## another attempt to get around the problem by setting
## an explicit fill scale between two transparent colours
## fails.

ggplot(z,aes(x=x,y=y,fill=z))+
geom_point(shape=21,alpha=.5, size=6, colour="black")+
scale_fill_continuous(low=rgb(1,0,0,0.5),high=rgb(0,0,1,0.5))

-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.10 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAk3yRLEACgkQc5UpGjwzenN0KQCfZVllEkUVYrJ2mUfRBxO9xAzf
6W0AnjZZVDH5/E48EllxddYvWQZyok97
=OW6Z
-----END PGP SIGNATURE-----

Ista Zahn

unread,
Jun 10, 2011, 12:57:05 PM6/10/11
to Ben Bolker, ggp...@googlegroups.com
Ben, you're right, jumped to the example and didn't read the OP's
question carefully enough. The closest I could get to a solution to
the more difficult case where you want transparency for points whose
fill color is mapped to a variable is

ggplot(z,aes(x=x,y=y,fill=z))+
geom_point(shape=21, size=6, colour="black")+
scale_fill_manual(breaks = levels(z), values = c(alpha("red", 0.5),
alpha("blue", 0.5), alpha("green", 0.5), alpha("black", 0.5)))

This is not really nice because we have to convert to a factor first,
and is a bit verbose, but does produce the desired result.

Best,
Ista

--

Ista Zahn

unread,
Jun 10, 2011, 1:00:57 PM6/10/11
to Ben Bolker, ggp...@googlegroups.com
And I neglected to show that I'd converted to a factor... the working example is

z$z <- cut(z$z, 4)


ggplot(z,aes(x=x,y=y,fill=z))+
geom_point(shape=21, size=6, colour="black")+
scale_fill_manual(breaks = levels(z), values = c(alpha("red", 0.5),
alpha("blue", 0.5), alpha("green", 0.5), alpha("black", 0.5)))

-Ista

Reply all
Reply to author
Forward
0 new messages