【问题标题】:Add significance brackets to horizontal barplot向水平条形图添加显着性括号
【发布时间】:2022-06-14 23:08:48
【问题描述】:

在我的数据集中,两个不同组的受访者被邀请选择或不选择一系列变量。我可以让它工作以将重要星添加到标准垂直条形图,但我希望能够旋转它并使其水平。这可能吗?

#dataframe
group1<-sample(c("A", "B"), 100, replace=T)
var1<-sample(c(0,1),100, replace=T, prob=c(0.3, 0.7))
var2<-sample(c(0,1),100, replace=T, prob=c(0.4, 0.6))
var3<-sample(c(0,1),100, replace=T, prob=c(0.8, 0.2))
df<-data.frame(group1, var1, var2, var3)
library(tidyverse)
library(broom)
#Pivot
df %>% 
  pivot_longer(-group1) %>% 
  group_by(group1, name, value) %>% 
  #summarize(n=n()) %>% 
#Nest by variable
  nest(-name) %>%
#For each variable run the chi-squared test of group on whether or not 
# it was selected 
  mutate(model=map(data, ~chisq.test(.$group1, .$value)), 
#Tidy the results
         tidied=map(model, tidy)) %>% 
#unnest and store in new object
  unnest(tidied)->x2_test
#Adjust p-values for multiple comparisons 
x2_test$p.value<-p.adjust(x2_test$p.value, method="bonferroni", n=3)
x2_test

library(ggsignif)
#draw the plot 
df %>% 
  pivot_longer(-group1) %>% 
  group_by(name, group1, value) %>% 
  summarize(n=n()) %>% 
  ggplot(., aes(x=name,y=n))+geom_col(position="dodge", aes(fill=group1))+
#Set the positions of some significance tests at about y=50 
 geom_signif(y_position=c(50,50,50), xmin=c(0.8, 1.8, 2.2),xmax=c(1.2,2.2, 3),
              annotation=c(x2_test$p.value), map_signif_level = T)

【问题讨论】:

  • 你试过coord_flip()吗?

标签: r


猜你喜欢
  • 1970-01-01
  • 2021-12-04
  • 2021-04-09
  • 2013-06-09
  • 2019-09-07
  • 2021-12-15
  • 1970-01-01
相关资源
最近更新 更多