2014-09-04 76 views
-2

我具有以下形式的數據:顯示每組的多個箱圖中的R

Day A B 
1 1 4 
1 2 5 
1 3 6 
2 2 2 
2 3 4 
2 5 6 
3 6 7 
3 4 6 

我想單個圖表上顯示此,與沿x軸Day,以及與每個x位置對於AB(顏色編碼)中的每一個都有箱形圖。

+0

我也有興趣在改變用於箱線圖值統計,但這可能是一個單獨的問題。 – thomasfedb 2014-09-04 01:32:45

+0

大家知道,這裏的很多r標籤子社區都會看到這樣的問題,相當粗魯:基本上就是一個工單,就像在餐廳下訂單一樣。所以它可能會吸引投票,很多人會忽略它。另一方面,如果你分享它,我肯定人們會很樂意幫助你做出一個無效的_specific_嘗試。 – joran 2014-09-04 01:53:00

+0

這可能是有用的:http://stackoverflow.com/questions/10441214/r-boxplot-with-multiple-factor-labels – thelatemail 2014-09-04 02:36:18

回答

2

以下是對?boxplot幫助頁面示例的(輕微)修改。你或許應該看看那些例子發佈到計算器之前

tg <- data.frame(
    dose=ToothGrowth$dose[1:30], 
    A=ToothGrowth$len[1:30], 
    B=ToothGrowth$len[31:60] 
) 
head(tg) 
# dose A B 
# 1 0.5 4.2 15.2 
# 2 0.5 11.5 21.5 
# 3 0.5 7.3 17.6 
# 4 0.5 5.8 9.7 
# 5 0.5 6.4 14.5 
# 6 0.5 10.0 10.0 

boxplot(A ~ dose, data = tg, 
     boxwex = 0.25, at = 1:3 - 0.2, 
     col = "yellow", 
     main = "Guinea Pigs' Tooth Growth", 
     xlab = "Vitamin C dose mg", 
     ylab = "tooth length", 
     xlim = c(0.5, 3.5), ylim = c(0, 35), yaxs = "i") 
boxplot(B ~ dose, data = tg, add = TRUE, 
     boxwex = 0.25, at = 1:3 + 0.2, 
     col = "orange") 
legend(2, 9, c("A", "B"), 
     fill = c("yellow", "orange")) 

enter image description here

+0

謝謝@MrFlick,精彩。對於我在RTFM中缺乏盡職調查,我感到抱歉! – thomasfedb 2014-09-04 05:36:18

2

嘗試:

ddf = structure(list(Day = c(1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L), A = c(1L, 
2L, 3L, 2L, 3L, 5L, 6L, 4L), B = c(4L, 5L, 6L, 2L, 4L, 6L, 7L, 
6L)), .Names = c("Day", "A", "B"), class = "data.frame", row.names = c(NA, 
-8L)) 

mm = melt(ddf, id='Day') 
ggplot(mm)+geom_boxplot(aes(x=factor(Day), y=value, fill=variable)) 

enter image description here