用R语言画柱形图怎么让屁股朝右

发布时间:2021-11-22 15:58:26 作者:iii
来源:亿速云 阅读:195

本篇内容主要讲解“用R语言画柱形图怎么让屁股朝右”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“用R语言画柱形图怎么让屁股朝右”吧!

用R语言画柱形图怎么让屁股朝右  
image.png

因为A图的数据较多,我们来模仿B图,过程其实是一样的

用R语言画柱形图怎么让屁股朝右  
image.png
 首先构造柱形图的数据集

最左边的屁股朝右的柱形图还是第一次画,查了一下可以借助 scale_y_reverse()函数实现

set.seed(1234)
x<-seq(5,10,by=0.5)
 
df<-data.frame(`s__Klebsiella_phage_vB_KpnP_SU552A` = sample(x,10,replace = T),
               `s__Escherichia_phage_ECBP5` = sample(x,10,replace = T),
               `s__Clostridium_phage_phi8074-B1` = sample(x,10,replace = T),
               check.names = F)
head(df)
 
用R语言画柱形图怎么让屁股朝右  
image.png
 宽格式转换为长格式
df%>%
  reshape2::melt() -> df1
   分组求均值和标准差
library(dplyr)
df%>%
  reshape2::melt()%>%
  group_by(variable)%>%
  summarise(mean_value=mean(value),
            sd_value=sd(value)) -> df2
   柱形图叠加误差线和散点图
ggplot()+
  geom_col(data=df2,aes(x=variable,y=mean_value),
           fill="#8babd3",
           color="black")+
  geom_errorbar(data=df2,aes(x=variable,
                             ymin=mean_value-sd_value,
                             ymax=mean_value+sd_value),
                width=0.2)+
  geom_jitter(data=df1,aes(x=variable,y=value),
              width = 0.2,color="grey")
 
用R语言画柱形图怎么让屁股朝右  
image.png
 接下来就是调整坐标轴,让屁股朝右
ggplot()+
  geom_col(data=df2,aes(x=variable,y=mean_value),
           fill="#8babd3",
           color="black")+
  geom_errorbar(data=df2,aes(x=variable,
                             ymin=mean_value-sd_value+0.001,
                             ymax=mean_value+sd_value),
                width=0.2)+
  geom_jitter(data=df1,aes(x=variable,y=value),
              width = 0.2,color="grey")+
  #scale_y_continuous(expand = c(0,0))+
  theme_bw()+
  coord_flip()+
  scale_y_reverse(expand=c(0,0),
                  position="right")+
  labs(x=NULL,y=NULL)
 
用R语言画柱形图怎么让屁股朝右  
image.png
 第二个柱形图也直接用这个数据画了
ggplot()+
  geom_col(data=df2,aes(x=variable,y=mean_value),
           fill="#ffc080",
           color="black")+
  geom_errorbar(data=df2,aes(x=variable,
                             ymin=mean_value-sd_value+0.001,
                             ymax=mean_value+sd_value),
                width=0.2)+
  geom_jitter(data=df1,aes(x=variable,y=value),
              width = 0.2,color="grey")+
  scale_y_continuous(expand = c(0,0),
                     position = "right")+
  theme_bw()+
  coord_flip()+
  labs(x=NULL,y=NULL)+
  theme(axis.text.y = element_blank(),
        axis.ticks.y = element_blank())
 
用R语言画柱形图怎么让屁股朝右  
image.png
 接下来是构造最右侧的热图数据
df3<-data.frame(x="A",
                y=c("s__Klebsiella_phage_vB_KpnP_SU552A",
                    "s__Escherichia_phage_ECBP5",
                    "s__Clostridium_phage_phi8074-B1"),
                group=c("f__Siphoviridae",
                        "f__Podoviridae",
                        "f__Podoviridae"))

ggplot(df3,aes(x=x,y=y))+
  geom_tile(aes(fill=group),show.legend = F)+
  labs(x=NULL,y=NULL)+
  scale_x_discrete(expand = c(0,0))+
  scale_y_discrete(expand = c(0,0))+
  theme(panel.background = element_blank(),
        axis.ticks = element_blank(),
        axis.text = element_blank())+
  scale_fill_manual(values = c("#c65911","#ffd965"))
 
用R语言画柱形图怎么让屁股朝右  
image.png
 最后是拼图
library(patchwork)
p1+p2+p3+ggtitle("Bacteriophages")+
  theme(plot.title = element_text(hjust=5))+
  plot_layout(widths = c(1.2,1,0.2)) -> p
ggsave(filename = "Rplot10.pdf",
       p,
       width = 10,height = 3)
 

最终的结果如下

用R语言画柱形图怎么让屁股朝右

到此,相信大家对“用R语言画柱形图怎么让屁股朝右”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

推荐阅读:
  1. R语言笔记 画多个图
  2. 用R语言怎么画小提琴图

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

r语言

上一篇:如何用R语言画堆积柱形图以及时间格式数据做坐标轴的操作

下一篇:c语言怎么实现含递归清场版扫雷游戏

相关阅读

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

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