如何制作两组之间存在显着差异的箱线图

时间:2017-11-29 17:52:19

标签: r plot ggplot2

我设法制作了这样的数据

df<- structure(list(label = structure(c(2L, 2L, 2L, 1L, 1L, 1L, 2L, 
2L, 2L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 1L, 1L), .Label = c("boys", 
"girls"), class = "factor"), variable = structure(c(1L, 1L, 1L, 
1L, 1L, 1L, 3L, 3L, 3L, 3L, 3L, 5L, 2L, 2L, 2L, 2L, 2L, 4L, 4L, 
4L), .Label = c(" G1", " G20", " G5", " G52", " G9"), class = "factor"), 
    value = structure(c(3L, 8L, 18L, 1L, 15L, 17L, 19L, 7L, 2L, 
    2L, 11L, 10L, 6L, 4L, 9L, 12L, 14L, 5L, 13L, 16L), .Label = c("112864.443", 
    "11319531", "12874.443", "142983324", "1612410048", "16349475.63", 
    "184901841", "2223793.8", "30553282.01", "312004.547", "3135868.44", 
    "317403612.9", "3686081.063", "43701608", "623793.8", "64959501.42", 
    "67666215", "767666215", "775987137.8"), class = "factor")), .Names = c("label", 
"variable", "value"), class = "data.frame", row.names = c(NA, 
-20L))

现在我正在尝试为每一组制作一个箱形图

当我这样做时

ggplot(data = df, aes(x=variable, y=value)) + geom_boxplot(aes(fill=label))

它只给我以下内容,这意味着它绘制了所有分开的数据

enter image description here

我想要的是把它们放在一起作为一个盒子。 这些都是G1,这意味着它们将一起装箱(一种颜色的女孩和另一种颜色的男孩)x轴变为1。在这一组中,女孩有3个重复(samp1,2和3),男孩有3个重复(samp4,5,6)

然后第二个盒子将在这个案例中女孩有3个重复(samp1,2,3),男孩有2个重复(samp5,6)

如果框图不能绘制很少的点,那么像这样的东西也会很棒 https://www.r-graph-gallery.com/47-groups-distribution-with-ggplot2/

我想对不同x轴的女孩和不同x轴的男孩进行重要的比较,如Put stars on ggplot barplots and boxplots - to indicate the level of significance (p-value)

1 个答案:

答案 0 :(得分:0)

好的,您的真正问题是,您将value存储为factor,就好像它是分类数据一样。我们可以解决这个问题,然后绘制:

df$value = as.numeric(as.character(df$value))

ggplot(df, aes(x = variable, y = value, fill = label)) +
    geom_boxplot()

但你真的没有足够的箱形图数据。我会使用积分,也许有点像这样:

ggplot(df, aes(x = variable, y = value, color = label)) +
    geom_point(position = position_dodge(width = 0.2))

enter image description here