
image.png
最近在看論文 Phased diploid genome assemblies and pan-genomes provide insights into the genetic history of apple domestication(高水平論文看起來還真是吃力?。┛炊稽c(diǎn)記一點(diǎn)吧。今天的筆記記錄的是論文中Figure2圖d中的堆積柱形圖的畫法,(本來今天的推文也計(jì)劃將右側(cè)的散點(diǎn)圖拼接到堆積柱形圖的右側(cè)的內(nèi)容也記一下的,但是實(shí)現(xiàn)的時(shí)候遇到一些問題,暫時(shí)還沒有重復(fù)出來,等到有辦法了再來記錄)

image.png
做堆積柱形圖參考 https://www.datanovia.com/en/blog/how-to-create-a-ggplot-stacked-bar-chart-2/
第一步準(zhǔn)備數(shù)據(jù)
數(shù)據(jù)總共三列,
需要示例數(shù)據(jù)可以直接在文末留言

image.png
讀入數(shù)據(jù)
df<-read.csv("NG_stacked_barplot_example.csv",
header = T)
head(df)
基本的堆積柱形圖
library(ggplot2)
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))

image.png
旋轉(zhuǎn)圖形
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))+
coord_flip()

image.png
更改圖例的位置到最底部
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))+
coord_flip()+
theme(legend.justification = c(0,0))

image.png
更改配色
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))+
coord_flip()+
theme(legend.justification = c(0,0))+
scale_fill_manual(values = c("#6cc396","#9896ca"))

image.png
更改坐標(biāo)軸的標(biāo)題
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))+
coord_flip()+
theme(legend.justification = c(0,0))+
scale_fill_manual(values = c("#6cc396","#9896ca"))+
labs(x=expression(paste(italic("M. domestica"),
" accession")),
y="Fraction of nuclear genome")

image.png
將y軸的標(biāo)題離文字標(biāo)簽遠(yuǎn)一點(diǎn)
ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))+
coord_flip()+
theme(legend.justification = c(0,0),
axis.title.y = element_text(vjust=5),
plot.margin = unit(c(0.1,0.1,0.1,0.5),"cm"))+
scale_fill_manual(values = c("#6cc396","#9896ca"))+
labs(x=expression(paste(italic("M. domestica"),
" accession")),
y="Fraction of nuclear genome")

image.png
簡單美化,去掉灰色背景,去掉網(wǎng)格線,更改一些圖例的字體
library(ggplot2)
library(Cairo)
p1<-ggplot(df,aes(x=x,y=y))+
geom_col(aes(fill=Origin))+
coord_flip()+
theme_bw()+
theme(legend.justification = c(0,0),
axis.title.y = element_text(vjust=5),
plot.margin = unit(c(0.1,0.1,0.1,0.5),"cm"),
panel.grid = element_blank(),
legend.text = element_text(family = "Comic Sans MS"))+
scale_fill_manual(values = c("#6cc396","#9896ca"))+
labs(x=expression(paste(italic("M. domestica"),
" accession")),
y="Fraction of nuclear genome")
ggsave("Rplot10.pdf",p1,device = cairo_pdf)

image.png
但是這里遇到一個(gè)問題是:如何將圖例的字體更改為斜體呢?暫時(shí)還沒有想到比較好的辦法
歡迎大家關(guān)注我的公眾號(hào)
小明的數(shù)據(jù)分析筆記本