Search code examples
rdataframeprobability-density

Generating only one density graph for each group of user - R


I have a binary data frame, which each row represents data related to a user (size of data frame :90 rows * 65 cols). The last column of this data frame contains the label for the users (4 labels :Excellent, Good, bad, fail).

My question is, how can I plot only one density curve for each label. I mean, My final plot would have only 4 curves (each curve corresponding to each label).

Thanks


Solution

  • I think that this question can be found here (Multiple Groups in geom_density() plot) so my answer is almost exactly the same.

    The only difference is that I used mtcars with an extra column :

    library(ggplot2)
    
    test <- head(mtcars)
    addcol <- c("great", "good", "bad", "great", "bad", "good")
    test <- cbind(test, addcol)
    
    ggplot() + 
      geom_density(data = test, aes(x = wt, group = addcol, color = addcol), adjust=2) + 
      xlab("wt") +
      ylab("Density")