簡體   English   中英

使用 ggplot stat_summary 繪制 p 值

[英]plotting p-values using ggplot stat_summary

我想用系數和誤差線繪制一個數據框(統計數據),並自動在每個點上方寫入 p 值。

stats <- data.frame(Coefficient = c(-0.07,-0.04,-0.15173266),
                    p_value = c(.0765210755,0.5176050652,0.0001309025),
                    conf_low = c(-.1544418,-0.1686583,-0.2294873),
                    conf_high = c(0.007812205,0.084939487,-0.073978033),
                    Test = c("TestA","TestB","TestC"))

我正在嘗試制作一個函數來繪制每個系數點上方的 p 值。 (下圖中的 coord_flip 也可能讓我失望。

give.pval <- function(y){
  return(c(x = Coefficient, label = stats$p_value))
}

以下 ggplot 正是我所需要的,除了我做錯的 stat_summary 行

ggplot(stats, aes(x = Test, y = Coefficient)) +
  geom_point(aes(size = 6)) +
  geom_errorbar(aes(ymax = conf_high, ymin = conf_low)) +
  geom_hline(yintercept=0, linetype="dashed") +
  #stat_summary(fun.data = give.pval, geom = "text") + 
  theme_calc() +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text.x = element_text(size = 12, vjust = 0.5), axis.title.x = element_text(size = 16),
        axis.text.y = element_text(size = 12), axis.title.y = element_blank(),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 24)) +
  coord_flip() + 
  ylab("Coefficient")

我希望有這個圖,但在三個系數點中的每一個之上都有適當的 p 值。

感謝您的任何建議。

這可以用一個可實現geom_text在那里你映射層p_value上的label AES和一些額外的輕推

library(ggplot2)

stats <- data.frame(Coefficient = c(-0.07,-0.04,-0.15173266),
                    p_value = c(.0765210755,0.5176050652,0.0001309025),
                    conf_low = c(-.1544418,-0.1686583,-0.2294873),
                    conf_high = c(0.007812205,0.084939487,-0.073978033),
                    Test = c("TestA","TestB","TestC"))

ggplot(stats, aes(x = Test, y = Coefficient)) +
  geom_point(aes(size = 6)) +
  geom_errorbar(aes(ymax = conf_high, ymin = conf_low)) +
  geom_hline(yintercept=0, linetype="dashed") +
  geom_text(aes(label = p_value), nudge_x = .2) +
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank(),
        axis.text.x = element_text(size = 12, vjust = 0.5), axis.title.x = element_text(size = 16),
        axis.text.y = element_text(size = 12), axis.title.y = element_blank(),
        legend.position = "none",
        plot.title = element_text(hjust = 0.5, size = 24)) +
  coord_flip() + 
  ylab("Coefficient")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM