Data Analysis

How to plot a variable in R that has spaces it it?

There are thousands of datasets on the web available for analysis using R. Many of them are listed by the plus or minus 175 countries, like “United States” or “Cote d’Ivoire”

So, ignoring for a moment that the experts say never name a variable with spaces, in the real world how do you plot a variable with spaces in its name?

Simple. When programming, encase the variable name with backticks. Like so: `United States`

Example: see below the line with, y=`Costa Rica`

ggplot(df, aes(Year, group=1)) +
ylab(“Country”) +
geom_line(aes(y = `Costa Rica`, color=”Costa Rica”)) +
geom_point(aes(y = Belgium, color=”Belgium”))

r-Peter_Bakke