Overlapped density plots in ggplot2

后端 未结 3 2044
梦毁少年i
梦毁少年i 2020-12-18 02:25

Imagine I have two vectors each of different length. I want to generate one plot with the density of both vectors overlaid. What I thought I should do is this:



        
3条回答
  •  盖世英雄少女心
    2020-12-18 03:03

    Try this if you want have legends too:

    df <- rbind(data.frame(x=rnorm(2000, 0, 1), vec='1'),
                data.frame(x=rnorm(3000, 1, 1.5), vec='2'))
    ggplot(df, aes(x, group=vec, col=vec)) + geom_density(position='dodge')
    

提交回复
热议问题