如何为R上的每个构面(条形图)注释不同的值? [英] How to annotate different values for each facet (bar plot) on R?

查看:217
本文介绍了如何为R上的每个构面(条形图)注释不同的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道如何注释条形图中的每个方面.现在,我正在使用geom_signif函数,该函数可以完美地工作,除了它可以将一个面上的注释复制到另一个面上.

I'd like to know how to annotate each facet in my bar plot. Right now, I'm using the geom_signif function which works perfectly except that it duplicates the annotation on one facet onto the other facet.

我的代码是这样的:

geom_signif(annotation = c("p=0.01"),
            y_position = c(9), xmin = c(2), xmax = c(3))

我的条形图:

请告知.我在这里已经阅读了一些类似的解决方案,并尝试了其他方法,但是我似乎仍然无法弄清楚..这是到目前为止我所获得的最接近,最简单的解决方案,只是需要两个不同的注释(在这种情况下,p值-我在方面分别进行了方差分析.

Please advise. I've read through some similar solutions here, tried some other ways but I still can't seem to figure it out.. This is the closest and easiest solution to what I got so far except that I want 2 different annotations (labeling of p-values in this case -I ran ANOVA separately) on the facets.

推荐答案

此处是通过手动解构图并使用新注释进行重构来实现此目的的示例.我理解它是因为您希望每个图都有手动文本注释.此(非常手动的)解决方案基于另一个答案如何在R的多面条形图上标注p值?,这可能正是您要寻找的.

Here is an example of to do it by manually deconstructing the plot and reconstructing with new annotations. I understood it as you wanted manual text annotations per plot. This (very manual) solution is based on another answer, How do I annotate p-values onto a faceted bar plots on R?, which might be exactly what you are looking for.

df <- data.frame(iris,type = c(1,2))

## Construct your plot exactly as you have already done
## Annotations are replicated.
myplot <- ggplot(df, aes(x=Species,y = Sepal.Length)) + 
    geom_boxplot() + 
    facet_grid(.~type) + 
    geom_signif(annotation = c("foo"),xmin = 1, xmax = 2,y_position = 7.5)
myplot

## Disassemble plot
myplot2 <- ggplot_build(myplot)
myplot2$data[[2]]

 x xend     y  yend annotation group PANEL shape colour textsize angle hjust vjust alpha family fontface lineheight
1 1    1 7.392 7.500        foo     1     1    19  black     3.88     0   0.5     0    NA               1        1.2
2 1    2 7.500 7.500        foo     1     1    19  black     3.88     0   0.5     0    NA               1        1.2
3 2    2 7.500 7.392        foo     1     1    19  black     3.88     0   0.5     0    NA               1        1.2
4 1    1 7.392 7.500        bar     1     2    19  black     3.88     0   0.5     0    NA               1        1.2
5 1    2 7.500 7.500        bar     1     2    19  black     3.88     0   0.5     0    NA               1        1.2
6 2    2 7.500 7.392        bar     1     2    19  black     3.88     0   0.5     0    NA               1        1.2
  linetype size
1        1  0.5
2        1  0.5
3        1  0.5
4        1  0.5
5        1  0.5
6        1  0.5

## Note there are 6 observations, 3 for each "PANEL". 
## Now, change the annotation on each "PANEL".
myplot2$data[[2]]$annotation <- c(rep("foo",3),rep("bar",3))

## Reconstruct plot
myplot3 <- ggplot_gtable(myplot2)
plot(myplot3)

这篇关于如何为R上的每个构面(条形图)注释不同的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