这期内容当中小编将会给大家带来有关如何使用R语言ggplot2进行图例去掉灰色背景、添加椭圆和圆形分组边界,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
示例数据就直接用内置的鸢尾花的数据集了
library(ggplot2)colnames(iris)ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(size=Petal.Length,color=Species))+ guides(color=F)+ scale_size_continuous(range = c(5,10), breaks = c(2,4,6))
我开始想复杂了,以为需要去图例相关的参数里进行设置,原来直接更改点的形状就好了,给shape参数设置成21就好了
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(size=Petal.Length,color=Species), shape=21)+ guides(color=F)+ scale_size_continuous(range = c(5,10), breaks = c(2,4,6))
这样的话图上的点也都变成空心的了,如果想把图上的点设置成实心的,就再增加一个fill参数就好了
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(size=Petal.Length, color=Species, fill=Species), shape=21)+ guides(color=F,fill=F)+ scale_size_continuous(range = c(5,10), breaks = c(2,4,6))
这里还可以看到图例是带灰色背景的,如果想要去掉怎么办呢?答案是在主题里设置
legend.key
参数
ggplot(iris,aes(x=Sepal.Length,y=Sepal.Width))+ geom_point(aes(size=Petal.Length, color=Species, fill=Species), shape=21)+ guides(color=F,fill=F)+ scale_size_continuous(range = c(5,10), breaks = c(2,4,6))+ theme(legend.key = element_blank())
这里的key对应的中文意思是什么呢?
用到的是stat_ellipse()
函数
ggplot(data=iris,aes(x=Sepal.Length, y=Sepal.Width, color=Species))+ geom_point()+ theme(legend.key = element_blank())+ stat_ellipse(aes(x=Sepal.Length, y=Sepal.Width, color=Species, fill=Species), geom = "polygon", alpha=0.5)
用到的是ggforce
这个包里的geom_circle()
函数
library(ggplot2)library(ggforce)colnames(iris)ggplot()+ geom_point(data=iris,aes(x=Sepal.Length, y=Sepal.Width, color=Species))+ theme(legend.key = element_blank(), panel.background = element_blank(), panel.border = element_rect(color="black", fill = "transparent"))+ geom_circle(aes(x0=5,y0=3.5,r=1), fill="blue", alpha=0.2, color="red")+ xlim(2,8)+ ylim(2,8)+ geom_circle(aes(x0=7,y0=3,r=1), fill="green", alpha=0.2, color="red")
上述就是小编为大家分享的如何使用R语言ggplot2进行图例去掉灰色背景、添加椭圆和圆形分组边界了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注亿速云行业资讯频道。
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。
原文链接:https://my.oschina.net/u/4579431/blog/4943565