How to properly use variable in ggplot?

2019-08-23 12:08发布

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)

Without $ in the variable name

# 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)

enter image description here

1条回答
做自己的国王
2楼-- · 2019-08-23 12:49

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

查看更多
登录 后发表回答