2023-10-09 | ggplot2繪圖

1.繪圖函數(shù)
geom_line        #折線圖
geom_point       #散點(diǎn)圖
geom_bar         #條形圖
geom_boxplot     #箱型圖
geom_jitter      #抖動點(diǎn)
geom_violin      #小提琴圖
geom_density     #密度圖
geom_histogram   #直方圖

散點(diǎn)圖

ggplot(data,aes(x,y,
               fill,color,group))+
      geom_point(size, stroke, alpha, shape)

# shape        形狀,1-25可選,20以上可設(shè)置填充和邊框
# size         點(diǎn)的大小,倍數(shù)
# stroke       調(diào)整輪廓粗細(xì)
# alpha        透明度,一般是填充色透明度

條形圖

ggplot(data,aes(x,y,
               fill,color,group))+
      geom_bar(stat='identity', position='stack', width)

#stat         默認(rèn)為count,一般指定indetity
#position     位置調(diào)整,stack為堆疊,dodge為并行擺放,fill為按比例堆疊
#width        條帶寬度

2.圖形調(diào)整參數(shù)

# 調(diào)整顏色、填充色和形狀
scale_fill_manual(values=)
scale_color_manual(values=)
scale_shape_manual(values=)
scale_color_gradient(low = "SpringGreen", high = "OrangeRed")    # 設(shè)置連續(xù)色,2種

library(RColorBrewer)
m_col = brewer.pal(9,"Set1")
scale_color_gradientn(colors=m_cols)                             # 設(shè)置連續(xù)色,N種

# 軸標(biāo)簽、圖例調(diào)整
scale_x_discrete(limits = unique(data$Source1))+                 # 固定X軸出現(xiàn)順序和讀入表格一致
scale_y_continuous(labels=percent)                               # y軸改為百分比形式
scale_x_continuous(expand=c(0.02,0),breaks = c(seq(0,29, by=2))) # 設(shè)置X軸標(biāo)簽為0,29間隔為2
theme(axis.text = element_text(face="bold", angle=45, hjust=1))  # 加粗、旋轉(zhuǎn)XY標(biāo)簽,可用axis.text.x單獨(dú)設(shè)置
labs(x="", y=" ")                                                # 設(shè)置XY軸標(biāo)簽

theme(lengend.position="none")                                   # 去除/修改圖例位置,top,right,left,bottom等可用
theme(lengend.position=c(0.5,0.5))                               # 調(diào)整圖例位置
guides(fill=guide_legend(title=NULL, byrow=F))                   # 去除圖例
guides(col=F)                                                    # 去除圖例
theme(legend.title=element_blank())                              # 去圖例標(biāo)題
theme(legend.title=element_title())                              # 修改圖例標(biāo)題

# 添加XY軸相應(yīng)位置虛線
geom_hline(yintercept = 0.4, linetype = "dashed", color = "black")
geom_vline(xintercept = 0.17, linetype = "dashed", color = "black")


# 主題調(diào)整
默認(rèn)主題
theme_classic() # 去除灰色背景和線,但只有左下邊框
theme_bw()      # 去除灰色背景,有所有邊框

library(ggthemes)
theme_few()     # 去除灰色背景和線,有全部邊框
theme_base()    # 去除灰色背景和線,有全部邊框,且放大XY軸標(biāo)簽
theme_minimal() # 去除灰色背景,只有網(wǎng)格線和圖形

坐標(biāo)軸截?cái)?/h4>
library(ggbreak)

scale_y_break(c(100000,250000), space=0.2, scales=1, expand=c(0,0)) # 設(shè)置Y軸(100000,250000)位置截?cái)?,scales調(diào)整截?cái)嗪蟊壤?scale_x_break(c(100000,250000), space=0.2, scales=1, expand=c(0,0)) # X軸同理

拼圖

library(ggpubr)
p <- ggarrange(ggarrange(p1, p2,ncol = 2, labels = c("a", "b"),widths = c(0.6,0.4)),
               ggarrange(p3, p4, p5,ncol = 3, labels = c("c", "d","e"),widths=c(0.35,0.35,0.3)), 
               nrow = 2, heights = c(0.4,0.6))
p

# ncol 該層分為幾格
# nrow 分為幾層
# heights 每層所占比例, 總和=1
# labels 每張圖的圖標(biāo)
# widths 每格占比,總和=1 
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容