-1

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

Ester Silva
  • 670
  • 6
  • 24

1 Answers1

1

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")
bretauv
  • 7,756
  • 2
  • 20
  • 57
  • Thanks @bretauv. It worked well. In my data frame, the name of the columns (except the last one) presents the dates. How I can use the column names as the x-axis? (I means, I want to re-scale the x-axis and use the column names for the ticks and labels of the tick of the plot – Ester Silva Jun 27 '19 at 14:32
  • I don't understand your question : in the example I made, wt represents a unique column so if I put the column name on the x-axis, there will necessarily be a unique column name. Maybe you should post another question with a reproducible example (or edit this question as @MrFlick suggested) so that we can precisely see what you want – bretauv Jun 27 '19 at 15:08
  • Sorry for my mistake @bretauv. You are right. Now, my x-axis is scaled from 0 to 1, which instead I want to use dates which are in one column. How it can be? Thanks – Ester Silva Jun 27 '19 at 15:15
  • I never used dates in R, but check here (https://stackoverflow.com/questions/11748384/formatting-dates-on-x-axis-in-ggplot2/11748820#11748820), that should help you – bretauv Jun 27 '19 at 15:18