在用ggplot2作点图时,点的颜色默认是由color参数控制的。如下所示:
library(ggplot2)
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point(aes(color=Species),size=4)
但偶尔想要让点的颜色由fill参数控制的时候就需要设置点的shape,所有shape如下所示:
这里shape21~shaoe25的中间颜色是由fill参数控制,描边颜色由color参数控制。但是有些扭曲的人(比如我)会想到一些扭曲的要求——使用fill控制点的颜色然而需要去除边框。以前做的时候只能想到把边框调到最细(stroke=0),但是效果并不理想,如下图所示:
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point(aes(fill=Species),shape=21,size=4,stroke=0)
可以看到多少还是可以看到黑框,之前还尝试过设置不透明度来去除边框,如下所示:
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point(aes(fill=Species),shape=21,size=4,stroke=0,alpha=0)
这下连点都没了。。。直到今天突发奇想——有没有一种颜色自带透明度?简单检索之后发现rgb()函数除了红绿蓝三个色道参数以外还有第四个不透明度参数。用ggplot试一下能不能去除点的描边。
p1 <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point(aes(fill=Species),color=rgb(0,0,0),shape=21,size=4)
p2 <- ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+
geom_point(aes(fill=Species),color=rgb(0,0,0,0),shape=21,size=4)
p1
p2
![P1](https://img-blog.csdnimg.cn/14858d7c9eb74022ba0db6282a6869c9.png)
![](https://img-blog.csdnimg.cn/19872fd520374a2bac2d8d67a1c3ff36.png)
可以发现P1依然存在边框,而P2的边框已经消失不见了。大功告成!
————————————
时隔一个月更新一下,我发现其实只要写color=NA 就可以了,不需要用到rgb(0,0,0,0)。
参考: