One Solution to “no symbol named ‘x’ in scope” When Referencing Column Names in ggplot()

Column names “displ” and “hwy” not in scope. The code executes correctly, but the warning scorches my eyes.

Since I just started to learn R, I wanted to do as many things correctly as possible the first time. Therefore, I turned on all the diagnostics in RStudio, including “Warn if variable used has no definition in scope”, which by default was not selected.

Five minutes later, I ran into a problem.

I was following the code on this reference. Upon copying ggplot(mpg) + geom_point(aes(displ, hwy)) to the diagnostics-enabled RStudio text editor, I got this warning:

no symbol named ‘displ’ in scope

no symbol named ‘hwy’ in scope

This was understandable, as these two symbols have not been declared on the script. Interestingly, since R uses non-standard evaluation to obtain column names without the need to declare them, the names not being in scope would not raise any error when the script is executed.

This was apparently useful for quick scripting or playing around on the console, but I did not like it, as it could lead to spaghetti code down the line whether a user chooses to disable the diagnostics or ignore the warnings. Two possible solutions were suggested in this article. Unfortunately, the .$displ method didn’t work for me, and the var(displ, hwy) method was too clunky (I didn’t even try it).

Having no prior knowledge of how R’s syntax works with data frame, just from the notation of .$displ, I guessed it was trying to reference column displ from the data frame. Since it didn’t work for me initially, I guessed perhaps I needed to specify which data frame displ came from. So I tried mpg.$displ, but nope, RStudio warned that “no symbol named ‘mpg.’ in scope”. Then, purely out of luck and curiosity, I tried mpg$displ, and viola! It worked!

In other words, the following code executed as expected and did not trigger “not in scope” warning from RStudio.

I am not sure whether this is already a common practice in the R community, or whether this will work in other formula/functions. But so far, it works for ggplot and is a solution I can live with for now.

Hi, I am from the Earth. And you?

Hi, I am from the Earth. And you?