簡體   English   中英

如何在 r 中分組 plot 幾個箱線圖?

[英]How to plot several boxplots by group in r?

ID <- 1:10 
group <- c(1,1,1,2,2,2,3,3,3,3)
var1 <- c(6:15) 
var2 <- c(7:16) 
var3 <- c(6:11, NA, NA, NA, NA)
var4 <- c(4:9, NA, NA, NA, NA) 
data <- data.frame(ID, group, var1, var2, var3, var4)

library(dplyr)
 data %>% group_by(group) %>% boxplot(var1, var2)

最后一行不按我的意願工作。 這個想法是在一個圖形中獲得 4 個箱線圖。 每個變量兩個。 也許我需要使用 ggplot2?

如果要在同一圖中獲取兩個變量,則需要重新組織數據。 這是一個ggplot2解決方案:

# load library
  library(ggplot2)
  library(tidyr)
  library(ggthemes)


# reorganize data
  df <- gather(data, "ID","group") 

#rename columns 
  colnames(df) <- c("ID","group","var","value")

# plot
  ggplot(data=df) + 
    geom_boxplot( aes(x=factor(group), y=value, fill=factor(var)), position=position_dodge(1)) +
    scale_x_discrete(breaks=c(1, 2, 3), labels=c("A", "B", "C")) +
    theme_minimal() +
    scale_fill_grey() 

在此輸入圖像描述

制作具有相同寬度的箱形圖是一個完全不同的問題(這里的解決方案) ,但一個簡單的替代方案將是這樣的:

# recode column `group` in the `data.frame`.
  df <- transform(df, group = ifelse(group==1, 'A', ifelse(group==2, 'B', "C")))

# plot
  ggplot(data=df) + 
  geom_boxplot( aes(x=factor(var), y=value, fill=factor((var))), position=position_dodge(1)) +
  geom_jitter(aes(x=factor(var), y=value, color=factor((var)))) +
  facet_grid(.~group, scales = "free_x") +
  theme_minimal()+
  scale_fill_grey() +
  theme(axis.text.x=element_blank(),
        axis.title.x=element_blank(),
        axis.ticks=element_blank())

在此輸入圖像描述

您可以嘗試首先融合數據框(在@lukeA的評論中提到),然后堅持基礎圖形。 ggplot2lattice是其他不錯的選擇。

library(reshape2)

DF <- melt(data, id.vars = c("ID", "group"), measure.vars = c("var1", "var2"))

boxplot(value ~ group + variable, DF)

在此輸入圖像描述

替代lattice碼,也使用DF

bwplot(~ value | variable + group, data = DF)

替代ggplot2代碼,也使用DF

ggplot(DF, aes(x = factor(group), y = value, fill = variable)) + geom_boxplot()

雖然很晚,但在這里找到了一個很棒的 base-R 解決方案

# Create some data, e.g. from https://en.wikipedia.org/wiki/One-way_analysis_of_variance#Example
df <- as.data.frame(matrix(c(6, 8, 13, 8, 12, 9, 4, 9, 11, 5, 11, 8, 3, 6, 7, 4, 8, 12),ncol = 3, byrow = TRUE))
df <- reshape(data = df, direction = "long", idvar=1:3, varying=1:3, sep = "", timevar = "Treatment")
df$Treatment <- as.factor(df$Treatment)
rownames(df) <- NULL

par(mfrow = c(2, 1))
par(mar=c(1,4,4,2) + 0.1) # mar=c(b,l,t,r)
boxplot(V ~ Treatment, data = df, xlab = NULL, xaxt = "n",
        ylab = "V", main = "One-way anova with 3 different levels of one factor")
stripchart(V ~ Treatment,     # Points
           data = df,         # Data
           method = "jitter", # Random noise
           pch = 19,          # Pch symbols
           col = 4,           # Color of the symbol
           vertical = TRUE,   # Vertical mode
           add = TRUE)        # Add it over

par(mar=c(5,4,0,2) + 0.1)
boxplot(V ~ Treatment, data = df, xlab = "Treatment",
        ylab = "V", main = NULL)
stripchart(V ~ Treatment,     # Points
           data = df,         # Data
           method = "overplot", # Random noise
           pch = 19,          # Pch symbols
           col = 4,           # Color of the symbol
           vertical = TRUE,   # Vertical mode
           add = TRUE)        # Add it over
par(mfrow = c(1, 1))

結果: 在此處輸入圖像描述

暫無
暫無

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

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