image.png今天的推文是昨天推文的延续。在昨天的推文中模仿了论文 Landscapes of bacterial and metabolic signatures and their interaction in major depressive disorders中的 figure2B
但是有一个细节没有能够实现,就是让坐标轴以上图样子的科学计数法显示,昨天的推文发出后有读者留言了对应的解决办法,今天在推文中记录一下
首先是构造一份数据
df<-data.frame(x=c("A","B","C","D"),
y=c(0.001,0.002,0.003,0.004))
df
最基本的柱形图
ggplot(df,aes(x=x,y=y))+
geom_col()
image.png
默认是以小数形式
加上如下函数
ggplot(df,aes(x=x,y=y))+
geom_col()+
scale_y_continuous(labels = scales::scientific)
image.png
能够修改成上图的科学计数法
另外的方式是
ggplot(df,aes(x=x,y=y))+
geom_col()+
scale_y_continuous(labels = c(expression(italic(0)),
expression(1%*%10^-10),
expression(2%*%10^-10),
expression(3%*%10^-10),
expression(4%*%10^-10)),
#position = "right",
expand = c(0,0),
breaks = c(0,0.001,0.002,0.003,0.004),
limits = c(0,0.005))
image.png
这个expression()函数还真好用,得花时间学习一下他的用法
接下来简单的美化一下
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=x))+
scale_y_continuous(labels = c(expression(italic(0)),
expression(1%*%10^-10),
expression(2%*%10^-10),
expression(3%*%10^-10),
expression(4%*%10^-10)),
position = "right",
expand = c(0,0),
breaks = c(0,0.001,0.002,0.003,0.004),
limits = c(0,0.005))+
labs(y=NULL)+
coord_flip()+
theme_bw()
image.png
昨天放到推文里的代码稍微有点错误,今天放一个完整的代码
library(ggplot2)
library(dplyr)
library(patchwork)
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)
df%>%
reshape2::melt()%>%
group_by(variable)%>%
summarise(mean_value=mean(value),
sd_value=sd(value)) -> df2
df%>%
reshape2::melt() -> df1
p1<-ggplot()+
geom_col(data=df2,aes(x=variable,y=mean_value),
fill="#8babd3",
color="black",
width = 0.2)+
geom_errorbar(data=df2,aes(x=variable,
ymin=mean_value-sd_value,
ymax=mean_value+sd_value),
width=0.1)+
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)
p1
p2<-ggplot()+
geom_col(data=df2,aes(x=variable,y=mean_value),
fill="#ffc080",
color="black",
width=0.2)+
geom_errorbar(data=df2,aes(x=variable,
ymin=mean_value-sd_value,
ymax=mean_value+sd_value),
width=0.1)+
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())
p2
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"))
p3<-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),
position = "right",
labels=c("f__Podoviridae",
"",
"f__Siphoviridae"))+
theme(panel.background = element_blank(),
axis.ticks = element_blank(),
axis.text.x = element_blank())+
scale_fill_manual(values = c("#c65911","#ffd965"))
p1+p2+p3+ggtitle("Bacteriophages")+
theme(plot.title = element_text(hjust=5))+
plot_layout(widths = c(1.2,1,0.2)) -> p
ggsave(filename = "Rplot11.pdf",
p,
width = 10,height = 3)
最终的结果如下
image.png再次感谢昨天推文读者的留言。
欢迎大家关注我的公众号
小明的数据分析笔记本
小明的数据分析笔记本 公众号 主要分享:1、R语言和python做数据分析和数据可视化的简单小例子;2、园艺植物相关转录组学、基因组学、群体遗传学文献阅读笔记;3、生物信息学入门学习资料及自己的学习笔记!
网友评论