I have been trying to plot simple density plots using R as:
plot(density(Data$X1),col="red")
plot(density(Data$X2),col="green")
Since I want to compare, I’d like to plot both in one figure. But ‘matplot’ doesn’t work!! I also tried with ggplot2 as:
library(ggplot2)
qplot(Data$X1, geom="density")
qplot(Data$X2, add = TRUE, geom="density")
Also in this case, plots appear separately (though I wrote add=TRUE)!! Can anyone come up with an easy solution to the problem, please?
In
ggplot2orlatticeyou need to reshape the data to seupose them.For example :
Using “lattice`
Using “ggplot2`
EDIT add X1+X2
Using lattice and the extended formua interface, it is extremely easy to do this: