在ggplot2中,对于因子数据,计算的误差条不会绘制。

huangapple go评论94阅读模式
英文:

Computed error bars do not plot for factor data in ggplot2 R

问题

I am trying to plot error bars for factor data as shown in this reproducible example here. This example just runs fine. However, I tried the same thing with my original dataset, but error bars refuse to plot. I am a little lost. The following are the relevant pieces of code and as reproducible as possible. Any idea, why I cannot get the error bars to work?

Dataframe df has two columns: yaxis.og and species (4 species) like this:

  1. yaxis.og species
  2. <dbl> <fct>
  3. 1 2.56 A
  4. 2 3.81 B
  5. 3 1.48 C
  6. 4 1.65 D
  7. 5 -0.177 A
  8. .....

I computed a simple linear model and stored the results as such:

  1. summary_df <- summary(mod)$coefficients %>%
  2. as_tibble() %>%
  3. dplyr::mutate(species = c("A","B","C","D")) %>%
  4. dplyr::rename(yaxis = Estimate) %>%
  5. mutate(ymin = yaxis - 1.96 * `Std. Error`,
  6. ymax = yaxis + 1.96 * `Std. Error`)

When I plot it without error bars, it plots fine:

  1. p <- ggplot(aes(x = species, y = yaxis.og, color = species, group = species), data = df) +
  2. geom_jitter(width = 0.1) +
  3. geom_point(aes(x = species, y = yaxis), color = 'black', size = 2, data = summary_df)

When I add error bars in, it throws a huge error. The error doesn't make too much sense either because the last layer uses a different dataset, so why would it need yaxis.og?

  1. p + geom_errorbar(aes(x = species, ymin = ymin, ymax = ymax), data = summary_df)

