How to properly use variable in ggplot?

被刻印的时光 ゝ 提交于 2019-12-24 21:31: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)

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



来源:https://stackoverflow.com/questions/47860112/how-to-properly-use-variable-in-ggplot

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!