日本免费高清视频-国产福利视频导航-黄色在线播放国产-天天操天天操天天操天天操|www.shdianci.com

學無先后,達者為師

網站首頁 編程語言 正文

R語言繪制數據可視化Dumbbell?plot啞鈴圖_R語言

作者:黃小仙 ? 更新時間: 2022-04-21 編程語言

又是一年春來到,小仙祝大家在新的一年開開心心、順順利利!
今天給大家分享的圖是啞鈴圖(Dumbbell plot)。

Step1. 繪圖數據的準備

首先要把你想要繪圖的數據調整成R語言可以識別的格式,建議大家在excel中保存成csv格式。
作圖數據格式如下:

Step2. 繪圖數據的讀取

data <- read.csv(“your file path”, header = T, check.names=F)
#注釋:header=T表示數據中的第一行是列名,如果沒有列名就用header=F
#注釋:R讀取數據的時候,默認會把列名里的空格變成 ".",check.names=F就不會變了

Step3. 繪圖所需package的安裝、調用

library(ggplot2) 
library(reshape2)              
# 注釋:package使用之前需要調用

Step4. 繪圖

data_melt <- melt(data,id.vars = "Gene")
# 注釋:將原始的寬數據變成長數據,方便畫圖
p <- ggplot(data_melt,aes(x = value, y = Gene)) +  
       geom_line(aes(group = Gene)) +
       geom_point(aes(fill = variable), size = 3)
p

注意改變點顏色的語句fill = variable沒有發揮作用,為什么呢?

還是跟geom_point()中的shape有關系,默認是16號實心原點,只有color參數

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

調整順序

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

原文鏈接:https://blog.csdn.net/biocity/article/details/113810545

欄目分類
最近更新