r - 突出显示 ggplot2 stat_qq 输出中的点

标签 r ggplot2

我试图根据 ggplot stat_qq 输出中的顺序统计信息突出显示选定的点:

ydata <- data.frame(sample = c(rep("Sample 1", 100),
                               rep("Sample 2", 100),
                               rep("Sample 3", 100),
                               rep("Sample 4", 100)),
                               x=rnorm(400))

ydata <- ydata[order(ydata$sample, ydata$x),]
ydata$order <- 1:100

ggplot(ydata, aes(sample=x)) + stat_qq() + facet_wrap(~sample) + scale_x_continuous(breaks = -2:2, labels = function(x) paste0(x, " \n [",100 * signif(pnorm(-2:2, lower.tail=FALSE),2), "%]")) + theme_bw(base_size = 14, base_family = "sans") + labs(title = "Four Samples of 100 Observations From Normal Distribution", 
       caption = "4 Samples of n = 100 from Normal Distribution \nNumbers indicate order of value",
       y = "Sample Value",
       x = "Standard Deviation\n[%exceeding]") +  
  geom_text(data = ydata[ydata$order %in% c(2,16,50,84,98),], aes(x=qnorm(pnorm(x)), y=x, label = order), nudge_y = 1)

产生了这个:

enter image description here

显然我的文本符号没有突出显示正确的点(第 2、16、50、84、98 个点)。我希望我也能用红色突出显示实际的点。如有任何建议,我们将不胜感激。

最佳答案

您可以在 ggplot 之外计算 qq 值,并创建一个单独的列,将 qq 值分组为突出显示和未突出显示。然后,您可以使用 geom_point 绘制它们,并将分组变量作为颜色美感。例如:

library(tidyverse)

# Generate data reproducibly
set.seed(2)
ydata <- data.frame(sample = c(rep("Sample 1", 100),
                               rep("Sample 2", 100),
                               rep("Sample 3", 100),
                               rep("Sample 4", 100)),
                    x=rnorm(400))

ydata <- ydata[order(ydata$sample, ydata$x),]
ydata$order <- 1:100

# Quantile indices to highlight
pts = c(2,16,50,84,98)

# Add qq values and grouping column to data frame and pipe into ggplot
# Use split and map to calculate the qq values separately for each Sample
split(ydata, ydata$sample) %>% 
  map_df(~ .x %>% mutate(xq = qqnorm(x, plot.it=FALSE)$x,
                         group = ifelse(order %in% pts, "A", "B"))) %>% 
  ggplot(aes(xq, x, colour=group)) +
  geom_point(size=1) +
  geom_text(aes(label=ifelse(group=="A", order, "")), 
            nudge_y=1, size=3) +
  facet_wrap(~ sample) +
  theme_bw(base_size = 14, base_family = "sans") +
  scale_colour_manual(values=c("red", "black")) +
  guides(colour=FALSE)

enter image description here

作为替代方案,一种快速破解方法是使用 ggplot_build 来突出显示原始图中的特定点(但请注意,相对于突出显示的点放置标签的方式不太正确) ):

pts = rep(c(2,16,50,84,98), 4) + rep(seq(0,300,100), each=5)

# Assuming you've assigned your plot to the object p
pb = ggplot_build(p)

# Change point colors
pb$data[[1]][pts, "colour"] = "red"

# Change label colors
pb$data[[2]][["colour"]] = "red"

# Regenerate plot object
p = ggplot_gtable(pb)
plot(p)

enter image description here

关于r - 突出显示 ggplot2 stat_qq 输出中的点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48479018/

相关文章:

r - 如何根据在ggplot中分配的概率对单词重新排序

r - 在 "."中的 `fread` 中使用 sep = "data.table"

在 R 中使用鲁棒错误复制 Stata Probit

r - Terra 从分类栅格中提取不正确的值

r - Sweave 似乎没有正确获得 .Rnw 文件编码

r - 无法使用 labeller = label_parsed 将分面标签设置为斜体

ggplot2 中的圆角?

r - ShinyManager 身份验证屏幕不会超时

r - geom_rect 和 alpha - 这是否适用于硬编码值?

r - ggplot错误的颜色分配