I wish to create a density plot on qplot()
comprising three response variables. So the graph will be Density (y) against Elevation (x), with three colour-coded density functions showing how densities of each change as Elevation (x-axis) changes.
First I subsetted the three response variables (3 columns in my dataset "CAIRNGORM") into a small subset called "ZONES":
ZONES<-CAIRNGORM[c("prop_Cal", "prop_Emp", "prop_Jun")]
Then I tried to create the qplot:
library(ggplot2)
qplot(Elevation, data=CAIRNGORM, geom="density", fill="ZONES", alpha=I(0.5))
which creates a plot but instead of giving me three traces, one for each of prop_Cal, prop_Emp and prop_Jun, I just have one trace and it appears to show the density of my Elevation data - a straight line!
I would really appreciate somebody's help with this - how do I instruct qplot to build three traces contained within "ZONES" instead of the x-variable? Thanks
Edit: Shortened version of my data (trying to put correct code formatting in Stack Overflow):
> head(CAIRNGORM)
position group Elevation
1 Q1 A 680
2 Q2 A 730
3 Q3 A 780
4 Q4 A 830
5 Q5 A 880
6 Q6 A 930
prop_bar prop_Cal prop_Vac prop_Emp prop_Jun prop_Ces prop_Eri ZONES.prop_Cal
1 0.00 1.00 0.0 0 0 0.36 0.4 1.00
2 0.00 1.00 0.0 0 0 0.28 0.0 1.00
3 0.00 0.84 0.6 0 0 0.48 0.0 0.84
4 0.00 1.00 0.0 0 0 0.00 0.0 1.00
5 0.24 0.76 0.0 0 0 0.72 0.0 0.76
6 0.36 0.72 0.0 0 0 0.00 0.0 0.72
ZONES.prop_Emp ZONES.prop_Jun
1 0 0
2 0 0
3 0 0
4 0 0
5 0 0
6 0 0
> head(ZONES)
prop_Cal prop_Emp prop_Jun
1 1.00 0 0
2 1.00 0 0
3 0.84 0 0
4 1.00 0 0
5 0.76 0 0
6 0.72 0 0