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

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

问题描述

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

我的代码是这样的:

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

我的条形图:

请指教.我在这里阅读了一些类似的解决方案,尝试了其他一些方法,但我似乎仍然无法弄清楚.在这种情况下的 p 值 - 我单独运行方差分析)在方面.

解决方案

这是一个通过手动解构绘图并使用新注释重建来实现的示例.我理解它是因为您需要每个图的手动文本注释.这个(非常手动的)解决方案基于另一个答案,

## 反汇编图myplot2 <- ggplot_build(myplot)myplot2$data[[2]]

<块引用>

 x xend y yesnd 注释组 PANEL 形状颜色 textsize 角度 hjust vjust alpha family fontface lineheight1 1 1 7.392 7.500 foo 1 1 19 黑色 3.88 0 0.5 0 NA 1 1.22 1 2 7.500 7.500 foo 1 1 19 黑色 3.88 0 0.5 0 不适用 1 1.23 2 2 7.500 7.392 foo 1 1 19 黑色 3.88 0 0.5 0 NA 1 1.24 1 1 7.392 7.500 bar 1 2 19 黑色 3.88 0 0.5 0 NA 1 1.25 1 2 7.500 7.500 bar 1 2 19 黑色 3.88 0 0.5 0 NA 1 1.26 2 2 7.500 7.392 bar 1 2 19 黑色 3.88 0 0.5 0 NA 1 1.2线型尺寸1 1 0.52 1 0.53 1 0.54 1 0.55 1 0.56 1 0.5

## 注意有 6 个观察,每个PANEL"有 3 个.## 现在,更改每个面板"上的注释.myplot2$data[[2]]$annotation <- c(rep("foo",3),rep("bar",3))##重建情节myplot3 <- ggplot_gtable(myplot2)情节(我的情节3)

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.

My code is as such:

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

My bar plot:

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.

解决方案

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天全站免登陆