在ggplot2箱图上添加符号以指示变量的值

时间:2016-04-13 20:23:14

标签: r ggplot2 boxplot

使用更大数据集的以下子集

ex <- structure(list(transect_id = c(1L, 1L, 1L, 1L, 1L, 15L, 15L, 
15L, 15L, 15L, 15L), number_f = c(2L, 2L, 2L, 2L, 2L, 0L, 0L, 
0L, 0L, 0L, 0L), years_f = c(1L, 1L, 1L, 1L, 1L, 6L, 6L, 6L, 
6L, 6L, 6L), b = c(5.036625862, 6.468666553, 8.028989792, 4.168409348, 
5.790089607, 10.67796993, 9.371051788, 10.54364777, 6.904324532, 
7.203606129, 9.1611166)), .Names = c("transect_id", "number_f", 
"years_f", "b"), class = "data.frame", row.names = c(1L, 2L, 
3L, 4L, 5L, 2045L, 2046L, 2047L, 2048L, 2049L, 2050L))

我已经绘制了&#34; b&#34;的分布图。对于由&#34; transect_id&#34;指示的每个组;并用&#34; number_f&#34;给它们着色,我在这里做:

ggplot(aes(x=reorder(transect_id, b, FUN=median), y=b), data=ex) + geom_boxplot(aes(fill=as.factor(number_f))) + xlab('Transect ID')

enter image description here

我需要为每个&#34; transect_id&#34; groups是每个boxplot顶部的堆栈符号 - 星号或其他符号 - 以提供&#34; years_f&#34;的值的指示。对应于每个&#34; transect_id&#34;。在下面的数据子集中,&#34; years_f&#34; transect_ids 1和15分别为1和6。我希望看到类似的内容,我手动模拟了这些内容。

enter image description here

另外请记住,我使用的数据集非常大,所以我需要使用一些循环或其他一些自动执行此操作的方法。请注意,我绝对欢迎其他想法,以更好的方式来表明&#34; years_f&#34;这可能不会使数字负担过重,因为所有这些堆叠符号对于较大的&#34; years_f&#34;值尤其是一个问题。

1 个答案:

答案 0 :(得分:1)

尝试添加

annotate('text', x = c(1, 2), y = 3, label =  paste0('Year_F =', unique(ex$years_f)))

到你的情节结束时如下:

ggplot(aes(x=reorder(transect_id, b, FUN=median), y=b), data=ex) + 
    geom_boxplot(aes(fill=as.factor(number_f))) + xlab('Transect ID')+ 
    annotate('text', x = c(1, 2), y = 3, label =  paste0('Year_F =', unique(ex$years_f)))

enter image description here

要在更大的数据集上使用它,您必须编辑xy参数,但这可能是一个不错的选择。 y坐标的可能性可能类似于0.9 * min(ex$b)

修改回复您的评论:

您可以先计算transect_id指定x

的级别数
len.levels <- length(levels(as.factor(ex$transect_id)))

然后,您可以按years_f创建uniqe transect_id变量的摘要表:

sum.table <- aggregate(years_f~reorder(ex$transect_id, ex$b, median),
                       data = ex, FUN = unique)

  reorder(ex$transect_id, ex$b, median) years_f
1                                     1       1
2                                    15       6

然后绘制如下:

ggplot(aes(x=reorder(transect_id, b, FUN=median), y=b), data=ex) + 
  geom_boxplot(aes(fill=as.factor(number_f))) + xlab('Transect ID')+ 
  annotate('text', x = 1:len.levels, y = .9 * min(ex$b), 
           label =  paste0('Year_F =', sum.table[,2]))