[英]Plot multiple boxplot in one graph
我將數據保存為.csv
文件,包含 12 列。 第 2 列到第 11 列(標記為F1, F2, ..., F11
)是features
。 Column one
包含這些特征的label
,要么good
要么bad
。
我想根據label
繪制所有這 11 個特征的boxplot
,但按good
或bad
分開。 到目前為止我的代碼是:
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))
編輯:我意識到您可能需要分面。 這也是一個實現:
p <- ggplot(data = df.m, aes(x=variable, y=value)) +
geom_boxplot(aes(fill=Label))
p + facet_wrap( ~ variable, scales="free")
編輯 2:如何添加x-labels
、 y-labels
、 title
、更改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
編輯 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
使用基礎圖形,我們可以使用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)
陰謀:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.