2015-06-29 54 views
0

我是R的新手,一直在試圖製作boxplot。我使用的部分數據顯示使用ggplot2的R中的Boxplot

  h1   h2   h3   h4   h5   h6   h7   h8   h9   h10 
1 0.003719430 0.002975544 0.003049933 0.003421876 0.003421876 0.003347487 0.003645042 0.003496264 0.007364472 0.009075410 
2 0.003400540 0.002749373 0.003038781 0.003328188 0.003328188 0.003400540 0.003472892 0.003400540 0.007741656 0.009333398 
3 0.003741387 0.002918282 0.003142765 0.003367248 0.003367248 0.003367248 0.003666559 0.003516904 0.008081396 0.008156223 
4 0.003870634 0.002884002 0.003187581 0.003339370 0.003567055 0.003415265 0.003794739 0.003491160 0.008348426 0.007741268 
5 0.003782963 0.002950711 0.003177689 0.003480326 0.003404667 0.003404667 0.003707304 0.003631645 0.008927793 0.007414608 
6 0.003643736 0.002884624 0.003264180 0.003416002 0.003491913 0.003416002 0.003871469 0.003795558 0.009033428 0.007135649 
7 0.003718600 0.003035592 0.003111482 0.003339151 0.003566821 0.003566821 0.003642710 0.003870380 0.008120209 0.008044319 
8 0.003819313 0.002979064 0.003284609 0.003360995 0.003590154 0.003437382 0.003895699 0.003590154 0.008326102 0.007791398 
9 0.003899334 0.002981844 0.003211216 0.003364131 0.003669961 0.003440589 0.003746419 0.003669961 0.008410328 0.007569295 
10 0.003828488 0.002986220 0.003292499 0.003445639 0.003522209 0.003522209 0.003598778 0.003598778 0.008422673 0.007810115 

當我使用默認boxplot命令,那麼這是我所得到的

boxplot(df) 

enter image description here

我一直在試圖生成boxplot相同的數據使用ggplot2,但它給出了我無法解決的錯誤。這是我的嘗試。

library(ggplot2) 
df <- readRDS('data.Rda') 
ggplot(df) + geom_boxplot() 

這裏的錯誤

Don't know how to automatically pick scale for object of type data.frame. Defaulting to continuous 
Error: Aesthetics must either be length one, or the same length as the dataProblems:df[, 6:15] 

只見ggplot2 docsgeom_boxplot與實現(從示例),我需要重新安排我的數據,如

col1  col2 
h1 0.003719430 
h1 0.003400540 
h1 0.003741387 
h1 0.003870634 
h1 0.003782963 
h1 0.003643736 
h2 0.002975544 
h2 0.002749373 
h2 0.002918282 
h2 0.002884002 
h2 0.002950711 
h2 0.002884624 
... 

,並使用類似

ggplot(df, aes(factor(col1), col2)) + geom_boxplot() 

但這是很多工作。我相信必須有一些方法可以自動執行,而我無法找到。任何幫助表示讚賞。

+0

都能跟得上這是它的要點。這是'ggplot2'的缺點。更詳細。但也很容易做出非常漂亮。 – CephBirk

+2

...你可能花了10倍多的時間來寫這個問題,「工作」涉及重塑你的數據:'庫(reshape2);熔體(DF)'。 – joran

回答

3

你是對的ggplot需要您的數據進行重新塑造,但它並不難使用包reshape2

library(reshape2) 
df <- melt(df) 
ggplot(df, aes(x=variable, y=value)) + geom_boxplot() 
+2

gah。你打敗了我。 – theamateurdataanalyst