R語言繪制數(shù)據(jù)可視化Dumbbell?plot啞鈴圖
又是一年春來到,小仙祝大家在新的一年開開心心、順順利利!
今天給大家分享的圖是啞鈴圖(Dumbbell plot)。
Step1. 繪圖數(shù)據(jù)的準(zhǔn)備
首先要把你想要繪圖的數(shù)據(jù)調(diào)整成R語言可以識別的格式,建議大家在excel中保存成csv格式。
作圖數(shù)據(jù)格式如下:
Step2. 繪圖數(shù)據(jù)的讀取
data <- read.csv(“your file path”, header = T, check.names=F) #注釋:header=T表示數(shù)據(jù)中的第一行是列名,如果沒有列名就用header=F #注釋:R讀取數(shù)據(jù)的時候,默認(rèn)會把列名里的空格變成 ".",check.names=F就不會變了
Step3. 繪圖所需package的安裝、調(diào)用
library(ggplot2) library(reshape2) # 注釋:package使用之前需要調(diào)用
Step4. 繪圖
data_melt <- melt(data,id.vars = "Gene") # 注釋:將原始的寬數(shù)據(jù)變成長數(shù)據(jù),方便畫圖 p <- ggplot(data_melt,aes(x = value, y = Gene)) + geom_line(aes(group = Gene)) + geom_point(aes(fill = variable), size = 3) p
注意改變點(diǎn)顏色的語句fill = variable沒有發(fā)揮作用,為什么呢?
還是跟geom_point()中的shape有關(guān)系,默認(rèn)是16號實(shí)心原點(diǎn),只有color參數(shù)
p <- ggplot(data_melt,aes(x= value, y= Gene)) + geom_line(aes(group = Gene)) + geom_point(aes(color = variable), size = 3) p
改變size的大小
p <- ggplot(data_melt,aes(x= value, y= Gene)) + geom_line(aes(group = Gene)) + geom_point(aes(color = variable, size = value)) p
調(diào)整順序
order <- c("Gene1","Gene2","Gene3","Gene4","Gene5","Gene6","Gene7","Gene8","Gene9","Gene10") p <- ggplot(data_melt,aes(x= value, y= Gene)) + geom_line(aes(group = Gene)) + geom_point(aes(fill=variable), shape = 21, size = 3) + scale_y_discrete(limits = order) p
Gene1放在y軸最上面
order <- rev(order) p <- ggplot(data_melt,aes(x= value, y= Gene)) + geom_line(aes(group = Gene)) + geom_point(aes(fill=variable), shape = 21, size = 3) + scale_y_discrete(limits = order) p
以上就是R語言繪制Dumbbell plot啞鈴圖的詳細(xì)內(nèi)容,更多關(guān)于R語言繪制啞鈴圖的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
R語言實(shí)現(xiàn)用cbind合并兩列數(shù)據(jù)
這篇文章主要介紹了R語言實(shí)現(xiàn)用cbind合并兩列數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-04-04