R: How to : 3d Density plot with gplot and geom_density

后端 未结 3 513
轻奢々
轻奢々 2020-12-14 23:21

I\'m trying to combine multiple density plots with overlay. ggplot and geom_density do the job, but the densities are stacked on top of each other.

3条回答
  •  醉梦人生
    2020-12-14 23:52

    As @jlhoward mentioned, using facets could work, or using subplots, but either option doesn't scale well with a large number of groups. Consider using an ecdf plot instead.

    Without the data in your object all.complete, I can't recreate your plot, so here is a simplified example:

    library(ggplot2)
    
    ggplot(iris, aes(x = Sepal.Length)) +  
      geom_density(aes(group = Species, 
                       colour = Species, 
                       fill = Species),
                   alpha = 0.2)
    

    For more than a couple groups, I've found ecdf plots to be much easier to interpret. To make a similar plot:

     ggplot(iris, aes(x = Sepal.Length)) +  
      stat_ecdf(aes(color = Species))
    

    You can have dozens of ecdf plots on the same plot, and since they are just lines they are still separate enough to view. Density plots or histograms would be too overlapped, as in your example.

    This is the blog post that got me to start using ecdf plots and has more info about them: http://allendowney.blogspot.com/2013/08/are-my-data-normal.html

提交回复
热议问题