如何在同一个图表中制作beanplot和boxplot?

时间:2014-10-06 10:37:23

标签: r boxplot

seasons <- structure(list(values = c(204, 339, 304, 434, 334, 212, 361, 
102, 298, 369, 149, 227, 278, 199, 360, 211, 219, 209, 177, 299, 
262, 285, 237, 227, 216, 229, 317, 321, 327, 123, 84, 321, 442, 
263, 225, 290, 259, 219, 244, 325, 257, 672, 762, 381, 698, 578, 
576, 386, 834, 790, 815, 736, 517, 556, 685, 781, 703, 1071, 
537, 784, 753, 790, 489, 878, 433, 742, 638, 731, 1017, 850, 
804, 612, 923, 1000, 855, 750, 921, 676, 621, 781, 703, 1054, 
156, 312, 267, 152, 352, 155, 215, 184, 186, 221, 352, 183, 307, 
353, 507, 255, 159, 109, 343, 377, 209, 260, 193, 231, 111, 167, 
233, 360, 488, 347, 208, 178, 371, 276, 263, 166, 486, 119, 153, 
315, 226, 158, 142, 78, 75, 156, 53, 103, 141, 94, 94, 55, 84, 
35, 82, 65, 150, 30, 201, 184, 94, 119, 150, 70, 63, 50, 74, 
160, 49, 52, 135, 105, 129, 75, 83, 85, 84, 85, 77, 147, 100, 
46), ind = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 
3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), .Label = c("spring", 
"summer", "autumn", "winter"), class = "factor", scores = structure(c(3, 
1, 2, 4), .Dim = 4L, .Dimnames = list(c("autumn", "spring", "summer", 
"winter"))))), .Names = c("values", "ind"), row.names = c(NA, 
-164L), class = "data.frame")

我在下面制作了一个boxplot和beanplot。

boxplot(seasons$values~seasons$ind, ylim= c(0,1200))
beanplot(seasons$values~seasons$ind, ylim= c(0,1200),
     col = c("#CAB2D6", "#33A02C", "#B2DF8A"), border = "#CAB2D6", side="second")

我想同时制作一个包含这些boxplot和beanplot的图表。 这将使比较变得容易。泰国人就是为什么我在两个地块都做了同样的事情。

我有什么办法吗?

2 个答案:

答案 0 :(得分:5)

使用beanplot包,使用add = TRUE:

boxplot(seasons$values~seasons$ind, ylim= c(0,1200))
beanplot(seasons$values~seasons$ind, ylim= c(0,1200), col = c("#CAB2D6", "#33A02C", "#B2DF8A"), border = "#CAB2D6", side="second", add=T)

enter image description here

答案 1 :(得分:2)

尝试使用ggplot:

ggplot(seasons, aes(x=ind, y=values))+geom_boxplot()+geom_violin(fill='lightblue', alpha=0.5)+geom_jitter(position = position_jitter(width = .1))

enter image description here