Home > Software engineering >  Is ```summarize()``` contractive with ```ggplot()```
Is ```summarize()``` contractive with ```ggplot()```

Time:04-15

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.

CodePudding user response:

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
        )
      )
  }

CodePudding user response:

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.

  • Related