SS_11
SS_11

Reputation: 49

multiple Density plot using R

I am thinking about plotting multiple density plots on a single graph.My situation is this: I have a query result that has three different columns

CustomerID  Group  AvgVisitsperday
1             grp1        10
2             grp1        20
3             grp2        30
4             grp3        40
5             grp2        15
6             grp2         5

I have around 50,000 rows of data. I want to plot 3 density plots with three different colors that would represent the 3 distint groups and their average visits on a single graph..I feel that the density plot would be ideal to represent the distribution..Any ideas or thoughts on this, please feel free to comment. I have been able to do one density plot using R but not able to do group wise.. Please help!

Upvotes: 2

Views: 2907

Answers (2)

IRTFM
IRTFM

Reputation: 263301

It's reasonably easy to use the output of density with base graphics. The estimate comes out with $x and $y components, so plotting one, then adding the others with lines works quite well.

plot(density( subset(dfrm, Group=='GrpA', AvgVisitsperday), na,rm=TRUE) )  
with( density( subset(dfrm, Group=='GrpB', AvgVisitsperday), na,rm=TRUE) ,
      lines(x,y)
with( density( subset(dfrm, Group=='GrpC', AvgVisitsperday), na,rm=TRUE) ,
      lines(x,y)

With lattice:

require(lattice)
densityplot( ~ AvgVisitsperday, groups=Group, data=dfrm)

Upvotes: 1

Señor O
Señor O

Reputation: 17412

I'd recommend the ggplot2 package:

ggplot(df, aes(x=AvgVisitsperday, color=Group)) + stat_density(fill=NA)

Upvotes: 1

Related Questions