首页 文章

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

提问于
浏览
26

假设我有以下内容

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

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

3 回答

  • 6

    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

  • 37

    为了补充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

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

相关问题