Points: mixing numbered and character shapes?

52 views
Skip to first unread message

winston

unread,
Sep 15, 2011, 7:10:46 PM9/15/11
to ggplot2
Is it possible to mix numbered point shapes with characters? Here's
some code that illustrates:

library(ggplot2)

x<-rnorm(20)
y<-rnorm(20)
type <- sample(LETTERS[1:3], size=20, replace=TRUE)
a <- data.frame(x,y,type)

# Plot with just numbered shapes
ggplot(data=a, aes(x=x, y=y)) +
geom_point(aes(shape=type)) +
scale_shape_manual(values=c(1,2,3))


What I would like is something like this, with numbered shapes and a
character shape. But this doesn't actually work -- the vector for the
shapes is converted to a character vector, so they all come out as
characters on the graph.
ggplot(data=a, aes(x=x, y=y)) +
geom_point(aes(shape=type)) +
scale_shape_manual(values=c(1,2,"W"))

Is this possible to do this with ggplot2?

Brandon Hurr

unread,
Sep 16, 2011, 3:06:42 AM9/16/11
to winston, ggplot2
If you split your data and plot it separately you can do exactly that. 

ab<-a[a$type== "A"| a$type== "B",]
c<-a[a$type== "C",]
ggplot()+
geom_point(data=ab, aes(x=x, y=y, shape=type))+
geom_text(data=c, aes(x=x, y=y, label="W"))

The tricky part is getting the legend... That's something we'll both need help with, since I'm pretty sure you can't mix types like that. 

B

Winston Chang

unread,
Sep 16, 2011, 7:59:00 PM9/16/11
to Brandon Hurr, ggplot2
Thanks, this works.  It's too bad the legend doesn't come out, though.

Kohske Takahashi

unread,
Sep 17, 2011, 1:45:28 AM9/17/11
to Winston Chang, Brandon Hurr, ggplot2
hi,

you can use ascii code with geom_points (internally, pointsGrob).
see ?points and try this:

library(ggplot2)
library(R.oo) # for ascii code lookup

x<-rnorm(20)
y<-rnorm(20)
type <- sample(LETTERS[1:3], size=20, replace=TRUE)
a <- data.frame(x,y,type)

# Plot with just numbered shapes


ggplot(data=a, aes(x=x, y=y)) +
geom_point(aes(shape=type)) +

scale_shape_manual(values=c(1,2,charToInt("W")))


--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

> --
> 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
>

Brandon Hurr

unread,
Sep 17, 2011, 3:57:51 AM9/17/11
to Kohske Takahashi, Winston Chang, ggplot2
That is excellent Takahashi. 

Just FYI, charToInt is in the R.oo package which requires the R.methodsS3 package
Reply all
Reply to author
Forward
0 new messages