Referencing piped dataset in ggplot layers for subsetting

Viewed 927

Trying to find a way to reference different parts of the dataset for different ggplot2 geom layers without having to save the dataset first in the global environment.

Ex non working solution.

read_excel("Book1.xlsx",sheet = "Sheet2") %>% 
  ggplot(aes(x,y)) +
  geom_col() +
  geom_point(data=subset($ID == "1"),colour="red")

Above does not seem to work since i am not referencing the piped(magrittr) dataset in a way that R can recognize it. I have searched but the only solutions i could see are based of the one where i first save the dataset in the global environment as such

Ex working solution with saving the dataset first.

df <- read_excel("Book1.xlsx",sheet = "Sheet2")

  ggplot(df,aes(x,y)) +
  geom_col() +
  geom_point(data=subset(df,ID == "1"),colour="red")
1 Answers
Related