繁体   English   中英

回归系数作为条形图和按组:ggplot

[英]Regression coefficients as bar chart and by group: ggplot

以下数据是我的线性回归的 output 比较干预组与对照组在 4 个不同时间点的多种营养素。 在此处输入图像描述 如何为每种营养素制作水平条形图:

  1. 所有 4 个时间点(列:后续)作为条形(列:inter_val)并按照特定的营养顺序
  2. 在条形顶部添加 95% CI 条形(列:inter_lowCI、inter_upCI)
  3. 最上面的条应该是基线,然后是 6 个月、24 个月和 48 个月
  4. 添加 * 或 ** 或 *** 以显示 p 值显着性(列:p_Intervention)
  5. 对对照组做同样的事情(显示没有 p 值显着性)

这就是我所做的并且无法做到以上几点。

ggplot(plot1, aes(factor(Nutrient), inter_val, fill = Followup)) + 
 geom_bar(stat="identity", position = "dodge") + 
 geom_errorbar(aes(ymin=inter_val-inter_lowCI, ymax=inter_val+inter_upCI), width=1, size=1) + 
 scale_fill_brewer(palette = "Set1") +
 coord_flip()
plot1 <- structure(list(Followup = structure(c(1L, 2L, 3L, 4L, 1L, 2L, 
3L, 4L), .Label = c("Baseline", "6 months", "24 months", "48 months"
), class = "factor"), Nutrient = structure(c(1L, 1L, 1L, 1L, 
2L, 2L, 2L, 2L), .Label = c("Protein_g", "Fat_g"), class = "factor"), 
    estimate_control = c(61.00596313, 65.53883145, 73.31875608, 
    78.91867614, 67.0690134, 60.01715111, 62.63232916, 82.27888654
    ), lowCIcontrol = c(50.941157, 53.03671101, 59.93245069, 
    63.98993695, 53.7771638, 44.59304192, 45.56292816, 61.10072183
    ), upCIcontrol = c(71.07076925, 78.0409519, 86.70506146, 
    93.84741533, 80.36086301, 75.44126031, 79.70173015, 103.45705126
    ), inter_val = c(60.10474197, 60.46426229, 71.52401348, 77.26970387, 
    66.22599924, 40.25131748, 47.51238789, 74.312322), inter_lowCI = c(48.07336716, 
    45.44116548, 55.56009104, 59.53520327, 50.33704695, 21.7170307, 
    27.15616668, 49.15385617), inter_upCI = c(72.13611677, 75.48735912, 
    87.4879359, 95.00420447, 82.11495155, 58.78560428, 67.86860908, 
    99.47078784), p_Intervention = c(0.36882348, 0.00008475, 
    0.17207734, 0.24888026, 0.5243924, 0, 0, 0.00009439)), row.names = c(5L, 
6L, 7L, 8L, 13L, 14L, 15L, 16L), class = "data.frame")

谢谢您的帮助!

您也可以在同一个 plot 中进行控制和处理:

library(dplyr)
library(tidyr)
plot1 <- plot1 %>% 
  mutate(Followup = factor(Followup, levels=rev(c("Baseline", "6 months", 
                                              "24 months", "48 months")))) %>% 
  setNames(c("Followup", "Nutrient", "est_control", "low_control", "up_control", 
             "est_val", "low_val", "up_val", "p_Intervention")) 

plot1 <- plot1 %>% 
  pivot_longer(est_control:up_val, 
               names_pattern="(.*)_(.*)", 
               names_to = c("type", "treat"), 
               values_to="vals") %>% 
  pivot_wider(names_from="type", values_from="vals") %>% 
  mutate(treat = factor(treat, levels=c("val", "control"), 
                        labels=c("Treatment", "Control"))) %>% 
  mutate(sig = case_when(
    p_Intervention < .1 & p_Intervention >= .05 & treat == "Treatment" ~ "*", 
    p_Intervention < .05 & p_Intervention >= .01 & treat == "Treatment" ~ "**", 
    p_Intervention < .01 & treat == "Treatment" ~ "***", 
    TRUE ~ ""
  ))

ggplot(plot1, aes(x=Nutrient, y=est, fill=Followup)) + 
  geom_bar(stat="identity", position="dodge") + 
  geom_errorbar(aes(ymin=low, ymax=up), position=position_dodge(width=.9), width=.15) + 
  geom_text(aes(y=up + 10, label=sig), position=position_dodge(width=.9)) + 
  facet_wrap(~treat) + 
  scale_fill_brewer(palette="Set1") + 
  labs(y="Estimate", x="") +
  coord_flip() 

在此处输入图像描述


编辑:在评论中回答问题

评论中的问题是如何使用geom_text()添加估计和 CI? 答案是您首先必须创建一个包含估计值和置信区间文本的变量。 然后,您可以将该变量用作label美学。 这是一个例子:

plot1 <- plot1 %>% mutate(est_ci = sprintf("%.2f \n(%.2f, %.2f)", est, low, up))
ggplot(plot1, aes(x=Nutrient, y=est, fill=Followup)) + 
  geom_bar(stat="identity", position="dodge") + 
  geom_errorbar(aes(ymin=low, ymax=up), position=position_dodge(width=.9), width=.15) + 
  geom_text(aes(y=up + 10, label=sig), position=position_dodge(width=.9)) + 
  geom_text(aes(y=15, label=est_ci), col="white",position=position_dodge(width=.9), size=4, vjust=1) + 
  facet_wrap(~treat) + 
  scale_fill_brewer(palette="Set1") + 
  labs(y="Estimate", x="") +
  coord_flip() 

ggsave("test.png", height=8, width=12, units="in", dpi=150)

在此处输入图像描述

像这样的东西?

library(ggsignif)
plot1 = plot1 %>% 
  mutate(sig = case_when(p_Intervention < 0.01 ~ "***",
                         p_Intervention < 0.05 ~ "**",
                         p_Intervention < 0.1 ~ "*",
                         TRUE ~ ""))

ggplot(plot1, aes(factor(Nutrient), inter_val, fill = fct_rev(Followup))) + 
  geom_bar(stat="identity", position = "dodge") + 
  geom_errorbar(aes(ymin=inter_val-inter_lowCI, ymax=inter_val+inter_upCI), width=1, size=1,position="dodge") + 
  coord_flip() + 
  geom_text(aes(x=factor(Nutrient),y = inter_val+inter_upCI + 10,label=sig,group=fct_rev(Followup)),position = position_dodge(width=1)) + 
  scale_fill_brewer(palette = "Set1")

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM