温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

R语言怎么给坐标轴添加表示分组的线段

发布时间:2021-11-22 15:55:21 来源:亿速云 阅读:339 作者:iii 栏目:大数据

本篇内容主要讲解“R语言怎么给坐标轴添加表示分组的线段”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“R语言怎么给坐标轴添加表示分组的线段”吧!

R语言怎么给坐标轴添加表示分组的线段  
image.png
 首先是构造数据
df<-data.frame(A=1:3,B=1:3)
library(ggplot2)
ggplot(df,aes(x=A,y=B))+
  geom_col()+
  annotate(geom = "text",x=1,y=0,label="AAA",vjust=3)
 
R语言怎么给坐标轴添加表示分组的线段  
image.png

可以看到AAA这个文本大部分都被盖住了,可以看下加上coord_cartesian(clip = "off")函数的效果

df<-data.frame(A=1:3,B=1:3)
library(ggplot2)
ggplot(df,aes(x=A,y=B))+
  geom_col()+
  annotate(geom = "text",x=1,y=0,label="AAA",vjust=3)+
  coord_cartesian(clip = "off")
 
R语言怎么给坐标轴添加表示分组的线段  
image.png
 接下来实现文章开头的图

首先是气泡图

set.seed("20210329")
df<-data.frame(x=rep(letters[1:6]),
               y=c(rep("A",6),rep("B",6)),
               value=sample(1:100,12))
df
ggplot(data=df,aes(x=x,y=y))+
  geom_point(aes(size=value),
             color="red",
             alpha=0.5,
             show.legend = F)+
  scale_size_continuous(range = c(3,20))+
  theme_bw()
 
R语言怎么给坐标轴添加表示分组的线段  
image.png
ggplot(data=df,aes(x=x,y=y))+
  geom_point(aes(size=value),
             color="red",
             alpha=0.5,
             show.legend = F)+
  scale_size_continuous(range = c(3,20))+
  theme_bw()+
  geom_segment(x=1,xend=3,y=2.7,yend=2.7)+
  geom_segment(x=4,xend=6,y=2.7,yend=2.7)+
  geom_segment(x=1,xend=1,y=2.65,yend=2.7)+
  geom_segment(x=3,xend=3,y=2.65,yend=2.7)+
  geom_segment(x=4,xend=4,y=2.65,yend=2.7)+
  geom_segment(x=6,xend=6,y=2.65,yend=2.7)+
  annotate(geom="text",x=2,y=2,label="First",vjust=-16)+
  annotate(geom="text",x=5,y=2,label="Second",vjust=-16)+
  coord_cartesian(clip = "off")+
  theme(plot.margin = unit(c(2,0.2,0.2,0.2),'cm'))
 
R语言怎么给坐标轴添加表示分组的线段  
image.png

到此,相信大家对“R语言怎么给坐标轴添加表示分组的线段”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI