I have a dataframe d
like this
head(d)
Id Group Index
1 ID_key1 F 38.69361
2 ID_key2 A 34.01609
3 ID_key3 A 31.21704
4 ID_key4 A 30.72222
5 ID_key5 F 39.52463
6 ID_key6 A 28.98853
and I want to plot a density plot of the variable Index grouping observations by the variable Group
I did it using the following code
ggplot(d, aes(x= Index)) + geom_density() + facet_grid(Group~ .)
and I obtained this density plot
However, I noticed that if I use the dollar $
to select the data for the plot within the previous line of code, like this
ggplot(d, aes(x = d$Index)) + geom_density() + facet_grid(d$Group~ .)
the resulting graph is totally different:
I always thought that the two forms were equivalent. Why there is this difference between the two plots?