绘制框图和矩阵的重叠数据点

时间:2015-10-19 11:08:52

标签: r ggplot2

我的数据格式如下:

          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)

但是这只是手段,每当我尝试使用所有点时不仅意味着它崩溃。

1 个答案:

答案 0 :(得分: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