前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >R语言ggplot2杂记:图例去掉灰色背景、添加椭圆和圆形分组边界

R语言ggplot2杂记:图例去掉灰色背景、添加椭圆和圆形分组边界

作者头像
科研菌
发布2021-02-12 17:58:49
3K0
发布2021-02-12 17:58:49
举报
文章被收录于专栏:科研菌
常规气泡图的图例

示例数据就直接用内置的鸢尾花的数据集了

代码语言:javascript
复制
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))

image.png

image.png

那如何变成如上这种空心的圆呢?

我开始想复杂了,以为需要去图例相关的参数里进行设置,原来直接更改点的形状就好了,给shape参数设置成21就好了

代码语言:javascript
复制
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))

image.png

这样的话图上的点也都变成空心的了,如果想把图上的点设置成实心的,就再增加一个fill参数就好了

代码语言:javascript
复制
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))

image.png

这里还可以看到图例是带灰色背景的,如果想要去掉怎么办呢?答案是在主题里设置legend.key参数

代码语言:javascript
复制
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())

image.png

这里的key对应的中文意思是什么呢?

image.png

添加椭圆的分组边界

用到的是stat_ellipse()函数

代码语言:javascript
复制
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)

image.png

添加圆形的分组边界

用到的是ggforce这个包里的geom_circle()函数

代码语言:javascript
复制

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")
本文参与 腾讯云自媒体同步曝光计划,分享自微信公众号。
原始发表:2021-01-30,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 科研菌 微信公众号,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 常规气泡图的图例
  • 那如何变成如上这种空心的圆呢?
  • 添加椭圆的分组边界
  • 添加圆形的分组边界
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档