{"id":412,"date":"2014-01-01T22:15:20","date_gmt":"2014-01-02T03:15:20","guid":{"rendered":"http:\/\/homepages.uc.edu\/~yaozo\/wordpress\/?p=412"},"modified":"2014-01-01T22:15:20","modified_gmt":"2014-01-02T03:15:20","slug":"ggplot2-plotting-two-or-more-overlapping-density-plots-on-the-same-graph","status":"publish","type":"post","link":"https:\/\/zhuoyao.net\/index.php\/2014\/01\/01\/ggplot2-plotting-two-or-more-overlapping-density-plots-on-the-same-graph\/","title":{"rendered":"ggplot2: Plotting two or more overlapping density plots on the same graph"},"content":{"rendered":"<div id=\"content\">\n<div id=\"post-94\">\n<p><a href=\"http:\/\/onertipaday.blogspot.com\/2007\/09\/plotting-two-or-more-overlapping.html\">One R Tip A Day<\/a>\u00a0uses a custom R function to plot two or more overlapping density plots on the same graph.<\/p>\n<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" alt=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/3densities.gif?w=400&amp;h=400\" src=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/3densities.gif?w=400&amp;h=400\" width=\"400\" height=\"400\" \/><\/div>\n<p>The same can be very easily accomplished in\u00a0<a href=\"http:\/\/had.co.nz\/ggplot2\/\">ggplot2<\/a>.<\/p>\n<p>&nbsp;<\/p>\n<table width=\"100%\" border=\"0\" bgcolor=\"#e8e8e8\">\n<tbody>\n<tr>\n<td>\n<pre>&gt; library(ggplot2)<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Create dummy dataframe:<\/p>\n<table width=\"100%\" border=\"0\" bgcolor=\"#e8e8e8\">\n<tbody>\n<tr>\n<td>\n<pre>&gt; df &lt;- data.frame(x = rnorm(1000, 0, 1), y = rnorm(1000,\n     0, 2), z = rnorm(1000, 2, 1.5))<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u201cMelt\u201d data:<\/p>\n<table width=\"100%\" border=\"0\" bgcolor=\"#e8e8e8\">\n<tbody>\n<tr>\n<td>\n<pre>&gt; df.m &lt;- melt(df)<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>Default plot:<\/p>\n<table width=\"100%\" border=\"0\" bgcolor=\"#e8e8e8\">\n<tbody>\n<tr>\n<td>\n<pre>&gt; ggplot(df.m) + geom_freqpoly(aes(x = value,\n     y = ..density.., colour = variable))<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" alt=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/density3.png?w=416&amp;h=415\" src=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/density3.png?w=416&amp;h=415\" width=\"416\" height=\"415\" \/><\/div>\n<p>Default plot + few formatting adjustments:<\/p>\n<table width=\"100%\" border=\"0\" bgcolor=\"#e8e8e8\">\n<tbody>\n<tr>\n<td>\n<pre>&gt; ggplot(df.m) + geom_freqpoly(aes(x = value,\n     y = ..density.., colour = variable)) +\n     labs(x = NULL) + opts(legend.position = \"none\") +\n     opts(title = \"Frequency Polygons (based on binned counts)\")<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" alt=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/density11.png?w=416&amp;h=415\" src=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/density11.png?w=416&amp;h=415\" width=\"416\" height=\"415\" \/><\/div>\n<p><strong>Update:<\/strong>\u00a0Hadley kindly points out that the above plots are frequency polygons. I have updated the post with a \u201creal\u201d density plot.<\/p>\n<table width=\"100%\" border=\"0\" bgcolor=\"#e8e8e8\">\n<tbody>\n<tr>\n<td>\n<pre>&gt; ggplot(df.m) + geom_density(aes(x = value,\n     colour = variable)) + labs(x = NULL) +\n     opts(legend.position = \"none\") + opts(title = \"Densities from a kernel density estimator\")<\/pre>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div align=\"center\"><img loading=\"lazy\" decoding=\"async\" alt=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/density2.png?w=416&amp;h=415\" src=\"http:\/\/learnr.files.wordpress.com\/2009\/03\/density2.png?w=416&amp;h=415\" width=\"416\" height=\"415\" \/><\/div>\n<div id=\"jp-post-flair\">\n<div id=\"like-post-wrapper-6877108-94-52c4d0bc24b09\" data-src=\"\/\/widgets.wp.com\/likes\/#blog_id=6877108&amp;post_id=94&amp;origin=learnr.wordpress.com&amp;obj_id=6877108-94-52c4d0bc24b09\" data-name=\"like-post-frame-6877108-94-52c4d0bc24b09\"><\/div>\n<\/div>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>One R Tip A Day\u00a0uses a custom R function to plot two or more overlapping density plots on the same graph. The same can be&hellip; <\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[],"class_list":["post-412","post","type-post","status-publish","format-standard","hentry","category-r"],"_links":{"self":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/412","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/comments?post=412"}],"version-history":[{"count":0,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/posts\/412\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/media?parent=412"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/categories?post=412"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhuoyao.net\/index.php\/wp-json\/wp\/v2\/tags?post=412"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}