How to properly use variable in ggplot?

2019-08-23 11:56发布

问题:

I have found an issue, that I was unable to understand. Can someone please point to an explanation?

In ggplot, if I use/don't use "$" with variable name , it gives different result. Please see the example below,

library(ggplot2)
df <- read.csv("pseudo_facebook.tsv", sep = '\t')

# Without $ sign
ggplot(data = df, aes(x = friend_count)) + geom_histogram(binwidth = 25) +
  scale_x_continuous(limits = c(1, 1000), breaks = seq(0, 1000, 25)) + 
  facet_grid(~df$gender)

# With $ sign
ggplot(data = df, aes(x = df$friend_count)) + geom_histogram(binwidth = 25) +
  scale_x_continuous(limits = c(1, 1000), breaks = seq(0, 1000, 25)) + 
  facet_grid(~df$gender)

回答1:

I'm not sure if this is what's causing your behaviour, but in the first example, you still have df$ in the facet_grid formula. It's possible there's some sneaky evaluation problem going on if you're mixing bare column names on their own with column names specified with the data frame.

If you switch out that filename in read.csv with a URL, you'll have a reprex that I can test