R语言画森林图方法4
生活随笔
收集整理的這篇文章主要介紹了
R语言画森林图方法4
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
獲取更多R語言知識,請關注公眾號:醫學和生信筆記
醫學和生信筆記,專注R語言在臨床醫學中的使用,R語言數據分析和可視化。主要分享R語言做醫學統計學、meta分析、網絡藥理學、臨床預測模型、機器學習、生物信息學等。
文章目錄
- ggplot2
- ggforestplot
今天繼續學習使用R語言畫森林圖!
前面學習了2種通用方法,1種可視化模型的方法,今天學習使用ggplot2和ggforestplot畫森林圖!
不過我不喜歡,我還是最喜歡前兩種通用的方法,美觀,通用!
數就是圖,圖就是數!
ggplot2
先編造一個數據。
library(tibble) options(digits = 2) df <- tibble(label = LETTERS[1:22],mean = rnorm(22,mean = 1, sd=0.2),lower = mean - 0.1,upper = mean + 0.2,group = c(rep("Group-1",7),rep("Group-2",7),rep("Group-3",8)) ) df ## # A tibble: 22 x 5 ## label mean lower upper group ## <chr> <dbl> <dbl> <dbl> <chr> ## 1 A 0.917 0.817 1.12 Group-1 ## 2 B 0.880 0.780 1.08 Group-1 ## 3 C 1.36 1.26 1.56 Group-1 ## 4 D 1.03 0.926 1.23 Group-1 ## 5 E 0.965 0.865 1.17 Group-1 ## 6 F 0.697 0.597 0.897 Group-1 ## 7 G 0.718 0.618 0.918 Group-1 ## 8 H 0.804 0.704 1.00 Group-2 ## 9 I 1.08 0.977 1.28 Group-2 ## 10 J 1.21 1.11 1.41 Group-2 ## # ... with 12 more rows加載R包
library(ggplot2)畫圖!
p <- ggplot(data = df)+geom_point(aes(x=mean,y=label),size = 2)+geom_errorbar(aes(x = mean,y=label,xmin=lower,xmax=upper))+geom_vline(xintercept = 1, color = "black",linetype="dashed",alpha=0.6)+labs(x=NULL,y=NULL)+facet_grid(group ~.,scales = "free",space = "free")+theme_minimal()+theme(text=element_text(size=18, color="black"))+theme(panel.spacing = unit(1, "lines")) p這就是一個簡單的森林圖了,你可以添加各種映射改變顏色和大小形狀等。對于森林圖中的文字部分可以通過geom_text添加。
不過確實不太好看的樣子!
# 保存 ggsave(filename = "ggplot_forestplot.png",height = 26,width = 18,units = "cm")ggforestplot
這個包是基于ggplot2系列的,看似優雅,但是顏值畫出來總感覺顏值不高。
# 目前只能通過github安裝 devtools::install_github("NightingaleHealth/ggforestplot") library(ggforestplot) library(tidyverse) ## -- Attaching packages ----------------------------- tidyverse 1.3.1 -- ## v tidyr 1.2.0 v dplyr 1.0.7 ## v readr 2.1.1 v stringr 1.4.0 ## v purrr 0.3.4 v forcats 0.5.1 ## -- Conflicts -------------------------------- tidyverse_conflicts() -- ## x dplyr::filter() masks stats::filter() ## x dplyr::lag() masks stats::lag()# 篩選部分數據 df <-ggforestplot::df_linear_associations %>%filter(trait == "BMI",dplyr::row_number() <= 30)df ## # A tibble: 30 x 5 ## name trait beta se pvalue ## <chr> <chr> <dbl> <dbl> <dbl> ## 1 Isoleucine BMI 0.339 0.00945 1.11e-281 ## 2 Leucine BMI 0.343 0.00951 1.25e-285 ## 3 Valine BMI 0.287 0.00951 7.94e-200 ## 4 Phenylalanine BMI 0.343 0.00862 0 ## 5 Tyrosine BMI 0.261 0.00900 6.65e-185 ## 6 Alanine BMI 0.179 0.00890 8.62e- 90 ## 7 Glutamine BMI -0.134 0.00945 7.68e- 46 ## 8 Glycine BMI -0.0296 0.00937 1.56e- 3 ## 9 Histidine BMI 0.0364 0.00917 7.25e- 5 ## 10 Lactate BMI 0.131 0.00911 9.20e- 47 ## # ... with 20 more rows基本畫圖,只需要幾個參數即可:
ggforestplot::forestplot(df = df,name = name,estimate = beta,se = se,pvalue = pvalue,psignif = 0.002, # 顯著性閾值xlab = "1-SD increment in BMI\nper 1-SD increment in biomarker concentration",title = "Associations of blood biomarkers to BMI" )下面是一個多組的。
# 數據準備 selected_bmrs <- df %>% pull(name)df_compare_traits <-ggforestplot::df_linear_associations %>%filter(name %in% selected_bmrs) %>%# Set class to factor to set order of display.mutate(trait = factor(trait,levels = c("BMI", "HOMA-IR", "Fasting glucose")))畫圖:
# 畫圖ggforestplot::forestplot( df = df_compare_traits, estimate = beta, pvalue = pvalue, psignif = 0.002, xlab = "1-SD increment in cardiometabolic trait\nper 1-SD increment in biomarker concentration", title = "Biomarker associations to metabolic traits", colour = trait)森林圖一共介紹了4種,還有一種生存分析的森林圖沒用,因為太簡單了!直接ggforest(model)就解決了,而且如果你能提取出數據,用前兩種方法完全可以搞定。
獲取更多R語言知識,請關注公眾號:醫學和生信筆記
醫學和生信筆記,專注R語言在臨床醫學中的使用,R語言數據分析和可視化。主要分享R語言做醫學統計學、meta分析、網絡藥理學、臨床預測模型、機器學習、生物信息學等。
總結
以上是生活随笔為你收集整理的R语言画森林图方法4的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 驾考你准备好了吗 之 交通标志、标线篇
- 下一篇: 计算机办公自动化应用课程,1-《计算机应