{"id":287,"date":"2022-01-24T10:48:27","date_gmt":"2022-01-24T02:48:27","guid":{"rendered":"https:\/\/linguopeng.top\/?p=287"},"modified":"2022-01-24T10:48:29","modified_gmt":"2022-01-24T02:48:29","slug":"ggplot2%e7%bb%98%e5%88%b6%e5%9f%ba%e5%9b%a0%e4%bd%9c%e7%94%a8%e5%85%83%e4%bb%b6%e5%9b%be","status":"publish","type":"post","link":"https:\/\/linguopeng.top\/?p=287","title":{"rendered":"ggplot2\u7ed8\u5236\u57fa\u56e0\u4f5c\u7528\u5143\u4ef6\u56fe"},"content":{"rendered":"\n<pre class=\"wp-block-code\"><code>package.list=c(\"tidyverse\",\"ggsci\",\"ggh4x\",\"aplot\",\"RColorBrewer\",\"ggpubr\",\"cowplot\",\"grid\",\"ggplotify\")\n\nfor (package in package.list) {\n  if (!require(package,character.only=T, quietly=T)) {\n    install.packages(package)\n    library(package, character.only=T)\n  }\n}\n\ncolor &lt;- c(\"white\",\"#709AE1FF\",\"#8A9197FF\",\"#D2AF81FF\",\"#FD7446FF\",\n           \"#D5E4A2FF\",\"#197EC0FF\",\"#F05C3BFF\",\"#46732EFF\",\n           \"#71D0F5FF\",\"#370335FF\",\"#075149FF\",\"#C80813FF\",\"#91331FFF\",\n           \"#1A9993FF\",\"#FD8CC1FF\")\n\ndata &lt;- read_tsv('data.txt',col_names=F) %>% \n  select(1,2,6) %>% \n  group_by(X1,X6) %>% \n  count(X2) %>% ungroup() %>% select(-2) %>% \n  pivot_wider(., names_from =X2,values_from = n) %>% \n  mutate_all(~replace(.,is.na(.), 0)) %>% \n  pivot_longer(-X1) %>% \n  mutate(value=as.character(value),signif=value,\n         signif=case_when(value==\"0\" ~ \" \",TRUE ~ as.character(value)),\n         name=case_when(name==\"Box II -like sequence\" ~ \n                          \"Box II -like sequence  \",\n                        TRUE ~ as.character(name)),\n         n=str_remove(X1,\"gene\") %>% as.numeric()) %>%\n  arrange(n)\n\ndf &lt;- data %>% select(name) %>% distinct() %>% \n  mutate(group= rep(LETTERS&#91;1:3],times=c(8,6,9)\n                  )) %>% left_join(.,data,by=\"name\")\n\ndf$X1 &lt;- factor(df$X1,levels=df$X1 %>% rev() %>% as.data.frame() %>% \n                  distinct() %>% pull())\n\n\n\np1 &lt;- df %>% ggplot(.,aes(interaction(name,group),X1,color=value,fill=value))+\n  geom_tile(color=\"grey80\",fill=\"white\",size=0.5)+\n  geom_point(pch=22,size=5)+\n  geom_text(aes(label=signif),size=3,color=\"black\")+\n  guides(x=\"axis_nested\")+\n  labs(x = NULL,y = NULL,color=NULL)+\n  scale_color_manual(values=color)+\n  scale_fill_manual(values=color)+\n  scale_x_discrete(expand=c(0,0)) +\n  scale_y_discrete(expand=c(0,0),position=\"left\")+\n  theme(axis.text.x=element_text(color=\"black\",angle=90,size=8,hjust=0),\n        axis.text.y=element_text(color=\"black\",size=8),\n        axis.ticks.x=element_blank(),\n        axis.ticks.y=element_blank(),\n        panel.border=element_rect(fill=NA,color=\"grey80\",size=1,linetype=\"solid\"),\n        ggh4x.axis.nestline.x = element_line(size = 1),\n        ggh4x.axis.nesttext.x = element_text(colour = \"blue\",angle =0),\n        legend.position = \"non\",\n        plot.margin=unit(c(0.2,0.2,0.2,0.2),units=,\"cm\"))+\n  scale_x_discrete(expand = c(0,0),position = 'top')\n\nmycolors &lt;- colorRampPalette(brewer.pal(8, \"Set1\"))(23)\n\np2 &lt;- df %>% select(name,X1,value) %>% mutate(value=as.numeric(value)) %>% \n  ggplot(aes(value,X1,fill=name))+\n  geom_col(position=\"stack\",width=0.5)+\n  labs(x=NULL,y=NULL)+\n  scale_fill_manual(values=mycolors)+\n  scale_x_continuous(expand = expansion(0),position = 'top')+\n  theme_test()+\n  theme(axis.text.x=element_text(color=\"black\",size=8,hjust=0),\n        axis.text.y=element_blank(),\n        axis.ticks.x=element_blank(),\n        axis.ticks.y=element_blank(),\n        panel.border=element_rect(fill=NA,color=\"grey70\",size=1,linetype=\"solid\"),\n        ggh4x.axis.nestline.x = element_line(size = 1),\n        legend.position = \"non\",\n        legend.key=element_blank(),   \n        legend.title = element_blank(),\n        legend.text = element_text(color=\"black\",size=5), \n        legend.spacing.x=unit(0.1,'cm'),\n        legend.spacing.y=unit(0.1,'cm'), \n        legend.key.width=unit(0.3,'cm'), \n        legend.key.height=unit(0.3,'cm'), \n        legend.background=element_blank(),\n        plot.margin=unit(c(0.2,0.2,0.2,0.2),units=,\"cm\"))+\n  guides(fill = guide_legend(direction = \"horizontal\"))+\n  guides(fill=guide_legend(nrow=6, byrow=TRUE)) \n\np3 &lt;- p2 + theme(legend.position = \"top\")\n\np1 %>% insert_right(p2,width=.6) %>% as.grob() %>% ggdraw()+\n  draw_plot(ggpubr::get_legend(p3) %>% as_ggplot(),scale=0.05,x=0.32,y=0.38)\n\nggsave(\"gene.pdf\",width=8.39,height=5.95,units=\"in\",dpi=300)<\/code><\/pre>\n\n\n\n<figure class=\"wp-block-image size-full\"><div class='fancybox-wrapper lazyload-container-unload' data-fancybox='post-images' href='https:\/\/linguopeng.top\/wp-content\/uploads\/2022\/01\/Rplot01-2.png'><img class=\"lazyload lazyload-style-1\" src=\"data:image\/svg+xml;base64,PCEtLUFyZ29uTG9hZGluZy0tPgo8c3ZnIHdpZHRoPSIxIiBoZWlnaHQ9IjEiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyIgc3Ryb2tlPSIjZmZmZmZmMDAiPjxnPjwvZz4KPC9zdmc+\"  loading=\"lazy\" decoding=\"async\" width=\"937\" height=\"746\" data-original=\"https:\/\/linguopeng.top\/wp-content\/uploads\/2022\/01\/Rplot01-2.png\" src=\"data:image\/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsQAAA7EAZUrDhsAAAANSURBVBhXYzh8+PB\/AAffA0nNPuCLAAAAAElFTkSuQmCC\" alt=\"\" class=\"wp-image-288\"\/><\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[3],"class_list":["post-287","post","type-post","status-publish","format-standard","hentry","category-uncategorized","tag-r"],"_links":{"self":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/287","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=287"}],"version-history":[{"count":1,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions"}],"predecessor-version":[{"id":289,"href":"https:\/\/linguopeng.top\/index.php?rest_route=\/wp\/v2\/posts\/287\/revisions\/289"}],"wp:attachment":[{"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=287"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=287"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linguopeng.top\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=287"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}