简体   繁体   English

使用 ggplot stat_summary 绘制 p 值

[英]plotting p-values using ggplot stat_summary

I want to plot a dataframe (stats) with the coefficient and error bars, and automatically write the p-values above each point.我想用系数和误差线绘制一个数据框(统计数据),并自动在每个点上方写入 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"))

I am trying to make a function to plot the p-values above each Coefficient point.我正在尝试制作一个函数来绘制每个系数点上方的 p 值。 (The coord_flip in the plot below may also be throwing me off. (下图中的 coord_flip 也可能让我失望。

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

The following ggplot is exactly what I need, except for the stat_summary line which I am doing incorrectly以下 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")

I would like the have this plot but with the appropriate p-value above each of the three Coefficient points.我希望有这个图,但在三个系数点中的每一个之上都有适当的 p 值。

Thanks for any advice.感谢您的任何建议。

This could be achieved with a geom_text layer where you map p_value on the label aes and some additional nudging这可以用一个可实现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