0

I am plotting data using dplyr, ggplot2, and ggrepel. I would like to use ggrepel to only label certain points in my dataset. I usually accomplish this by subsetting the data within the geom_text_repel() layer, but I have not found a way to make this work with changes made in the pipe. For example, I can plot mpg vs. qsec and label those cars with qsec>=18 with their number of gears as follows:

mtcars %>% 
  ggplot(aes(x=mpg,y=qsec,label=gear))+
  geom_point()+
  geom_text_repel(data=subset(mtcars, qsec>=18))

Since I did not modify the original dataset, using the subset() function within geom_text_repel() works just fine.

But say I wish to make some new variable indicating if the engine is small or large, and have this new variable be my label, such as:

mtcars %>% 
  mutate(eng_size=ifelse(cyl>=6,"large","small")) %>% 
  ggplot(aes(x=mpg,y=qsec,label=eng_size))+
  geom_point()+
  geom_text_repel(data=subset(mtcars, qsec>=18))

I get the error: "Error in FUN(X[[i]], ...) : object 'eng_size' not found", presumably since the dataset I am subsetting, mtcars, dows not have a column 'eng_size' since I created that column in the pipe with mutate(). Does anyone know how I can solve such problems without modifying the original dataframe as shown below?

mtcars$eng_size<-ifelse(mtcars$cyl>=6,"large","small")

mtcars %>% 
  ggplot(aes(x=mpg,y=qsec,label=eng_size))+
  geom_point()+
  geom_text_repel(data=subset(mtcars,qsec>=18))

Thanks!

0 Answers0