Changing shapes used for scale_shape() in ggplot2


Question

Suppose I have the following

y <- rnorm(10)
b <- as.factor(sample(1:4,10,replace=T))
qplot(1:10, y, shape=b)

How do I change the shapes that are used using ggplot2?

1
30
9/28/2009 6:38:36 AM

Accepted Answer

The ggplot way to do it would be to use scale_shape_manual and provide the desired shapes in the values argument:

qplot(1:10, y, shape=b) + scale_shape_manual(values = c(0, 5, 6, 15))

result of above

The shapes are the same as the usual 0-25 indexes: http://yusung.blogspot.com/2008/11/plot-symbols-in-r.html

42
6/9/2015 2:52:09 PM

To complement Harlan's answer, here is a references for the available shapes - start from 0 at bottom left and read right then up (10y + x):

df <- data.frame(x=c(0:129))
ggplot(df, aes(x=x%%10, y=floor(x/10), shape=factor(x), colour=x, size=10)) +
  geom_point() +
  scale_shape_manual(values=df$x) + theme(legend.position='none') +
  scale_x_continuous(breaks=0:10) + scale_y_continuous(breaks=0:12) +
  scale_colour_hue() + scale_colour_gradientn(colours=rainbow(3))

Shapes available in ggplot2


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon