Hi Everyone,
I'm using ggplot on a dataframe to make a histogram, faceted by a variable that has two levels. I will explain my problem and then add reproducible code below.
When I call aes(x = variable)..., everything is okay (see the correct.pdf attached). When I call aes(x= df$variable) or aes(x= df[,col]), I get the incorrect graph (see incorrect.pdf). In the incorrect pdf, the legend and header label are mixed up (legend has labeled pink as a when it is showing b data and header label labels a as b and vice versa). Also, this problem only happens if my factor is not in alphabetical order. If it is in alphabetical order, the legends and labels match up okay with the data.
I have seen in other posts that calling df$variable is bad form if I've already specified the df. However, I would like to use df[,col] in a loop where I am looping through certain columns to plot. I guess my question is why am I getting this error? I am just worried that I may be plotting other things (under other circumstances) and I won't catch the flip flop. Does this mean I just should not/cannot use df[,col] at all?
Below is my example of reproducible code (but it does not include multiple columns for simplicity). I'm sorry if the above was confusing, this is my first time posting to a forum for help!
##test data for my plots
a = rnorm(10)
b = rnorm(10,10)
data = cbind(b,a) # if you cbind(a,b), then the error won't occur
d_melt = melt(data)
names(d_melt)[2] = "type"
plot(ggplot(d_melt, aes(x=value, fill=type)) + geom_histogram()+ facet_wrap(~type))
plot(ggplot(d_melt, aes(x=d_melt[,3], fill=type)) + geom_histogram()+ facet_wrap(~type))