本系列課程要求大家有一定的R語(yǔ)言基礎(chǔ)祷肯,對(duì)于完全零基礎(chǔ)的同學(xué)沉填,建議去聽(tīng)一下師兄的《生信必備技巧之——R語(yǔ)言基礎(chǔ)教程》。本課程將從最基本的繪圖開(kāi)始講解佑笋,深入淺出的帶大家理解和運(yùn)用強(qiáng)大而靈活的ggplot2包翼闹。內(nèi)容包括如何利用ggplot2繪制散點(diǎn)圖、線圖蒋纬、柱狀圖猎荠、添加注解、修改坐標(biāo)軸和圖例等蜀备。
本次課程所用的配套書(shū)籍是:《R Graphic Cookbooks》
除了以上的基本圖形外关摇,師兄還會(huì)給大家講解箱線圖、提琴圖碾阁、熱圖输虱、火山圖、氣泡圖瓷蛙、傻课停基圖戈毒、PCA圖等各種常用的生信圖形的繪制,還不趕緊加入收藏夾横堡,跟著師兄慢慢學(xué)起來(lái)吧埋市!
R語(yǔ)言繪圖練習(xí)02 -- 克利夫蘭點(diǎn)圖:
###########
# 柱狀圖拓展:克里夫蘭點(diǎn)圖繪制:
library(gcookbook)
tophit <- tophitters2001[1:25, ] # 取前25行;
# 從最基本的散點(diǎn)圖出發(fā)
ggplot(tophit, aes(x=avg, y=name))+
geom_point()
# reorder排序一定要熟練:前面柱狀圖排序講過(guò):
ggplot(tophit, aes(x=avg, y=reorder(name, avg))) +
geom_point(size=3) + # 修改點(diǎn)的大小
theme_bw() + # 修改背景
theme(panel.grid.major.x=element_blank(), # 設(shè)置縱向網(wǎng)格線為空命贴;
panel.grid.minor.x =element_blank(),
# 設(shè)置橫向網(wǎng)格線為虛線(dashed)道宅;
panel.grid.major.y =element_line(colour="grey60", linetype="dashed"))
# 顛倒圖形的x軸和y軸;
ggplot(tophit, aes(x=reorder(name,avg), y=avg)) +
geom_point(size=3) +
theme_bw() +
theme(axis.text.x= element_text(angle=60, hjust=1),
panel.grid.major.y=element_blank(),
panel.grid.minor.y =element_blank(),
panel.grid.major.x =element_line(colour="grey60", linetype="dashed"))
# 按照l(shuí)g和avg對(duì)name進(jìn)行排序胸蛛,先按lg排污茵,再按avg排;
nameorder <- tophit$name[order(tophit$lg, tophit$avg)]
# 將name變量轉(zhuǎn)換為因子葬项,因子水平設(shè)定為nameorder泞当;
tophit$name <- factor(tophit$name, levels=nameorder)
## 繪制彩色克利夫蘭點(diǎn)圖:
ggplot(tophit, aes(x=avg, y=name))+
geom_segment(aes(yend=name), xend=0, colour="grey50")+
geom_point(size=3, aes(colour=lg))+ # 設(shè)置分組變量;
# limits限定顏色先后順序民珍;
scale_colour_brewer(palette="Set1", limits=c("NL", "AL"))+
theme_bw() +
theme(panel.grid.major.y =element_blank(), # 去除橫向網(wǎng)格線
legend.position=c(1, 0.55), # 設(shè)置圖例的位置:這里的1指的是與x軸的比例襟士;
# legend.justification=c(1, 0.5)表示圖例右邊緣中點(diǎn);
# (1嚷量,0)表示右下角陋桂,(0,1)表示左上角蝶溶,以此類推嗜历;
legend.justification=c(1, 0.5))
# 分面繪制:facet_grid()函數(shù):
ggplot(tophit, aes(x=avg, y=name))+
geom_segment(aes(yend=name), xend=0, colour="grey50")+
geom_point(size=3, aes(colour=lg))+
# guides去除圖例;
scale_colour_brewer(palette="Set1", limits=c("NL", "AL"),guide = F)+
theme_bw() +
theme(panel.grid.major.y = element_blank()) +
# 一列多行:lg~.抖所;行數(shù)等于lg的種類數(shù)目梨州;
# scales設(shè)置每個(gè)分塊的單位寬度;space設(shè)置每個(gè)分塊的寬度部蛇;
facet_grid(lg~., scales="free_y", space="free_y")
往期文章
- R語(yǔ)言繪圖(ggplot2摊唇、ggpubr)從入門(mén)到精通01--課程介紹
- R語(yǔ)言繪圖(ggplot2、ggpubr)從入門(mén)到精通02--柱狀圖和直方圖
- R語(yǔ)言繪圖(ggplot2涯鲁、ggpubr)從入門(mén)到精通03--箱式圖和函數(shù)圖像
- R語(yǔ)言繪圖(ggplot2巷查、ggpubr)從入門(mén)到精通04--柱狀圖美化之調(diào)色
- R語(yǔ)言繪圖(ggplot2、ggpubr)從入門(mén)到精通05--柱狀圖美化之分組修改
- R語(yǔ)言繪圖(ggplot2抹腿、ggpubr)從入門(mén)到精通06--柱狀圖美化之寬度調(diào)節(jié)
- R語(yǔ)言繪圖(ggplot2岛请、ggpubr)從入門(mén)到精通07--柱狀圖美化之如何加標(biāo)簽
- R語(yǔ)言繪圖練習(xí)01 -- 各種類型的餅圖