2015-10-19 41 views
0

我有格式化這樣的數據:情節箱線圖和重疊的數據點矩陣

  V1   V2   V3   V4   V5   V6   V7   V8   V9   V10  V11   V12 
1 0.18490759 0.09539521 0.07740119 0.05967579 0.04958317 0.041460405 0.035371830 0.02842569 0.02669743 0.023443969 0.02306812 0.021540634 
2 0.16242156 0.09896433 0.08041313 0.06165633 0.05775518 0.058245083 0.039312977 0.03703704 0.03010164 0.032751092 0.03015988 0.030905077 
3 0.13704054 0.07986890 0.06648361 0.05696411 0.04382437 0.039943880 0.037184482 0.03217842 0.02929786 0.023364864 0.02349103 0.023218186 
4 0.09796357 0.05204132 0.04126062 0.03342530 0.02788063 0.023995760 0.022976942 0.01981317 0.01803192 0.017386063 0.01561585 0.015185407 
5 0.03300330 0.01673640 0.03728814 0.03225806 0.01730104 0.008264463 0.023411371 0.02352941 0.01904762 0.015094340 0.01107011 0.004201681 
6 0.04137931 0.03305785 0.01980198 0.01776199 0.02877698 0.005415162 0.003508772 0.02304965 0.01458671 0.006980803 0.01554404 0.018518519 

我想繪製與重疊數據點每列的箱線圖。

我想:

tw_means <- (colMeans(tw)) 
colnames(tw) <- seq_len (ncol(tw)) 
boxplot(tw [,order (tw_means)], horizontal = FALSE, outline = FALSE) 
points(tw_means [order(tw_means)], pch=19, col=2) 

但這只是陰謀的手段,每當我嘗試使用所有點不僅意味着,它崩潰。

+0

你能在預期的輸出詳細點嗎?當我使用你的數據運行你的代碼時,我得到了12個帶重疊紅點的箱形圖表。 – Heroka

+0

是的。因此,我不希望每個數據點都來自該boxplot,最好是抖動,所以它們不會彼此堆疊。應該看起來像: http://stackoverflow.com/questions/22074164/scatter-plot-and-boxplot-overlay?rq=1 – redaktroll

+0

所以你想要一個boxplot,所有的數據點和一個不同的顏色點的意思? – Heroka

回答

0

這是給你一個開始:

library(reshape2) 

#ggplot needs long data 

tw_melt <- melt(tw) 

#order variable factor by the mean - can be left out but was included in original example. 
tw_means <- colMeans(tw) 
tw_melt$variable_ordered <- factor(tw_melt$variable, 
            levels=names(tw_means)[order(tw_means)]) 

p1 <- ggplot(tw_melt, aes(x=variable_ordered,y=value))+ 
    geom_boxplot()+ 
    geom_point(color="blue",jitter=T) 
p1 

enter image description here

+0

完美。我努力解決融化問題。 – redaktroll

+0

你的意思是在這個答案中,還是在你鏈接的例子中?對於未來,請閱讀如何提問。您只在標籤中提到了ggplot2。 – Heroka