簡體   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