改变r中的ggbiplot中的点颜色和形状

时间:2015-05-06 22:20:14

标签: r ggplot2 pca ggbiplot

我正在使用ggbiplot(),并希望操纵数据点的颜色和形状,以使它们更适合打印机。目前我从ggbiplot()获得默认的彩虹色。我尝试使用参数“+ scale_colour_discrete”和“+ scale_shape_manual”,但“groups =”参数ggbiplot似乎覆盖了这些。如果我消除“groups =”参数,则无法绘制省略号。 “+ theme”参数工作得很好。我的代码如下。我知道我可以在常规的biplot()函数中更容易地操纵颜色/形状,但我喜欢ggbiplot()提供的置信区间省略号。

g <- ggbiplot(size.pca, choices=1:2, obs.scale = 1, var.scale = 1,
       groups=fieldnames, ellipse = TRUE,ellipse.prob=0.68, varname.size=4) 

g <- g + scale_colour_discrete(name=" ") #only the name=" " part of this seems to work.

g <- g + scale_shape_manual(values=c(17,16,16,16,16,16), name= " ") #trying to indicate one population different from the rest, but it doesn't seem to do anything (no error either, just no change in the output).

g <- g + theme_bw() +theme(legend.direction = 'horizontal',
       legend.position = 'top') 

print(g)

1 个答案:

答案 0 :(得分:0)

在ggplot脚本和geom_point()中添加scale_color_manual参数,以覆盖组默认颜色而不更改分组向量,如下所示:

g <- ggbiplot(size.pca, choices=1:2, obs.scale = 1, var.scale = 1,
       groups=fieldnames, ellipse = TRUE,ellipse.prob=0.68, varname.size=4) +
geom_point(aes(colour = fieldnames), size = "your size") +

scale_color_manual(values=c(17,16,16,16,16,16)) +

theme_bw() +theme(legend.direction = 'horizontal',
       legend.position = 'top') 

print(g)

这应该有效!