Error in geom_errorbar():
! Problem while computing aesthetics.
ℹ Error occurred in the 3rd layer.
Caused by error in FUN():
! object 'yaxis.og' not found
Backtrace:

  1. base (local) `(x)
  2. ggplot2:::print.ggplot(x)
  3. ggplot2:::ggplot_build.ggplot(x)
  4. ggplot2:::by_layer(...)
  5. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
  6. l$compute_aesthetics(d, plot)
  7. ggplot2 (local) compute_aesthetics(..., self = self)
  8. base::lapply(aesthetics, eval_tidy, data = data, env = env)
  9. rlang (local) FUN(X[[i]], ...)
英文:

I am trying to plot error bars for factor data as shown in this reproducible example here. This example just runs fine. However, I tried the same thing with my original dataset, but errorbars refuse to plot. I am a little lost. The following are the relevant pieces of code and as reproducible as possible. Any idea, why I a cannot get the errorr bars to work?

Dataframe df has two columns: yaxis.og and species (4 species) like this:

  1. yaxis.og species
  2. &lt;dbl&gt; &lt;fct&gt;
  3. 1 2.56 A
  4. 2 3.81 B
  5. 3 1.48 C
  6. 4 1.65 D
  7. 5 -0.177 A
  8. .....

I computed a simple linear model and stored the results as such:

  1. summary_df &lt;- summary(mod)$coefficients %&gt;%
  2. as_tibble() %&gt;%
  3. dplyr::mutate(species = c(&quot;A&quot;,&quot;B&quot;,&quot;C&quot;,&quot;D&quot;)) %&gt;%
  4. dplyr::rename(yaxis = Estimate) %&gt;%
  5. mutate(ymin = yaxis - 1.96 * `Std. Error`,
  6. ymax = yaxis + 1.96 * `Std. Error`)
  7. yaxis `Std. Error` df `t value` `Pr(&gt;|t|)` species ymin ymax
  8. &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;chr&gt; &lt;dbl&gt; &lt;dbl&gt;
  9. 1 0.611 0.145 61.2 4.22 8.21e- 5 A 0.327 0.895
  10. 2 2.16 0.160 78.0 13.5 3.93e-22 B 1.85 2.47
  11. 3 1.21 0.153 68.7 7.92 2.84e-11 C 0.912 1.51
  12. 4 2.21 0.223 162. 9.89 2.43e-18 D 1.77 2.64

When I plot it without erorrbars, it plots fine:

  1. p &lt;- ggplot(aes(x = species, y = yaxis.og, color = species, group = species), data = df) +
  2. geom_jitter(width = 0.1) +
  3. geom_point(aes(x = species, y = yaxis), color = &#39;black&#39;, size = 2, data = summary_df)

在ggplot2中,对于因子数据,计算的误差条不会绘制。

When I add errorbars in, it throws a huge error. The error doesn't make to much sense either because the last layer uses a different dataset, so why would it need yaxis.og?

  1. p + geom_errorbar(aes(x = species, ymin = ymin, ymax = ymax), data = summary_df)
  2. Error in `geom_errorbar()`:
  3. ! Problem while computing aesthetics.
  4. Error occurred in the 3rd layer.
  5. Caused by error in `FUN()`:
  6. ! object &#39;yaxis.og&#39; not found
  7. Backtrace:
  8. 1. base (local) `&lt;fn&gt;`(x)
  9. 2. ggplot2:::print.ggplot(x)
  10. 4. ggplot2:::ggplot_build.ggplot(x)
  11. 5. ggplot2:::by_layer(...)
  12. 12. ggplot2 (local) f(l = layers[[i]], d = data[[i]])
  13. 13. l$compute_aesthetics(d, plot)
  14. 14. ggplot2 (local) compute_aesthetics(..., self = self)
  15. 15. base::lapply(aesthetics, eval_tidy, data = data, env = env)
  16. 16. rlang (local) FUN(X[[i]], ...)

答案1

得分: 1

问题在于ggplot()中定义的所有aes都是全局美学,即使没有使用,也会被所有geom图层继承。因此,ggplot2期望每个geom图层使用的数据中应该有一个名为yaxis.og的列,除非你像在geom_point中那样覆盖全局aes。

解决这个问题的一种方法是在geom_errorbar中添加inherit.aes=FALSE,这会阻止继承全局aes,或者通过使用aes(..., y = NULL)来覆盖全局aes。

使用基于iris的一些示例数据:

  1. library(ggplot2)
  2. p +
  3. geom_errorbar(
  4. aes(x = species, ymin = ymin, ymax = ymax, y = NULL),
  5. data = summary_df
  6. )

在ggplot2中,对于因子数据,计算的误差条不会绘制。

  1. p +
  2. geom_errorbar(
  3. aes(x = species, ymin = ymin, ymax = ymax),
  4. data = summary_df, inherit.aes = FALSE
  5. )

在ggplot2中,对于因子数据,计算的误差条不会绘制。

数据

  1. set.seed(123)
  2. df <- data.frame(
  3. yaxis.og = c(iris$Sepal.Length, sample(iris$Sepal.Length, 50)),
  4. species = rep(c("A", "B", "C", "D"), each = 50)
  5. )
  6. mod <- lm(yaxis.og ~ species - 1, data = df)
英文:

The issue is that all aes defined in ggplot() are global aesthetics and will inherited by all geom layers even if not used. As a result ggplot2 expects a column with name yaxis.og to be present in the data used by each geom layer, except in case you override the global aes as you do e.g. in geom_point.

One option to fix that would be add inherit.aes=FALSE to geom_errorbar which prevents the global aes to be inherited or to override the global aes by using aes(..., y = NULL).

Using some fake example data based on iris:

  1. library(ggplot2)
  2. p +
  3. geom_errorbar(
  4. aes(x = species, ymin = ymin, ymax = ymax, y = NULL),
  5. data = summary_df
  6. )

在ggplot2中,对于因子数据,计算的误差条不会绘制。<!-- -->

  1. p +
  2. geom_errorbar(
  3. aes(x = species, ymin = ymin, ymax = ymax),
  4. data = summary_df, inherit.aes = FALSE
  5. )

在ggplot2中,对于因子数据,计算的误差条不会绘制。<!-- -->

DATA

  1. set.seed(123)
  2. df &lt;- data.frame(
  3. yaxis.og = c(iris$Sepal.Length, sample(iris$Sepal.Length, 50)),
  4. species = rep(c(&quot;A&quot;, &quot;B&quot;, &quot;C&quot;, &quot;D&quot;), each = 50)
  5. )
  6. mod &lt;- lm(yaxis.og ~ species - 1, data = df)

huangapple
  • 本文由 发表于 2023年5月8日 00:22:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76195035.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定