【问题标题】:Add significance stars to odds ratio plot (ggplot)将显着性星添加到优势比图 (ggplot)
【发布时间】:2020-11-24 14:26:29
【问题描述】:

我正在尝试制作具有优势比以及上下 ci 的可发布图。现在我确实用 ggplot 得到了不错的图表,但我想在 geom_point-layer 上方添加显着性星,它们对应于三个级别(.005、.001、.001 )。我制作了一个示例数据框,从中获取我的值。它还包括 p 值:

or<-as.numeric(round(rnorm(5,0,1),2))

n<-c(100,100,100,100,100)

p<-c(.06,.05,.01,.0012,.000034)

df<-as.data.frame(cbind(or,n,p))

df$names<-as.factor(c(1:5))

df$lwr.ci<-as.numeric(or-2)

df$upr.ci<-as.numeric(or+2)

ggplot-code如下:

p <- ggplot(df, aes(or, fct_rev(names)))+
  theme_bw(base_family = "Times New Roman")


p+geom_errorbarh(aes(xmax =upr.ci, xmin = lwr.ci), size = 1, height = 0, color = 'gray') +
  geom_point(size = 2, color = 'black')  +
  theme(panel.grid = element_blank()) +
  scale_x_continuous(breaks = c(0:13),limits = c(-4,5))+
  ylab('CD') +
  xlab('Odds Ratios')+
  ggtitle('R')+
  geom_text(parse=T,aes(label=paste(round(or,2),sep =" ","(","italic(n)==",n,")")),x= -3.5, size = 3,color='black',family="Times New Roman")

如果有人对如何将相应的重要性星添加到 geom_points 有任何建议,我将不胜感激。

【问题讨论】:

标签: r ggplot2


【解决方案1】:

尝试在这样的新变量中创建星星,首先是数据:

library(dplyr)
library(ggplot2)
#Data
or<-as.numeric(round(rnorm(5,0,1),2))

n<-c(100,100,100,100,100)

p<-c(.06,.05,.01,.0012,.000034)

df<-as.data.frame(cbind(or,n,p))

df$names<-as.factor(c(1:5))

df$lwr.ci<-as.numeric(or-2)

df$upr.ci<-as.numeric(or+2)

现在,标签的代码:

#Labels
plab <- ifelse(p<0.001,'***',ifelse(p<0.01,'**',ifelse(p<0.05,'*','')))
df$plab <- plab
#Plot
p <- ggplot(df, aes(or, fct_rev(names)))
#Plot 2
p+geom_errorbarh(aes(xmax =upr.ci, xmin = lwr.ci), size = 1, height = 0, color = 'gray') +
  geom_point(size = 2, color = 'black')  +
  theme(panel.grid = element_blank()) +
  scale_x_continuous(breaks = c(0:13),limits = c(-4,5))+
  ylab('CD') +
  xlab('Odds Ratios')+
  ggtitle('R')+
  geom_text(parse=T,aes(label=paste(round(or,2),
                                    sep =" ","(","italic(n)==",n,")")),
            x= -3.5, size = 3,color='black',family="Times New Roman")+
  geom_text(aes(label=plab),vjust=-0.5,fontface='bold')

输出:

如果需要任何调整,请告诉我。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-05-27
    • 1970-01-01
    • 1970-01-01
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 2011-03-23
    • 2021-03-03
    相关资源
    最近更新 更多