How to annotate different values for each facet (bar plot) on R?

前端 未结 1 690
庸人自扰
庸人自扰 2021-01-16 14:37

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

相关标签:
1条回答
  • 2021-01-16 15:14

    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)
    

    0 讨论(0)
提交回复
热议问题