ggplot2绘制带箭头图的条形图

本节介绍一下用ggplot2绘制带箭头的条形图,很简单的绘图案例,但是用到了很多的画图函数,希望对大家学习ggplot2绘图有所帮助

加载R包

library(tidyverse)
library(ggsci)

创建数据集

p <- tribble(
  ~start, ~end,~group,
  -15,-10,"A",
  -10,-5,"B",
  -5,0,"C",
  0,5,"D",
  5,10,"E",
  10,15,"G")

数据可视化

p1 <- ggplot(p) + 
  geom_segment(aes(x=15,xend=17,y=0,yend=0),
               arrow = arrow(length = unit(1.5,"cm"),
                             type="closed"),
               arrow.fill="#78B7C5",colour="white")+
  geom_segment(aes(x=-15,xend=-17,y=0,yend=0),
               arrow = arrow(length = unit(1.5,"cm"),
                             type="closed"),
               arrow.fill="#78B7C5",colour="white")+
  geom_rect(aes(xmin=start, xmax=end,ymin=-0.3,ymax=0.3,
                fill=group),colour="white")+
  geom_text(data=p,aes(x=start+2.5,y=0,label=group),
            fill="white",size=4,colour="white")+
  scale_fill_nejm()+ ylim(c(-1,1))+
  theme(legend.position="non",
        legend.key.size=unit(0.5,'cm'), 
        legend.key.height=unit(0.5,'cm'), 
        legend.key.width=unit(0.5,'cm'), 
        legend.title=element_blank(),
        axis.text.y=element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x=element_blank(),
        axis.ticks.y=element_blank(),
        panel.background = element_blank())+
  labs(x=NULL,y=NULL)+
  guides(fill = guide_legend(ncol=6))+
  annotate("text",x = 10, y =-0.4, label = "10",
           colour = "black",size = 4)+
  annotate("text",x = 15, y =-0.4, label = "15",
           colour = "black",size = 4)+
  annotate("text",x = 5, y =-0.4, label = "5",
           colour = "black",size = 4)+
  annotate("text",x = 0, y =-0.4, label = "0",
           colour = "black",size = 4)+
  annotate("text",x = -5, y =-0.4, label = "-5",
           colour = "black",size = 4)+
  annotate("text",x = -10, y =-0.4, label = "-10",
           colour = "black",size = 4)+
  annotate("text",x = -15, y =-0.4, label = "-15",
           colour = "black",size = 4)
 
ggsave(p1,file="gene.pdf",width =9.74,height=2.19,units="in",dpi=300)
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容