如何绘制具有显着水平的箱线图?

Sam*_*rom 22 r significance ggplot2 boxplot

前段时间我问了一个关于绘制boxplot Link1的问题.

我有3个不同的组(或标签)的一些数据请在这里下载.我可以使用以下R代码来获取boxplot

library(reshape2)
library(ggplot2)

morphData <- read.table(".\\TestData3.csv", sep=",", header=TRUE);
morphData.reshaped <- melt(morphData, id.var = "Label")

ggplot(data = morphData.reshaped, aes(x=variable, y=value)) + 
  + geom_boxplot(aes(fill=Label))
Run Code Online (Sandbox Code Playgroud)

在这里,我只是想知道如何将重要水平放在箱线图上方.为了使自己清楚,我在这里放了一张剪纸截图:

箱形图

con*_*-ae 23

我知道这是一个老问题,DatamineR的答案已经为这个问题提供了一个解决方案.但我最近创建了一个ggplot扩展,简化了添加有效条的整个过程:ggsignif

您只需添加单个图层,而不是繁琐地添加geom_lineannotate您的图表geom_signif:

library(ggplot2)
library(ggsignif)

ggplot(iris, aes(x=Species, y=Sepal.Length)) + 
  geom_boxplot() +
  geom_signif(comparisons = list(c("versicolor", "virginica")), 
              map_signif_level=TRUE)
Run Code Online (Sandbox Code Playgroud)

Boxplot与意义栏

CRAN提供该软件包的完整文档.


Dat*_*neR 21

我不太明白你的意思是boxplot具有显着的水平,但这里有一个建议如何生成这些条:我会解决这个构建小数据帧与条的坐标.这是一个例子:

pp <- ggplot(mtcars, aes(factor(cyl), mpg)) + geom_boxplot()
df1 <- data.frame(a = c(1, 1:3,3), b = c(39, 40, 40, 40, 39))
df2 <- data.frame(a = c(1, 1,2, 2), b = c(35, 36, 36, 35))
df3 <- data.frame(a = c(2, 2, 3, 3), b = c(24, 25, 25, 24))

pp + geom_line(data = df1, aes(x = a, y = b)) + annotate("text", x = 2, y = 42, label = "*", size = 8) +
     geom_line(data = df2, aes(x = a, y = b)) + annotate("text", x = 1.5, y = 38, label = "**", size = 8) +
     geom_line(data = df3, aes(x = a, y = b)) + annotate("text", x = 2.5, y = 27, label = "n.s.", size = 8)
Run Code Online (Sandbox Code Playgroud)

在此输入图像描述