是``总结()

发布于 2025-01-21 05:15:19 字数 1003 浏览 0 评论 0原文

大家好,这里是一个示例代码

library(palmerpenguins)
mypenguins = penguins %>% drop_na()
penguins_df <- mypenguins %>% 
  group_by(species) %>% 
  summarize(mean_g = mean(bill_length_mm)) 
penguins_df %>% 
  ggplot(aes(x = mean_g, y = species)) +
  geom_col(width = 0.5) +
  theme(
    axis.text.y = element_text(
      color = if_else(penguins_df$mean_g > 48, "red", "black"), 
      face = if_else(penguins_df$mean_g  > 48, "bold", "plain"), 
      size = 20
    )
  )

,但是当我想整合两个部分“汇总”和“ ggplot”时,

penguins_df <- mypenguins %>% 
  group_by(species) %>% 
  summarize(mean_g = mean(bill_length_mm)) %>%
  ggplot(aes(x = mean_g, y = species)) +
  geom_col(width = 0.5) +
  theme(
    axis.text.y = element_text(
      color = if_else(mean_g > 48, "red", "black"), 
      face = if_else(mean_g  > 48, "bold", "plain"), 
      size = 20
    )
  )

我被警告了:“ nare_g”找不到,但是我确实定义了“ sane_g”,这使我感到非常困惑。 有人会介意提供一些建议吗?谢谢。

Hello everyone Here is one example code

library(palmerpenguins)
mypenguins = penguins %>% drop_na()
penguins_df <- mypenguins %>% 
  group_by(species) %>% 
  summarize(mean_g = mean(bill_length_mm)) 
penguins_df %>% 
  ggplot(aes(x = mean_g, y = species)) +
  geom_col(width = 0.5) +
  theme(
    axis.text.y = element_text(
      color = if_else(penguins_df$mean_g > 48, "red", "black"), 
      face = if_else(penguins_df$mean_g  > 48, "bold", "plain"), 
      size = 20
    )
  )

However when I want to integrate two parts "summarize" and "ggplot" like this

penguins_df <- mypenguins %>% 
  group_by(species) %>% 
  summarize(mean_g = mean(bill_length_mm)) %>%
  ggplot(aes(x = mean_g, y = species)) +
  geom_col(width = 0.5) +
  theme(
    axis.text.y = element_text(
      color = if_else(mean_g > 48, "red", "black"), 
      face = if_else(mean_g  > 48, "bold", "plain"), 
      size = 20
    )
  )

I was warned: "mean_g" not found, but I do define "mean_g" before, which confused me a lot.
Would someone mind offering some advice kindly? Thanks.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤凫 2025-01-28 05:15:19

我们可以使用卷曲牙套的围绕GGPLOT表达式的“技巧”。唯一的缺点是我们需要用点明确指的是管道对象。

这是代码:

library(tidyverse)
library(palmerpenguins)


mypenguins <- penguins %>% drop_na()

penguins_df <- mypenguins %>%
  group_by(species) %>%
  summarize(mean_g = mean(bill_length_mm))

penguins_df %>%
  {
    ggplot(., aes(x = mean_g, y = species)) +
      geom_col(width = 0.5) +
      theme(
        axis.text.y = element_text(
          color = if_else(.$mean_g > 48, "red", "black"),
          face = if_else(.$mean_g > 48, "bold", "plain"),
          size = 20
        )
      )
  }

We can use the "trick" of surrounding the ggplot expression with curly braces. The only downside is that we'll need to explicitly refer to the piped object with a dot.

Here's the code:

library(tidyverse)
library(palmerpenguins)


mypenguins <- penguins %>% drop_na()

penguins_df <- mypenguins %>%
  group_by(species) %>%
  summarize(mean_g = mean(bill_length_mm))

penguins_df %>%
  {
    ggplot(., aes(x = mean_g, y = species)) +
      geom_col(width = 0.5) +
      theme(
        axis.text.y = element_text(
          color = if_else(.$mean_g > 48, "red", "black"),
          face = if_else(.$mean_g > 48, "bold", "plain"),
          size = 20
        )
      )
  }
剑心龙吟 2025-01-28 05:15:19

评论太久了。

ggplot(AES(...))定义的美学,仅适用于GEOM s,而不是theme。这就是为什么这不起作用。

因此,例如,您可以编写:

mypenguins <- penguins %>% drop_na()
mypenguins %>% 
  group_by(species) %>% 
  summarize(mean_g = mean(bill_length_mm)) %>%
  ggplot(aes(x = mean_g, y = species)) +
  geom_col(width = 0.5)

这将起作用,因为mean_g是一种美学,因此ggplotdata.frame (它查找Mean_g列)。 主题(...)函数不尊重美学,因此它在调用环境中查找mean_g,并且由于没有这样的对象而失败。

Too long for a comment.

The aesthetics defined with ggplot(aes(...)), apply to geoms only, not to the theme. That's why this doesn't work.

So for instance, you can write:

mypenguins <- penguins %>% drop_na()
mypenguins %>% 
  group_by(species) %>% 
  summarize(mean_g = mean(bill_length_mm)) %>%
  ggplot(aes(x = mean_g, y = species)) +
  geom_col(width = 0.5)

This will work because mean_g is an aesthetic, so ggplot looks for it in the context of the data.frame (it looks for the mean_g column). The theme(...) function does not respect aesthetics, so it looks for mean_g in the calling environment, and fails because there is not such object.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文