簡體   English   中英

ggplot2 R 中不相關變量的堆疊條形圖將變量轉換為基於存在不存在的百分比

[英]stacked barplot converting a variable into a presence absence based percentage for unrelated variables in ggplot2 R

以下是示例數據框

df <- data.frame(SampleID = c(1, 2, 3, 4, 5, 6, 7, 8),
                 Var1 = c(0.1 , 0.5,    0.7,    0,  0,  0,  0.5,    0.2), 
                 Var1PA = c("Present", "Present", "Present", "Absent", "Absent", "Absent",  "Present", "Present"), 
                 Var2 = c(0, 0, 0, 0, 0.1, 0.5, 0.7, 0.2), 
                 Var2PA = c("Absent", "Absent", "Absent", "Absent", "Present", "Present", "Present", "Present"))

我的問題一開始看起來很簡單,但我找不到將 dataframe 適當地編輯為 plot 條形圖的方法。

對於 Var1,我想要 plot 樣本中存在 var1 的次數百分比的堆疊條形圖(即 var1 值 > 0)或不存在(類似於 var2 等)。

我可以通過以下方式確定這個百分比:

(1 - sum(df$Var1 == 0) / length(df$Var1)) * 100

但是如何在繪圖時將其轉換為百分比? 我查看了很多熔化選項,但對於這些變量沒有統一的標准可以構成一個共同的 X 軸

最后,如果我想從 dataframe 的 1000 個這樣的列變量中提取 plot 5 個變量,該如何回答上述問題?

編輯:感謝您到目前為止的回答! 我對問題進行了輕微的編輯,我只是在我的數據框中添加了一個變量

df <- data.frame(SampleID = c(1, 2, 3, 4, 5, 6, 7, 8),
             Var1 = c(0.1 , 0.5,    0.7,    0,  0,  0,  0.5,    0.2), 
             Var1PA = c("Present", "Present", "Present", "Absent", "Absent", "Absent",  "Present", "Present"), 
             Var2 = c(0, 0, 0, 0, 0.1, 0.5, 0.7, 0.2), 
             Var2PA = c("Absent", "Absent", "Absent", "Absent", "Present", "Present", "Present", "Present"),
             Disease = c("Case", "Control", "Case", "Control", "Case", "Control", "Case", "Control"))

我想弄清楚如何 plot 為 Var1PA、Var2PA 等在存在缺失情況下堆疊的案例和控件的條形圖。 如果我有正確的數據框輸入,ggplot2 代碼將是:vars <- c('Var1PA', 'Var2PA', 'Var2PA') ##based on the first comment by @rawr tt <- data.frame(prop .table(as.table(sapply(df[, vars], table)), 2) * 100) ggplot(tt, aes(Disease, Freq)) +
geom_bar(aes(fill = Var1), position = "堆棧", stat="身份") + facet_grid(~vars)

如何獲得每個變量的案例(存在和不存在)和控件(存在和不存在)的百分比? 謝謝!

這應該很好地概括。 當然,您可以對選擇的變量更具選擇性。

library(dplyr)
library(tidyr)
mdf = df %>% select(SampleID, ends_with("PA")) %>%
    gather(key = Var, value = PA, -SampleID) %>%
    mutate(PA = factor(PA, levels = c("Present", "Absent")))

ggplot(mdf, aes(x = Var, fill = PA)) +
    geom_bar(position = "fill") +
    scale_y_continuous(labels = scales::percent)

在此處輸入圖片說明

您可以將百分比列添加到長數據框中:

mdf %>% group_by(Var) %>%
    mutate(p_present = mean(PA == "Present"),
           p_absent = mean(PA == "Absent"))
# Source: local data frame [16 x 5]
# Groups: Var [2]
# 
#    SampleID    Var      PA p_present p_absent
#       <dbl>  <chr>  <fctr>     <dbl>    <dbl>
# 1         1 Var1PA Present     0.625    0.375
# 2         2 Var1PA Present     0.625    0.375
# 3         3 Var1PA Present     0.625    0.375
# 4         4 Var1PA  Absent     0.625    0.375
# 5         5 Var1PA  Absent     0.625    0.375
# 6         6 Var1PA  Absent     0.625    0.375
# 7         7 Var1PA Present     0.625    0.375
# 8         8 Var1PA Present     0.625    0.375
# 9         1 Var2PA  Absent     0.500    0.500
# 10        2 Var2PA  Absent     0.500    0.500

或者,如果你寧願看到一個1線每組總結,更換mutatesummarize

mdf %>% group_by(Var) %>%
    summarize(p_present = mean(PA == "Present"),
           p_absent = mean(PA == "Absent"))
# # A tibble: 2 × 3
#      Var p_present p_absent
#    <chr>     <dbl>    <dbl>
# 1 Var1PA     0.625    0.375
# 2 Var2PA     0.500    0.500

我的解決方案

library(ggplot2)
library(reshape)
library(dplyr)

df <- data.frame(
  SampleID = c(1, 2, 3, 4, 5, 6, 7, 8),
  Var1 = c(0.1, 0.5, 0.7, 0, 0, 0, 0.5, 0.2),
  Var1PA = c("Present", "Present", "Present", "Absent", "Absent", "Absent", "Present", "Present"),
  Var2 = c(0, 0, 0, 0, 0.1, 0.5, 0.7, 0.2),
  Var2PA = c("Absent", "Absent", "Absent", "Absent", "Present", "Present", "Present", "Present")
)

reshape::melt(df, c('SampleID')) |> 
  filter(variable == 'Var1' | variable == 'Var2') |> 
  mutate(value1 = ifelse(value == 0, 'Absent', 'Present')) |> 
  group_by(variable) |> count(variable, value1) |> 
  mutate(
    prc = n/sum(n)
  ) |>  as.data.frame() |> 
  ggplot( aes(x = variable, y = prc, fill = value1)) +
    geom_bar(stat = 'identity', position = 'fill', width = 0.7) +
    scale_y_continuous(labels = scales::percent) +
    labs(fill = 'Presence status') +
    geom_text(aes(x = variable, y = prc, label = stat(y)),
              position = position_fill(vjust = 0.5))

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM