【问题标题】:Residual plot with ggplot with X-axis as "ranked" residuals带有 ggplot 的残差图,X 轴为“排序”残差
【发布时间】:2019-01-25 06:17:30
【问题描述】:

我正在尝试在 ggplot: 中重新创建这样的情节。

此图从回归输出中获取残差,并按顺序绘制它们(X 轴是残差的等级)。

我对此的最佳尝试如下:

library(ggplot2)
library(modelr)

d <- d %>% add_residuals(mod1, var = "resid")
d$resid_rank <- rank(d$resid)

ggplot(data = d, aes(x = resid_rank, y = resid)) +
  geom_bar(stat="identity") +
  theme_bw()

但是,这会产生一个完全空白的图表。我试过这样的事情:

ggplot(data = d, aes(x = resid_rank, y = resid)) +
  geom_segment(yend = 0, aes(xend=resid)) +
  theme_bw()

但这会产生方向错误的细分。这样做的正确方法是什么,并通过第三个因素为这些线条着色?

假数据集:

library(estimatr)
library(fabricatr)

#simulation
dat <- fabricate(
  N = 10000,
  y =  runif(N, 0, 10),
  x = runif(N, 0, 100)
)

#add an outlier
dat <- rbind(dat, c(300, 5))
dat <- rbind(dat, c(500, 3))

dat$y_log <- log(dat$y)
dat$x_log <- log(dat$x)
dat$y_log_s <- scale(log(dat$y))
dat$x_log_s <- scale(log(dat$x))

mod1 <- lm(y_log ~ x_log, data = dat))

【问题讨论】:

  • 可以添加示例数据(dput(d))吗?
  • 我注意到你为残差创建了一个名为 resid 的变量,但是你在 d$resid_rank 上调用了 rank,不应该是 rank(d$resid) 吗?
  • 好收获。这只是试图将所有变量名称更改为通用名称的错误格式。 @PoGibas,不幸的是数据是专有的,但我可以制作一个模拟数据集。

标签: r ggplot2


【解决方案1】:

我使用来自lm() 帮助页面的内置数据集来创建此示例。我也只是直接使用resid() 来获取残差。目前还不清楚彩色条在哪里/为什么会有所不同,但基本上您需要在 data.frame 中添加一列,说明它们为什么是红色或蓝色,然后将其传递给fill。

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 3.4.4
#example from lm
ctl <- c(4.17,5.58,5.18,6.11,4.50,4.61,5.17,4.53,5.33,5.14)
trt <- c(4.81,4.17,4.41,3.59,5.87,3.83,6.03,4.89,4.32,4.69)
group <- gl(2, 10, 20, labels = c("Ctl","Trt"))
weight <- c(ctl, trt)
lm.D9 <- lm(weight ~ group)
resids <- data.frame(resid = resid(lm.D9))
#why are some bars red and some blue? No clue - so I'll pick randomly
resids$group <- sample(c("group 1", "group 2"), nrow(resids), replace = TRUE)
#rank
resids$rank <- rank(-1 * resids$resid)


ggplot(resids, aes(rank, resid, fill = group)) +
  geom_bar(stat = "identity", width = 1) +
  geom_hline(yintercept = c(-1,1), colour = "darkgray", linetype = 2) +
  geom_hline(yintercept = c(-2,2), colour = "lightgray", linetype = 1) +
  theme_bw() +
  theme(panel.grid = element_blank()) +
  scale_fill_manual(values = c("group 1" = "red", "group 2" = "blue"))

由reprex package (v0.2.1) 于 2019-01-24 创建

【讨论】:

  • 非常感谢!当我在我的真实数据集上运行它时,为什么没有条形图出现,但如果我使用 geom_point() 而不是 geom_bar(),点数会出现吗?
  • @Parseltongue - 不幸的是,没有。或者至少不查看您的实际数据的str(),这听起来像是专有的。您可以先比较我的示例中的str(),然后确定与您的数据相比有何不同。如果您设置了限制,即ylim(c(-.1, .1)),您将切断许多残差,但其中 一些 仍应显示。
  • 非常感谢您的帮助!看看有没有帮助
  • 奇怪的是,问题似乎是由两个因素引起的:scale_fill_manual(values = c("group 1" = "red", "group 2" = "blue")) 将强制我的数据不呈现任何内容。而且因为我的实际数据集是 255,000 个观测值,所以 ggplot 似乎不会以条形图的形式绘制那么多数据点。似乎我还需要删除所有缺失值才能正确处理 xlim
  • @Parseltongue - 那条线是红色和蓝色条的颜色...如果您没有名为“组”的变量并且该列中的两个唯一条目不是“组 1” " 和 "group 2",那么您将遇到问题,因为 ggplot 将红色和蓝色应用于第 1 组和第 2 组。如果您没有要着色的变量,只需注释掉/删除该行。目前尚不清楚您原始帖子中红色/蓝色的含义,所以我随机编造了一些东西(参见上面的代码sample())
猜你喜欢
  • 2020-10-22
  • 1970-01-01
  • 2015-07-26
  • 2020-03-25
  • 1970-01-01
  • 2016-08-12
  • 2023-03-26
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多