How to scale density plots (for several variables) in ggplot having melted data

前端 未结 2 1180
南方客
南方客 2021-02-20 10:53

I have a melted data set which also includes data generated from normal distribution. I want to plot empirical density function of my data against normal distribution but the sc

2条回答
  •  有刺的猬
    2021-02-20 11:37

    Is this what you had in mind?

    enter image description here

    There's a built-in variable, ..scaled.. that does this automatically.

    set.seed(1)
    df<-data.frame(type=rep(c('A','B'),each=100),x=rnorm(200,1,2)/10,y=rnorm(200))
    df.m<-melt(df)
    ggplot(df.m) + 
      stat_density(aes(x=value, y=..scaled..,color=variable), position="dodge", geom="line")
    

提交回复
热议问题