change point colors and shapes in ggbiplot in r

2019-09-17 20:31发布

I am using ggbiplot() and would like to manipulate the colors and shapes of the datapoints to make them more printer friendly. Currently I get the default rainbow of colors from ggbiplot(). I have tried using the arguments "+ scale_colour_discrete" and "+ scale_shape_manual" but the "groups=" argument ggbiplot seems to override these. If I eliminate the "groups=" argument then ellipses can't be drawn. The "+ theme" argument works just fine. My code is below. I know I could manipulate the colors/shapes more easily in the regular biplot() function, but I like the confidence interval ellipses provided by 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条回答
别忘想泡老子
2楼-- · 2019-09-17 21:25

add a geom_point() argument to your ggplot script and the scale_color_manual to override the group default colour without changing the grouping vector like this:

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)

This should work!

查看更多
登录 后发表回答