簡體   English   中英

在一張圖中繪制多個箱線圖

[英]Plot multiple boxplot in one graph

我將數據保存為.csv文件,包含 12 列。 第 2 列到第 11 列(標記為F1, F2, ..., F11 )是features Column one包含這些特征的label ,要么good要么bad

我想根據label繪制所有這 11 個特征boxplot ,但按goodbad分開。 到目前為止我的代碼是:

qplot(Label, F1, data=testData, geom = "boxplot", fill=Label, 
          binwidth=0.5, main="Test") + xlab("Label") + ylab("Features")

但是,這僅針對label顯示F1

我的問題是:如何在一個帶有dodge position圖表中針對label顯示F2, F3, ..., F11 我已經對特征進行了標准化,因此它們在 [0 1] 范圍內處於相同的比例。

測試數據可以在這里找到。 我手繪了一些東西來解釋這個問題(見下文)。

手繪箱線圖示例

在繪圖之前,您應該通過融合數據(有關融合數據的外觀,請參見下文)以特定格式獲取數據。 否則,你所做的似乎沒問題。

require(reshape2)
df <- read.csv("TestData.csv", header=T)
# melting by "Label". `melt is from the reshape2 package. 
# do ?melt to see what other things it can do (you will surely need it)
df.m <- melt(df, id.var = "Label")
> df.m # pasting some rows of the melted data.frame

#     Label variable      value
# 1    Good       F1 0.64778924
# 2    Good       F1 0.54608791
# 3    Good       F1 0.46134200
# 4    Good       F1 0.79421221
# 5    Good       F1 0.56919951
# 6    Good       F1 0.73568570
# 7    Good       F1 0.65094207
# 8    Good       F1 0.45749702
# 9    Good       F1 0.80861929
# 10   Good       F1 0.67310067
# 11   Good       F1 0.68781739
# 12   Good       F1 0.47009455
# 13   Good       F1 0.95859182
# 14   Good       F1 1.00000000
# 15   Good       F1 0.46908343
# 16    Bad       F1 0.57875528
# 17    Bad       F1 0.28938046
# 18    Bad       F1 0.68511766

require(ggplot2)
ggplot(data = df.m, aes(x=variable, y=value)) + geom_boxplot(aes(fill=Label))

boxplot_ggplot2

編輯:我意識到您可能需要分面。 這也是一個實現:

p <- ggplot(data = df.m, aes(x=variable, y=value)) + 
             geom_boxplot(aes(fill=Label))
p + facet_wrap( ~ variable, scales="free")

ggplot2_faceted

編輯 2:如何添加x-labelsy-labelstitle 、更改legend heading 、添加jitter

p <- ggplot(data = df.m, aes(x=variable, y=value)) 
p <- p + geom_boxplot(aes(fill=Label))
p <- p + geom_jitter()
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p 

ggplot2_geom_plot

編輯 3:如何將geom_point()點與箱線圖的中心對齊? 可以使用position_dodge來完成。 這應該有效。

require(ggplot2)
p <- ggplot(data = df.m, aes(x=variable, y=value)) 
p <- p + geom_boxplot(aes(fill = Label))
# if you want color for points replace group with colour=Label
p <- p + geom_point(aes(y=value, group=Label), position = position_dodge(width=0.75))
p <- p + facet_wrap( ~ variable, scales="free")
p <- p + xlab("x-axis") + ylab("y-axis") + ggtitle("Title")
p <- p + guides(fill=guide_legend(title="Legend_Title"))
p 

ggplot2_position_dodge_geom_point

使用基礎圖形,我們可以使用at =來控制框的位置,結合boxwex =來控制框的寬度。 第一個boxplot語句創建一個空白圖。 然后在以下兩個語句中添加 2 個跟蹤。

請注意,在下面,我們使用df[,-1]從要繪制的值中排除第一個 (id) 列。 對於不同的數據框,可能需要將其更改為包含要繪制的數據的列的子集。

boxplot(df[,-1], boxfill = NA, border = NA) #invisible boxes - only axes and plot area
boxplot(df[df$id=="Good", -1], xaxt = "n", add = TRUE, boxfill="red", 
  boxwex=0.25, at = 1:ncol(df[,-1]) - 0.15) #shift these left by -0.15
boxplot(df[df$id=="Bad", -1], xaxt = "n", add = TRUE, boxfill="blue", 
  boxwex=0.25, at = 1:ncol(df[,-1]) + 0.15) #shift to the right by +0.15

在此處輸入圖片說明

一些虛擬數據:

df <- data.frame(
  id = c(rep("Good",200), rep("Bad", 200)),
  F1 = c(rnorm(200,10,2), rnorm(200,8,1)),
  F2 = c(rnorm(200,7,1),  rnorm(200,6,1)),
  F3 = c(rnorm(200,6,2),  rnorm(200,9,3)),
  F4 = c(rnorm(200,12,3), rnorm(200,8,2)))

由於您沒有提到 plot 包,我在這里建議使用Lattice版本(我認為 ggplot2 的答案比lattice 的要多,至少因為我在這里)。

 ## reshaping the data( similar to the other answer)
 library(reshape2)
 dat.m <- melt(TestData,id.vars='Label')
 library(lattice)
 bwplot(value~Label |variable,    ## see the powerful conditional formula 
        data=dat.m,
        between=list(y=1),
        main="Bad or Good")

在此處輸入圖片說明

格子圖的ggplot版本:

library(reshape2)
library(ggplot2)
df <- read.csv("TestData.csv", header=T)
df.m <- melt(df, id.var = "Label")

ggplot(data = df.m, aes(x=Label, y=value)) + 
         geom_boxplot() + facet_wrap(~variable,ncol = 4)

陰謀:在此處輸入圖片說明

我知道這是一個較舊的問題,但它也是我遇到的一個問題,雖然公認的答案有效,但有一種方法可以在使用 ggplot 或lattice 等附加包的情況下做類似的事情。 箱線圖重疊而不是並排顯示並不是很好,但是:

boxplot(data1[,1:4])
boxplot(data2[,1:4],add=TRUE,border="red")

這是做什么的圖片。

這將放入兩組箱線圖,第二組的輪廓(無填充)為紅色,並且異常值也為紅色。 好消息是,它適用於兩個不同的數據幀,而不是試圖重塑它們。 快速而骯臟的方式。

在基R 2與交互的式接口( : )可用於實現這一目標。

df <- read.csv("~/Desktop/TestData.csv")
df <- data.frame(stack(df[,-1]), Label=df$Label) # reshape to long format

boxplot(values ~ Label:ind, data=df, col=c("red", "limegreen"), las=2)

例子

暫無
暫無

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

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