【问题标题】:R ggplot: text & graphics below a plot with facetR ggplot:带有刻面的图下方的文本和图形
【发布时间】:2014-04-29 10:11:14
【问题描述】:

我在 ggplot 中使用 facet 创建了一个分类数据图。

此处的示例脚本:

#script to produce plot with dummy data

rm(list=ls(all=TRUE))

library(ggplot2)
require(gridExtra)




#put dummy data in df   
dummy_data<-data.frame(experiment_number=c(rep("exp_1",15),rep("exp_2",15)),
   group=rep(c("A","B","C"),5),yvalue=runif(30, 0.0, 0.05))

# make plot

plot1<-ggplot(data = dummy_data)+
        geom_point(aes(x = group, y =  yvalue,
                    colour=group,shape=group),size=3.5,position = position_jitter(w = 0.2)) +
        facet_wrap( ~ experiment_number) +
        ylab("yvalue") +
        xlab("") 

#plot
plot1

我现在想在绘图下方添加文本和条形图,以显示与组之间的统计测试相关的 p 值 - 附上我刚刚在手中绘制的示例(p 值刚刚组成)。

请注意,两个不同面板中的 p 值会有所不同。我玩过注释和自定义注释,但似乎无法让它工作。有什么想法吗?

非常感谢

【问题讨论】:

    标签: r text graphics ggplot2 annotations


    【解决方案1】:

    这是一种完全荒谬的方式来做类似于您要求的事情。我使用geom_errorbar 作为条形图,所以我不得不翻转坐标系。无论如何,你应该能够自定义它来做你需要的。

    rm(list=ls(all=TRUE))    
    library(ggplot2)
    
    #put dummy data in df   
    dummy_data<-data.frame(experiment_number=c(rep("exp_1",15),rep("exp_2",15)),
                           group=rep(c("A","B","C"),5),yvalue=runif(30, 0.0, 0.05))
    # make plot 
    plot1<-ggplot(data = dummy_data)+
      geom_point(aes(y = group, x =  yvalue, #changed x and y
                     colour=group,shape=group),size=3.5,position = position_jitter(h = 0.2)) + # changed w=... to h=...
      facet_wrap( ~ experiment_number) +
      xlab("yvalue") +
      ylab("") + coord_flip() # flipped coordinate system
    
    #plot
    rng <- range(dummy_data$yvalue) # range
    df.lines <- data.frame(ymin=LETTERS[1:3], ymax=LETTERS[c(2,3,1)], x=rng[1]-diff(rng)*1:3/12) #data for geom_errorbar
    # data for geom_text
    df.txt <- data.frame(y=c("AB", "BC", "B"),
                         x=rng[1]-diff(rng)*(1:3+.5)/12, 
                         label=c("p=0.003", "p=0.05", "p=0.6", 
                                  "p=0.2", "p=0.1", "p=0.05"), 
                         experiment_number=rep(c("exp_1", "exp_2"), each=3))
    # add some space and geom_errorbar and geom_text
    plot2 <- plot1 + scale_x_continuous(limits=c(rng[1]-diff(rng)/3, rng[2]+diff(rng)/5)) +
      geom_errorbar(data=df.lines, aes(x=x, ymin=ymin, ymax=ymax)) +
      scale_y_discrete(breaks=LETTERS[1:3], limits=c("A", "AB", "B", "BC", "C")) +
      geom_text(data=df.txt, aes(x=x, y=y, label=label), xjust=0.5) 
    
    plot2
    

    【讨论】:

    • 您的图片文件没有出现。
    猜你喜欢
    • 2022-09-30
    • 1970-01-01
    • 2015-01-04
    • 2021-10-29
    • 2015-04-13
    • 2015-08-02
    • 2019-03-03
    • 1970-01-01
    • 2017-02-26
    相关资源
    最近更新 更多