在ggplot2中更改用于scale_shape()的形状

时间:2009-09-25 17:15:01

标签: r ggplot2

假设我有以下

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

如何使用ggplot2更改使用的形状?

3 个答案:

答案 0 :(得分:40)

ggplot的方法是使用scale_shape_manual并在values参数中提供所需的形状:

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

result of above

形状与通常的0-25索引相同:http://yusung.blogspot.com/2008/11/plot-symbols-in-r.html

答案 1 :(得分:19)

为了补充Harlan的答案,这里有一个可用形状的参考 - 从左下角0开始向右阅读然后向上看(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

答案 2 :(得分:6)

> y <- rnorm(10)
> b <- as.factor(sample(1:4,10,replace=T))
> qplot(1:10, y, shape=b)
> qplot(1:10, y, pch=letters[1:10], cex=6)
这是什么意思?我想你可以使用任何R的绘图角色......

这可能不是一种非常“ggplot”的方式,但是手册页上写着“你可以使用它,就像你使用'plot'功能一样。” : - )

alt text