CINXE.COM

Code 脿 la Mode: ggplot2

<!DOCTYPE html> <html class='v2' dir='ltr' xmlns='http://www.w3.org/1999/xhtml' xmlns:b='http://www.google.com/2005/gml/b' xmlns:data='http://www.google.com/2005/gml/data' xmlns:expr='http://www.google.com/2005/gml/expr'> <head> <link href='https://www.blogger.com/static/v1/widgets/3566091532-css_bundle_v2.css' rel='stylesheet' type='text/css'/> <link href='http://alexgorbatchev.com/pub/sh/current/styles/shCore.css' rel='stylesheet' type='text/css'/> <link href='http://alexgorbatchev.com/pub/sh/current/styles/shThemeDefault.css' rel='stylesheet' type='text/css'/> <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shCore.js' type='text/javascript'></script> <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushXml.js' type='text/javascript'></script> <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushCss.js' type='text/javascript'></script> <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPython.js' type='text/javascript'></script> <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushPlain.js' type='text/javascript'></script> <script src='http://alexgorbatchev.com/pub/sh/current/scripts/shBrushJScript.js' type='text/javascript'></script> <script type='text/javascript'> SyntaxHighlighter.config.bloggerMode = true; SyntaxHighlighter.all(); </script> <meta content='IE=EmulateIE7' http-equiv='X-UA-Compatible'/> <meta content='width=1100' name='viewport'/> <meta content='text/html; charset=UTF-8' http-equiv='Content-Type'/> <meta content='blogger' name='generator'/> <link href='http://codealamode.blogspot.com/favicon.ico' rel='icon' type='image/x-icon'/> <link href='http://codealamode.blogspot.com/search/label/ggplot2' rel='canonical'/> <link rel="alternate" type="application/atom+xml" title="Code 脿 la Mode - Atom" href="http://codealamode.blogspot.com/feeds/posts/default" /> <link rel="alternate" type="application/rss+xml" title="Code 脿 la Mode - RSS" href="http://codealamode.blogspot.com/feeds/posts/default?alt=rss" /> <link rel="service.post" type="application/atom+xml" title="Code 脿 la Mode - Atom" href="https://www.blogger.com/feeds/1300235002903206586/posts/default" /> <link rel="me" href="https://www.blogger.com/profile/14524776572196611296" /> <!--Can't find substitution for tag [blog.ieCssRetrofitLinks]--> <meta content='http://codealamode.blogspot.com/search/label/ggplot2' property='og:url'/> <meta content='Code 脿 la Mode' property='og:title'/> <meta content='Assortment of R, Python, and other code with applications to the social sciences' property='og:description'/> <title>Code 脿 la Mode: ggplot2</title> <meta content='' name='description'/> <style type='text/css'>@font-face{font-family:'IM Fell French Canon SC';font-style:normal;font-weight:400;font-display:swap;src:url(//fonts.gstatic.com/s/imfellfrenchcanonsc/v22/FBVmdCru5-ifcor2bgq9V89khWcmQghEURY7H0czWhU.eot);}@font-face{font-family:'Molengo';font-style:normal;font-weight:400;font-display:swap;src:url(//fonts.gstatic.com/s/molengo/v16/I_uuMpWeuBzZNBtQXbNalw.eot);}</style> <style id='page-skin-1' type='text/css'><!-- /* ----------------------------------------------- Blogger Template Style Name: Awesome Inc. Designer: Tina Chen URL: tinachen.org ----------------------------------------------- */ /* Content ----------------------------------------------- */ body { font: normal normal 14px Molengo; color: #000000; background: #000000 url(http://themes.googleusercontent.com/image?id=1_UPSuFGxHEaIUme6AnAeu5ZvDnijFNnoLXkl6NuCpTfWQN5BRuDJV_NkibASw-94tFfA) no-repeat scroll top center /* Credit: rajareddychadive (http://www.istockphoto.com/googleimages.php?id=9171196&platform=blogger) */; } html body .content-outer { min-width: 0; max-width: 100%; width: 100%; } a:link { text-decoration: none; color: #a281f4; } a:visited { text-decoration: none; color: #a281f4; } a:hover { text-decoration: underline; color: #644ca1; } .body-fauxcolumn-outer .cap-top { position: absolute; z-index: 1; height: 276px; width: 100%; background: transparent none repeat-x scroll top left; _background-image: none; } /* Columns ----------------------------------------------- */ .content-inner { padding: 0; } .header-inner .section { margin: 0 16px; } .tabs-inner .section { margin: 0 16px; } .main-inner { padding-top: 55px; } .main-inner .column-center-inner, .main-inner .column-left-inner, .main-inner .column-right-inner { padding: 0 5px; } *+html body .main-inner .column-center-inner { margin-top: -55px; } #layout .main-inner .column-center-inner { margin-top: 0; } /* Header ----------------------------------------------- */ .header-outer { margin: 0 0 15px 0; background: transparent none repeat scroll 0 0; } .Header h1 { font: normal normal 40px IM Fell French Canon SC; color: #e5e0ee; text-shadow: 0 0 20px #000000; } .Header h1 a { color: #e5e0ee; } .Header .description { font: normal normal 18px Molengo; color: #e5e0ee; } .header-inner .Header .titlewrapper, .header-inner .Header .descriptionwrapper { padding-left: 0; padding-right: 0; margin-bottom: 0; } .header-inner .Header .titlewrapper { padding-top: 22px; } /* Tabs ----------------------------------------------- */ .tabs-outer { overflow: hidden; position: relative; background: #651dd3 url(http://www.blogblog.com/1kt/awesomeinc/tabs_gradient_light.png) repeat scroll 0 0; } #layout .tabs-outer { overflow: visible; } .tabs-cap-top, .tabs-cap-bottom { position: absolute; width: 100%; border-top: 1px solid #000000; } .tabs-cap-bottom { bottom: 0; } .tabs-inner .widget li a { display: inline-block; margin: 0; padding: .6em 1.5em; font: normal normal 13px Molengo; color: #ffffff; border-top: 1px solid #000000; border-bottom: 1px solid #000000; border-left: 1px solid #000000; height: 16px; line-height: 16px; } .tabs-inner .widget li:last-child a { border-right: 1px solid #000000; } .tabs-inner .widget li.selected a, .tabs-inner .widget li a:hover { background: #000000 url(http://www.blogblog.com/1kt/awesomeinc/tabs_gradient_light.png) repeat-x scroll 0 -100px; color: #ffffff; } /* Headings ----------------------------------------------- */ h2 { font: normal normal 18px Molengo; color: #e5e0ee; } /* Widgets ----------------------------------------------- */ .main-inner .section { margin: 0 27px; padding: 0; } .main-inner .column-left-outer, .main-inner .column-right-outer { margin-top: -3.3em; } #layout .main-inner .column-left-outer, #layout .main-inner .column-right-outer { margin-top: 0; } .main-inner .column-left-inner, .main-inner .column-right-inner { background: transparent none repeat 0 0; -moz-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -webkit-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -goog-ms-box-shadow: 0 0 0 rgba(0, 0, 0, .2); box-shadow: 0 0 0 rgba(0, 0, 0, .2); -moz-border-radius: 0; -webkit-border-radius: 0; -goog-ms-border-radius: 0; border-radius: 0; } #layout .main-inner .column-left-inner, #layout .main-inner .column-right-inner { margin-top: 0; } .sidebar .widget { font: normal normal 13px Molengo; color: #e5e0ee; } .sidebar .widget a:link { color: #a281f4; } .sidebar .widget a:visited { color: #a281f4; } .sidebar .widget a:hover { color: #644ca1; } .sidebar .widget h2 { text-shadow: 0 0 20px #000000; } .main-inner .widget { background-color: transparent; border: 1px solid transparent; padding: 0 0 15px; margin: 20px -16px; -moz-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -webkit-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -goog-ms-box-shadow: 0 0 0 rgba(0, 0, 0, .2); box-shadow: 0 0 0 rgba(0, 0, 0, .2); -moz-border-radius: 0; -webkit-border-radius: 0; -goog-ms-border-radius: 0; border-radius: 0; } .main-inner .widget h2 { margin: 0 -0; padding: .6em 0 .5em; border-bottom: 1px solid transparent; } .footer-inner .widget h2 { padding: 0 0 .4em; border-bottom: 1px solid transparent; } .main-inner .widget h2 + div, .footer-inner .widget h2 + div { border-top: 0 solid transparent; padding-top: 8px; } .main-inner .widget .widget-content { margin: 0 -0; padding: 7px 0 0; } .main-inner .widget ul, .main-inner .widget #ArchiveList ul.flat { margin: -8px -15px 0; padding: 0; list-style: none; } .main-inner .widget #ArchiveList { margin: -8px 0 0; } .main-inner .widget ul li, .main-inner .widget #ArchiveList ul.flat li { padding: .5em 15px; text-indent: 0; color: #ffffff; border-top: 0 solid transparent; border-bottom: 1px solid transparent; } .main-inner .widget #ArchiveList ul li { padding-top: .25em; padding-bottom: .25em; } .main-inner .widget ul li:first-child, .main-inner .widget #ArchiveList ul.flat li:first-child { border-top: none; } .main-inner .widget ul li:last-child, .main-inner .widget #ArchiveList ul.flat li:last-child { border-bottom: none; } .post-body { position: relative; } .main-inner .widget .post-body ul { padding: 0 2.5em; margin: .5em 0; list-style: disc; } .main-inner .widget .post-body ul li { padding: 0.25em 0; margin-bottom: .25em; color: #000000; border: none; } .footer-inner .widget ul { padding: 0; list-style: none; } .widget .zippy { color: #ffffff; } /* Posts ----------------------------------------------- */ body .main-inner .Blog { padding: 0; margin-bottom: 1em; background-color: transparent; border: none; -moz-box-shadow: 0 0 0 rgba(0, 0, 0, 0); -webkit-box-shadow: 0 0 0 rgba(0, 0, 0, 0); -goog-ms-box-shadow: 0 0 0 rgba(0, 0, 0, 0); box-shadow: 0 0 0 rgba(0, 0, 0, 0); } .main-inner .section:last-child .Blog:last-child { padding: 0; margin-bottom: 1em; } .main-inner .widget h2.date-header { margin: 0 -15px 1px; padding: 0 0 0 0; font: normal normal 18px Molengo; color: #e5e0ee; background: transparent none no-repeat fixed center center; border-top: 0 solid transparent; border-bottom: 1px solid transparent; -moz-border-radius-topleft: 0; -moz-border-radius-topright: 0; -webkit-border-top-left-radius: 0; -webkit-border-top-right-radius: 0; border-top-left-radius: 0; border-top-right-radius: 0; position: absolute; bottom: 100%; left: 0; text-shadow: 0 0 20px #000000; } .main-inner .widget h2.date-header span { font: normal normal 18px Molengo; display: block; padding: .5em 15px; border-left: 0 solid transparent; border-right: 0 solid transparent; } .date-outer { position: relative; margin: 55px 0 20px; padding: 0 15px; background-color: #e5e0ee; border: 1px solid #a281f4; -moz-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -webkit-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -goog-ms-box-shadow: 0 0 0 rgba(0, 0, 0, .2); box-shadow: 0 0 0 rgba(0, 0, 0, .2); -moz-border-radius: 0; -webkit-border-radius: 0; -goog-ms-border-radius: 0; border-radius: 0; } .date-outer:first-child { margin-top: 0; } .date-outer:last-child { margin-bottom: 0; -moz-border-radius-bottomleft: 0; -moz-border-radius-bottomright: 0; -webkit-border-bottom-left-radius: 0; -webkit-border-bottom-right-radius: 0; -goog-ms-border-bottom-left-radius: 0; -goog-ms-border-bottom-right-radius: 0; border-bottom-left-radius: 0; border-bottom-right-radius: 0; } .date-posts { margin: 0 -0; padding: 0 0; clear: both; } .post-outer, .inline-ad { border-top: 1px solid #a281f4; margin: 0 -0; padding: 15px 0; } .post-outer { padding-bottom: 10px; } .post-outer:first-child { padding-top: 10px; border-top: none; } .post-outer:last-child, .inline-ad:last-child { border-bottom: none; } .post-body { position: relative; } .post-body img { padding: 8px; background: #ffffff; border: 1px solid #c7c7c7; -moz-box-shadow: 0 0 20px rgba(0, 0, 0, .2); -webkit-box-shadow: 0 0 20px rgba(0, 0, 0, .2); box-shadow: 0 0 20px rgba(0, 0, 0, .2); -moz-border-radius: 0; -webkit-border-radius: 0; border-radius: 0; } h3.post-title, h4 { font: normal bold 18px Molengo; color: #110d33; } h3.post-title a { font: normal bold 18px Molengo; color: #110d33; } h3.post-title a:hover { color: #644ca1; text-decoration: underline; } .post-header { margin: 0 0 1em; } .post-body { line-height: 1.4; } .post-outer h2 { color: #000000; } .post-footer { margin: 1.5em 0 0; } #blog-pager { padding: 15px; font-size: 120%; background-color: transparent; border: 1px solid transparent; -moz-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -webkit-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -goog-ms-box-shadow: 0 0 0 rgba(0, 0, 0, .2); box-shadow: 0 0 0 rgba(0, 0, 0, .2); -moz-border-radius: 0; -webkit-border-radius: 0; -goog-ms-border-radius: 0; border-radius: 0; -moz-border-radius-topleft: 0; -moz-border-radius-topright: 0; -webkit-border-top-left-radius: 0; -webkit-border-top-right-radius: 0; -goog-ms-border-top-left-radius: 0; -goog-ms-border-top-right-radius: 0; border-top-left-radius: 0; border-top-right-radius-topright: 0; margin-top: 0; } .blog-feeds, .post-feeds { margin: 1em 0; text-align: center; color: #e5e0ee; } .blog-feeds a, .post-feeds a { color: #a281f4; } .blog-feeds a:visited, .post-feeds a:visited { color: #a281f4; } .blog-feeds a:hover, .post-feeds a:hover { color: #644ca1; } .post-outer .comments { margin-top: 2em; } /* Comments ----------------------------------------------- */ .comments .comments-content .icon.blog-author { background-repeat: no-repeat; background-image: url(data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABIAAAASCAYAAABWzo5XAAAAAXNSR0IArs4c6QAAAAZiS0dEAP8A/wD/oL2nkwAAAAlwSFlzAAALEgAACxIB0t1+/AAAAAd0SU1FB9sLFwMeCjjhcOMAAAD+SURBVDjLtZSvTgNBEIe/WRRnm3U8RC1neQdsm1zSBIU9VVF1FkUguQQsD9ITmD7ECZIJSE4OZo9stoVjC/zc7ky+zH9hXwVwDpTAWWLrgS3QAe8AZgaAJI5zYAmc8r0G4AHYHQKVwII8PZrZFsBFkeRCABYiMh9BRUhnSkPTNCtVXYXURi1FpBDgArj8QU1eVXUzfnjv7yP7kwu1mYrkWlU33vs1QNu2qU8pwN0UpKoqokjWwCztrMuBhEhmh8bD5UDqur75asbcX0BGUB9/HAMB+r32hznJgXy2v0sGLBcyAJ1EK3LFcbo1s91JeLwAbwGYu7TP/3ZGfnXYPgAVNngtqatUNgAAAABJRU5ErkJggg==); } .comments .comments-content .loadmore a { border-top: 1px solid #000000; border-bottom: 1px solid #000000; } .comments .continue { border-top: 2px solid #000000; } /* Footer ----------------------------------------------- */ .footer-outer { margin: -0 0 -1px; padding: 0 0 0; color: #e5e0ee; overflow: hidden; } .footer-fauxborder-left { border-top: 1px solid transparent; background: transparent none repeat scroll 0 0; -moz-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -webkit-box-shadow: 0 0 0 rgba(0, 0, 0, .2); -goog-ms-box-shadow: 0 0 0 rgba(0, 0, 0, .2); box-shadow: 0 0 0 rgba(0, 0, 0, .2); margin: 0 -0; } /* Mobile ----------------------------------------------- */ body.mobile { background-size: auto; } .mobile .body-fauxcolumn-outer { background: transparent none repeat scroll top left; } *+html body.mobile .main-inner .column-center-inner { margin-top: 0; } .mobile .main-inner .widget { padding: 0 0 15px; } .mobile .main-inner .widget h2 + div, .mobile .footer-inner .widget h2 + div { border-top: none; padding-top: 0; } .mobile .footer-inner .widget h2 { padding: 0.5em 0; border-bottom: none; } .mobile .main-inner .widget .widget-content { margin: 0; padding: 7px 0 0; } .mobile .main-inner .widget ul, .mobile .main-inner .widget #ArchiveList ul.flat { margin: 0 -15px 0; } .mobile .main-inner .widget h2.date-header { left: 0; } .mobile .date-header span { padding: 0.4em 0; } .mobile .date-outer:first-child { margin-bottom: 0; border: 1px solid #a281f4; -moz-border-radius-topleft: 0; -moz-border-radius-topright: 0; -webkit-border-top-left-radius: 0; -webkit-border-top-right-radius: 0; -goog-ms-border-top-left-radius: 0; -goog-ms-border-top-right-radius: 0; border-top-left-radius: 0; border-top-right-radius: 0; } .mobile .date-outer { border-color: #a281f4; border-width: 0 1px 1px; } .mobile .date-outer:last-child { margin-bottom: 0; } .mobile .main-inner { padding: 0; } .mobile .header-inner .section { margin: 0; } .mobile .post-outer, .mobile .inline-ad { padding: 5px 0; } .mobile .tabs-inner .section { margin: 0 10px; } .mobile .main-inner .widget h2 { margin: 0; padding: 0; } .mobile .main-inner .widget h2.date-header span { padding: 0; } .mobile .main-inner .widget .widget-content { margin: 0; padding: 7px 0 0; } .mobile #blog-pager { border: 1px solid transparent; background: transparent none repeat scroll 0 0; } .mobile .main-inner .column-left-inner, .mobile .main-inner .column-right-inner { background: transparent none repeat 0 0; -moz-box-shadow: none; -webkit-box-shadow: none; -goog-ms-box-shadow: none; box-shadow: none; } .mobile .date-posts { margin: 0; padding: 0; } .mobile .footer-fauxborder-left { margin: 0; border-top: inherit; } .mobile .main-inner .section:last-child .Blog:last-child { margin-bottom: 0; } .mobile-index-contents { color: #000000; } .mobile .mobile-link-button { background: #a281f4 url(http://www.blogblog.com/1kt/awesomeinc/tabs_gradient_light.png) repeat scroll 0 0; } .mobile-link-button a:link, .mobile-link-button a:visited { color: #ffffff; } .mobile .tabs-inner .PageList .widget-content { background: transparent; border-top: 1px solid; border-color: #000000; color: #ffffff; } .mobile .tabs-inner .PageList .widget-content .pagelist-arrow { border-left: 1px solid #000000; } --></style> <style id='template-skin-1' type='text/css'><!-- body { min-width: 1130px; } .content-outer, .content-fauxcolumn-outer, .region-inner { min-width: 1130px; max-width: 1130px; _width: 1130px; } .main-inner .columns { padding-left: 0px; padding-right: 240px; } .main-inner .fauxcolumn-center-outer { left: 0px; right: 240px; /* IE6 does not respect left and right together */ _width: expression(this.parentNode.offsetWidth - parseInt("0px") - parseInt("240px") + 'px'); } .main-inner .fauxcolumn-left-outer { width: 0px; } .main-inner .fauxcolumn-right-outer { width: 240px; } .main-inner .column-left-outer { width: 0px; right: 100%; margin-left: -0px; } .main-inner .column-right-outer { width: 240px; margin-right: -240px; } #layout { min-width: 0; } #layout .content-outer { min-width: 0; width: 800px; } #layout .region-inner { min-width: 0; width: auto; } --></style> <link href='https://www.blogger.com/dyn-css/authorization.css?targetBlogID=1300235002903206586&amp;zx=29786718-da0a-4213-8273-13e20b0d02a4' media='none' onload='if(media!=&#39;all&#39;)media=&#39;all&#39;' rel='stylesheet'/><noscript><link href='https://www.blogger.com/dyn-css/authorization.css?targetBlogID=1300235002903206586&amp;zx=29786718-da0a-4213-8273-13e20b0d02a4' rel='stylesheet'/></noscript> <meta name='google-adsense-platform-account' content='ca-host-pub-1556223355139109'/> <meta name='google-adsense-platform-domain' content='blogspot.com'/> </head> <body class='loading variant-icy'> <div class='navbar section' id='navbar'><div class='widget Navbar' data-version='1' id='Navbar1'><script type="text/javascript"> function setAttributeOnload(object, attribute, val) { if(window.addEventListener) { window.addEventListener('load', function(){ object[attribute] = val; }, false); } else { window.attachEvent('onload', function(){ object[attribute] = val; }); } } </script> <div id="navbar-iframe-container"></div> <script type="text/javascript" src="https://apis.google.com/js/platform.js"></script> <script type="text/javascript"> gapi.load("gapi.iframes:gapi.iframes.style.bubble", function() { if (gapi.iframes && gapi.iframes.getContext) { gapi.iframes.getContext().openChild({ url: 'https://www.blogger.com/navbar/1300235002903206586?origin\x3dhttp://codealamode.blogspot.com', where: document.getElementById("navbar-iframe-container"), id: "navbar-iframe" }); } }); </script><script type="text/javascript"> (function() { var script = document.createElement('script'); script.type = 'text/javascript'; script.src = '//pagead2.googlesyndication.com/pagead/js/google_top_exp.js'; var head = document.getElementsByTagName('head')[0]; if (head) { head.appendChild(script); }})(); </script> </div></div> <div class='body-fauxcolumns'> <div class='fauxcolumn-outer body-fauxcolumn-outer'> <div class='cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left'> <div class='fauxborder-right'></div> <div class='fauxcolumn-inner'> </div> </div> <div class='cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </div> <div class='content'> <div class='content-fauxcolumns'> <div class='fauxcolumn-outer content-fauxcolumn-outer'> <div class='cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left'> <div class='fauxborder-right'></div> <div class='fauxcolumn-inner'> </div> </div> <div class='cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </div> <div class='content-outer'> <div class='content-cap-top cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left content-fauxborder-left'> <div class='fauxborder-right content-fauxborder-right'></div> <div class='content-inner'> <header> <div class='header-outer'> <div class='header-cap-top cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left header-fauxborder-left'> <div class='fauxborder-right header-fauxborder-right'></div> <div class='region-inner header-inner'> <div class='header section' id='header'><div class='widget Header' data-version='1' id='Header1'> <div id='header-inner'> <div class='titlewrapper'> <h1 class='title'> <a href='http://codealamode.blogspot.com/'> Code 脿 la Mode </a> </h1> </div> <div class='descriptionwrapper'> <p class='description'><span>Assortment of R, Python, and other code with applications to the social sciences</span></p> </div> </div> </div></div> </div> </div> <div class='header-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </header> <div class='tabs-outer'> <div class='tabs-cap-top cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left tabs-fauxborder-left'> <div class='fauxborder-right tabs-fauxborder-right'></div> <div class='region-inner tabs-inner'> <div class='tabs no-items section' id='crosscol'></div> <div class='tabs no-items section' id='crosscol-overflow'></div> </div> </div> <div class='tabs-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> <div class='main-outer'> <div class='main-cap-top cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left main-fauxborder-left'> <div class='fauxborder-right main-fauxborder-right'></div> <div class='region-inner main-inner'> <div class='columns fauxcolumns'> <div class='fauxcolumn-outer fauxcolumn-center-outer'> <div class='cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left'> <div class='fauxborder-right'></div> <div class='fauxcolumn-inner'> </div> </div> <div class='cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> <div class='fauxcolumn-outer fauxcolumn-left-outer'> <div class='cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left'> <div class='fauxborder-right'></div> <div class='fauxcolumn-inner'> </div> </div> <div class='cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> <div class='fauxcolumn-outer fauxcolumn-right-outer'> <div class='cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left'> <div class='fauxborder-right'></div> <div class='fauxcolumn-inner'> </div> </div> <div class='cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> <!-- corrects IE6 width calculation --> <div class='columns-inner'> <div class='column-center-outer'> <div class='column-center-inner'> <div class='main section' id='main'><div class='widget Blog' data-version='1' id='Blog1'> <div class='blog-posts hfeed'> <div class='status-msg-wrap'> <div class='status-msg-body'> Showing posts with label <b>ggplot2</b>. <a href="http://codealamode.blogspot.com/">Show all posts</a> </div> <div class='status-msg-border'> <div class='status-msg-bg'> <div class='status-msg-hidden'>Showing posts with label <b>ggplot2</b>. <a href="http://codealamode.blogspot.com/">Show all posts</a></div> </div> </div> </div> <div style='clear: both;'></div> <div class="date-outer"> <h2 class='date-header'><span>7/10/2013</span></h2> <div class="date-posts"> <div class='post-outer'> <div class='post hentry'> <a name='8314885214213519948'></a> <h3 class='post-title entry-title'> <a href='http://codealamode.blogspot.com/2013/07/plotting-regression-coefficients-with.html'>Plotting regression coefficients with confidence intervals in ggplot2</a> </h3> <div class='post-header'> <div class='post-header-line-1'></div> </div> <div class='post-body entry-content' id='post-body-8314885214213519948'> A graphical approach to displaying regression coefficients / effect sizes across multiple specifications can often be significantly more powerful and intuitive than presenting a regression table. Moreover, we can easily express uncertainty in the form of confidence intervals around our estimates. As a quick example, suppose that we wanted to compare the effect of British colonial status upon country-level corruption across multiple specifications and two methods (OLS and WLS) from the following paper:&nbsp;Treisman, Daniel. 2000. "The causes of corruption: a cross-national study," <i>Journal of Public Economics</i> 76: 399-457.<br /> <br /> Specifically, the dependent variable is TI98, the perceived corruption score calculated by Transparency International for 1998. The variable whose effect we seek is an indicator that equals 1 if the country is a former British colony or the UK, and 0 otherwise. I took the coefficients and associated standard errors on British colonial status from Tables 2 and 3 across the 5 different specifications where TI98 is the dependent variable. I then entered them into a data frame with the following structure:<br /> <br /> <div class="p1"> &nbsp; &nbsp; &nbsp;&nbsp;coef &nbsp; &nbsp;se &nbsp;method &nbsp; &nbsp; specification&nbsp; &nbsp; &nbsp; &nbsp;lb&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;ub</div> <div class="p1"> 1&nbsp; -1.99 &nbsp; 1.01&nbsp; &nbsp;&nbsp; WLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1 &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; -3.969564 -0.01043638</div> <div class="p1"> 2&nbsp; -1.56 &nbsp;0.59&nbsp; &nbsp; WLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;-2.716379 -0.40362125</div> <div class="p1"> 3&nbsp; -1.25 &nbsp; 0.52&nbsp; &nbsp; WLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-2.269181 -0.23081873</div> <div class="p1"> 4&nbsp; -1.20&nbsp; 0.54&nbsp; &nbsp; WLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;-2.258381 -0.14161945</div> <div class="p1"> 5&nbsp; -1.04&nbsp; 0.79&nbsp; &nbsp; WLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-2.588372&nbsp; 0.50837155</div> <div class="p1"> 6&nbsp; -1.25&nbsp; 0.81&nbsp;&nbsp;&nbsp; &nbsp; OLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 1&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-2.837571&nbsp; 0.33757083</div> <div class="p1"> 7&nbsp; -1.08 &nbsp;0.54&nbsp; &nbsp;&nbsp; OLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 2&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-2.138381 -0.02161945</div> <div class="p1"> 8&nbsp; -0.98 &nbsp;0.53&nbsp; &nbsp; OLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 3&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;-2.018781&nbsp; 0.05878091</div> <div class="p1"> 9&nbsp; -0.82 &nbsp;0.58&nbsp; &nbsp; OLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 4&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-1.956779&nbsp; 0.31677911</div> <div class="p1"> 10 -1.06 &nbsp;0.96&nbsp; &nbsp; OLS &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 5&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;-2.941565&nbsp; 0.82156543</div> <div class="p1"> <br /></div> <div class="p1"> Note that I calculated the upper bound (ub) and lower bound (lb) of the 95% confidence interval using the standard errors provided in the table (I assumed normality holds due to the Central Limit Theorem, which may be questionable in some specifications given small sample sizes).</div> <div class="p1"> <br /></div> <div class="p1"> I then generated the following plot:</div> <br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikNc6qjDxNyb8HUE6u-g3N0VrYUVVz8NOVM9We_vIvi9oOZ2eeJC0iTDquTUTmHalrLmrimtZ0p0HSk3LvViNrSD1WGWIHQhID26hTXwJKC1dDSTo_b__5mtWaLtXCejxTbOecHX8u-cmB/s1600/ex1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEikNc6qjDxNyb8HUE6u-g3N0VrYUVVz8NOVM9We_vIvi9oOZ2eeJC0iTDquTUTmHalrLmrimtZ0p0HSk3LvViNrSD1WGWIHQhID26hTXwJKC1dDSTo_b__5mtWaLtXCejxTbOecHX8u-cmB/s1600/ex1.jpg" width="400" /></a></div> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> Here is the code for making this plot in ggplot2 from the dataframe I provided above:</div> <pre class="brush:plain"> pd <- position_dodge(width=0.2,height=NULL) ggplot(treisman, aes(specification,coef, color=method)) + geom_point(aes(shape=method),size=4, position=pd) + scale_color_manual(name="Method",values=c("coral","steelblue")) + scale_shape_manual(name="Method",values=c(17,19)) + theme_bw() + scale_x_continuous("Specification", breaks=1:length(specification), labels=specification) + scale_y_continuous("Estimated effect of being a former British colony or the UK on TI98") + geom_errorbar(aes(ymin=lb,ymax=ub),width=0.1,position=pd) </pre> <div class="separator" style="clear: both; text-align: left;"> The <code>geom_errorbar()</code> function plots the confidence intervals. Note that I use the <code>position_dodge()</code> function to horizontally shift the coefficients and confidence intervals for the same specifications for clarity. The <code>height=NULL</code> option can be omitted. The color and shape for the legend is controlled manually.</div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> What would happen if I just set <code>name="Method"</code> for the <code>scale_color_manual</code> command, but left out the <code>scale_shape_manual</code> command, letting it be automatically determined:</div> <pre class="brush:plain"> ggplot(treisman, aes(specification,coef, color=method)) + geom_point(aes(shape=method),size=4, position=pd) + scale_color_manual(name="Method",values=c("coral","steelblue")) + theme_bw() + scale_x_continuous("Specification", breaks=1:length(specification), labels=specification) + scale_y_continuous("Estimated effect of being a former British colony or the UK on TI98") + geom_errorbar(aes(ymin=lb,ymax=ub),width=0.1,position=pd) </pre> <div class="separator" style="clear: both; text-align: left;"> This would be the plot:</div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-rnpugiBlhvPrr6RYMGhpeGk8OyXRjVmCEezLc_uHLODgZ5ICYNjY7Z9XP3KCGf74dwBz2-WO7I9PL6-E5N1MpeRh7LCH0scBuhWgIkJ9QNiwKPeZoUocy-F-DYmIiwsSHYqXbeIPLI4i/s1600/ex2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEj-rnpugiBlhvPrr6RYMGhpeGk8OyXRjVmCEezLc_uHLODgZ5ICYNjY7Z9XP3KCGf74dwBz2-WO7I9PL6-E5N1MpeRh7LCH0scBuhWgIkJ9QNiwKPeZoUocy-F-DYmIiwsSHYqXbeIPLI4i/s1600/ex2.jpg" width="320" /></a></div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> This happens because I also set the shape of the points to be determined by the <code>method</code> variable, just as for color. I thus I need to manually give the same name to both scales, or else otherwise they are automatically broken up into two legends, one manual titled "Method" and one automatic title "method".</div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> What if I wanted to reorder the ordering of the methods in the plot; that is, if we wanted WLS to be plotted first, then OLS?</div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> This can be achieved with the following command before running the first block of code on this page.</div> <pre class="brush:plain"> df$method <- reorder(df$method,rep(1:2,each=5)) </pre> <div class="p1"> The result is the following:</div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVZyF_JLw0kipnbf530LOQCwy9COM4GtCj10GJN2tv0_SGoKKdx89joIUcjwRFZJxVEaef8Hg0kqXRy7k_nmDThK10jxSGfUQfegY_k48msSxof0hC4SV_QqL0ntkazQGh4jc7OAPgQjp4/s1600/ex3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVZyF_JLw0kipnbf530LOQCwy9COM4GtCj10GJN2tv0_SGoKKdx89joIUcjwRFZJxVEaef8Hg0kqXRy7k_nmDThK10jxSGfUQfegY_k48msSxof0hC4SV_QqL0ntkazQGh4jc7OAPgQjp4/s1600/ex3.jpg" width="320" /></a></div> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> Finally, suppose that we wanted to customize the x-axis labels by tilting them diagonally and changing them to a dark grey. Adding the following extra piece of code to the blocks of code above would accomplish that:</div> <pre class="brush:plain"> + theme(axis.text.x=element_text(angle=45,color="darkgray")) </pre> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'><span class='post-author vcard'> Posted by <span class='fn'> <a href='https://www.blogger.com/profile/14524776572196611296' rel='author' title='author profile'> Konstantin Kashin </a> </span> </span> <span class='post-timestamp'> at <a class='timestamp-link' href='http://codealamode.blogspot.com/2013/07/plotting-regression-coefficients-with.html' rel='bookmark' title='permanent link'><abbr class='published' title='2013-07-10T15:41:00-04:00'>3:41&#8239;PM</abbr></a> </span> <span class='post-comment-link'> <a class='comment-link' href='http://codealamode.blogspot.com/2013/07/plotting-regression-coefficients-with.html#comment-form' onclick=''>1 comments</a> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2049479395'> <a href='https://www.blogger.com/post-edit.g?blogID=1300235002903206586&postID=8314885214213519948&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> <a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8314885214213519948&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8314885214213519948&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8314885214213519948&target=twitter' target='_blank' title='Share to X'><span class='share-button-link-text'>Share to X</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8314885214213519948&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8314885214213519948&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a> </div> </div> <div class='post-footer-line post-footer-line-2'><span class='post-labels'> Labels: <a href='http://codealamode.blogspot.com/search/label/ggplot2' rel='tag'>ggplot2</a>, <a href='http://codealamode.blogspot.com/search/label/R' rel='tag'>R</a> </span> </div> <div class='post-footer-line post-footer-line-3'></div> </div> </div> </div> </div></div> <div class="date-outer"> <h2 class='date-header'><span>4/07/2012</span></h2> <div class="date-posts"> <div class='post-outer'> <div class='post hentry'> <a name='7830083236475890360'></a> <h3 class='post-title entry-title'> <a href='http://codealamode.blogspot.com/2012/04/plotting-side-by-side-in-ggplot2.html'>Plotting side-by-side in ggplot2</a> </h3> <div class='post-header'> <div class='post-header-line-1'></div> </div> <div class='post-body entry-content' id='post-body-7830083236475890360'> Here's a quick example of plotting histograms next to one another in ggplot2. I wanted to plot the estimated propensity scores for treated and control units for the Lalonde non-experimental data.<br /> <br /> First, generate the estimate propensity scores using the fitted values from a logit model where the dependent variable is the treatment indicator. The dataframe (stripped of the outcome) is called <code>dta.nooutcome.</code> <br /> <code><br /></code><br /> <pre class="brush:plain">glm1 &lt;- glm(TREAT~MARR+NODEGREE+BLACK+HISPANIC+EDUC+AGE+RE74+RE75+U74+U75, family=binomial, data=dta.nooutcome) pscore1 &lt;- glm1$fitted </pre> <br /> Specifically, we can make two histograms for the distribution of the propensity scores corresponding to the two treatment levels.<br /> <br /> <pre class="brush:plain">plot1&lt;- ggplot() + geom_histogram(aes(x = pscore1[dta.nooutcome$TREAT==1]), binwidth=0.1, linetype=1, alpha=.25, color="black", fill="red") + scale_y_continuous(name="Count") + scale_x_continuous(name="Estimated Propensity Score", limits=c(0,1)) + theme_bw() + opts(title="Active Treatment Units") plot2&lt;-ggplot() + geom_histogram(aes(x = pscore1[dta.nooutcome$TREAT==0]), binwidth=0.1, linetype=1, alpha=.25, color="black", fill="blue") + scale_y_continuous(name="Count") + scale_x_continuous(name="Estimated Propensity Score", limits=c(0,1)) + theme_bw() + opts(title="Control Treatment Units") </pre> <br /> To arrange the two plots next to each other, we can use the gridExtra library.<br /> <pre class="brush:plain">grid.newpage() pushViewport(viewport(layout=grid.layout(2,1))) print(plot1, vp=viewport(layout.pos.row=1, layout.pos.col=1)) print(plot2, vp=viewport(layout.pos.row=2, layout.pos.col=1)) </pre> <br /> Note that one can pass the number of rows and columns to the <code>grid.layout()</code> option. Without explicitly specifying width and height of each cell, Viewport automatically arranges the plots. <br /> <br /> The result is:<br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDrfCV5axC_UGrgBD9TQ39jaP3FXEguxJFlRCmWhsyrlO96zvJSy8gSxAKdHEND8PA8fXpQCDcgDv7o6qDm7S5Scqh3BiWC04ot_QFkIZHYOgBRd2fYS0rYkJXTSeby6v-OsxbIGWOEpA9/s1600/pscore_orig.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="640" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDrfCV5axC_UGrgBD9TQ39jaP3FXEguxJFlRCmWhsyrlO96zvJSy8gSxAKdHEND8PA8fXpQCDcgDv7o6qDm7S5Scqh3BiWC04ot_QFkIZHYOgBRd2fYS0rYkJXTSeby6v-OsxbIGWOEpA9/s640/pscore_orig.jpg" width="640" /></a></div> <br /> <br /> If we want to add numbers to the top of each bar to make it easier to see the number of units in each propensity score bucket, we can modify the existing plots as follows: <br /> <br /> <pre class="brush:plain">plot1a &lt;- plot1 + geom_text(aes(x=ggplot_build(plot1)$data[[1]]$x, y=ggplot_build(plot1)$data[[1]]$count , label = sprintf("%2.0f", ggplot_build(plot1)$data[[1]]$count)), position = position_dodge(width = 1), size = 3.5, vjust = -0.3, colour = "grey10") plot2a &lt;- plot2 + geom_text(aes(x=ggplot_build(plot2)$data[[1]]$x, y=ggplot_build(plot2)$data[[1]]$count , label = sprintf("%2.0f", ggplot_build(plot2)$data[[1]]$count)), position = position_dodge(width = 1), size = 3.5, vjust = -0.3, colour = "grey10") grid.newpage() pushViewport(viewport(layout=grid.layout(2,1))) print(plot1a, vp=viewport(layout.pos.row=1, layout.pos.col=1)) print(plot2a, vp=viewport(layout.pos.row=2, layout.pos.col=1))</pre> <br /> Note that we extract the counts of units in each of the histogram bins using the <code>ggplot_build()</code> function and then subsetting the data. Alternatively, one could go about this whole exercise by first manually putting together a dataframe with the bins and the associated counts and passing it to the <code>geom_bar()</code> and <code>geom_text()</code> functions within the <code>ggplot</code> package.<br /> <br /> The result is: <br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4ILxCf_Vn480xOoEvLoZRiQXOyFlURb0sIWwM6r0GnIQS9wR4iwjmKYWKQwb-2uFB5fsFlZIYQ01_kwEgHRn12k_DGImNGrJHT9jFc1XVJYRT_8_aYsKEYaoiY1QYLDnGvo_JAl5A41RI/s1600/pscore_dist.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="634" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEi4ILxCf_Vn480xOoEvLoZRiQXOyFlURb0sIWwM6r0GnIQS9wR4iwjmKYWKQwb-2uFB5fsFlZIYQ01_kwEgHRn12k_DGImNGrJHT9jFc1XVJYRT_8_aYsKEYaoiY1QYLDnGvo_JAl5A41RI/s640/pscore_dist.jpg" width="640" /></a></div> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'><span class='post-author vcard'> Posted by <span class='fn'> <a href='https://www.blogger.com/profile/14524776572196611296' rel='author' title='author profile'> Konstantin Kashin </a> </span> </span> <span class='post-timestamp'> at <a class='timestamp-link' href='http://codealamode.blogspot.com/2012/04/plotting-side-by-side-in-ggplot2.html' rel='bookmark' title='permanent link'><abbr class='published' title='2012-04-07T16:04:00-04:00'>4:04&#8239;PM</abbr></a> </span> <span class='post-comment-link'> <a class='comment-link' href='http://codealamode.blogspot.com/2012/04/plotting-side-by-side-in-ggplot2.html#comment-form' onclick=''>2 comments</a> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2049479395'> <a href='https://www.blogger.com/post-edit.g?blogID=1300235002903206586&postID=7830083236475890360&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> <a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=7830083236475890360&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=7830083236475890360&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=7830083236475890360&target=twitter' target='_blank' title='Share to X'><span class='share-button-link-text'>Share to X</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=7830083236475890360&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=7830083236475890360&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a> </div> </div> <div class='post-footer-line post-footer-line-2'><span class='post-labels'> Labels: <a href='http://codealamode.blogspot.com/search/label/causal%20inference' rel='tag'>causal inference</a>, <a href='http://codealamode.blogspot.com/search/label/data%20visualization' rel='tag'>data visualization</a>, <a href='http://codealamode.blogspot.com/search/label/ggplot2' rel='tag'>ggplot2</a>, <a href='http://codealamode.blogspot.com/search/label/R' rel='tag'>R</a> </span> </div> <div class='post-footer-line post-footer-line-3'></div> </div> </div> </div> <div class='post-outer'> <div class='post hentry'> <a name='2331542766402140157'></a> <h3 class='post-title entry-title'> <a href='http://codealamode.blogspot.com/2012/04/density-plots-and-histograms-in-ggplot2.html'>Density Plots and Histograms in ggplot2</a> </h3> <div class='post-header'> <div class='post-header-line-1'></div> </div> <div class='post-body entry-content' id='post-body-2331542766402140157'> In this post, I'm going to go through how to make plots of distributions (either density plots or histograms) in ggplot2. I'm going to draw upon examples of Fisherian testing in the context of causal inference, but the examples should be completely understandable without knowledge of Fisher's approach to inference.<br /> <br /> For example, suppose that we want to plot the randomization distribution of a test statistic (difference in means in this case) under the sharp null of zero treatment effect across all units. The possible values of the test statistic across 1000 randomly sampled randomizations are in the vector <code>test.stat</code>. Moreover, the actually observed test statistic is <code>tau.hat</code>. Not only do we want to plot the density, but we want to shade in the area under the density that corresponds to the one-sided p-value of this test statistic.<br /> <br /> We can first turn the vector into a data frame that contains the values as x and the corresponding density as y:<br /> <pre class="brush: plain">dd &lt;- with(density(test.stat),data.frame(x,y))</pre> <div class="p2"> <br /> Then plot in <code>ggplot</code>:</div> <pre class="brush: plain">ggplot(data = dd, mapping = aes(x = x, y = y)) + geom_line(color="black") + layer(data = dd, mapping = aes(x=ifelse(x &lt; tau.hat,x,tau.hat), y=y), geom = "area", geom_params=list(fill="red",alpha=.3)) + scale_y_continuous(limits = c(0,max(dd$y)), name="Density") + scale_x_continuous(name="Difference in Means") + geom_vline(aes(xintercept=tau.hat.1a), color="red", linetype="dashed") + geom_text(mapping=aes(x2,y2,label = text2), data=data.frame(x2=-42, y2=0.015, text2="T[obs]"), color=I("red"), parse=TRUE) + geom_text(mapping=aes(x2,y2,label = text2), data=data.frame(x2=-60, y2=0.0025, text2="p-value = 0.041"), size=3) </pre> <div class="p1"> <span class="s1"> </span></div> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP3QrrT8t3zymkJIMPYm5_EmSJXdG3qoLQveKzCd_2EEIUJeJ6onmejFxZEwFMCkZJvtCbYO9cNVyTUM7_8028c15Xws6DDN4ICedMSxhhIbTMpvxTEyGLxPN-rPhXYijHnCZWxXRMz1Jx/s1600/density1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgP3QrrT8t3zymkJIMPYm5_EmSJXdG3qoLQveKzCd_2EEIUJeJ6onmejFxZEwFMCkZJvtCbYO9cNVyTUM7_8028c15Xws6DDN4ICedMSxhhIbTMpvxTEyGLxPN-rPhXYijHnCZWxXRMz1Jx/s320/density1.jpg" width="320" /></a></div> <br /> Several comments on the syntax passed to the <code>ggplot()</code> function above: <br /> <ul> <li>The <code>layer()</code> function with the <code>geom="area"</code> option fills the area under the distribution corresponding to the one-sided p-value (probability of observing a value of the test statistic at least as small as the one we observe).</li> <li>I manually pass the location of the text into the <code>geom_text()</code> function in this case by creating a data frame within the actual <code>geom_text()</code> function.</li> <li>Note that in order for <code>ggplot</code> to write expressions with text, such as subscripts which I use above, I pass the <code>parse=TRUE</code> option to <code>geom_text()</code></li> </ul> <div> </div> <div> <br /> <br /> Here's another example that involves plotting the distribution of p-values under the sharp null of a constant additive treatment effect for each unit. I wanted to plot the values of the resultant test statistics on the x-axis, the p-values for the sharp null on the y-axis, and then shade the 95% Fisherian confidence interval. In this case, <code>tau.vector</code> contains the values of the test statistic (difference in means). <code>p.vector</code> is a corresponding vector that contains the one-sided exact Fisher p-value for the sharp null hypothesis that the effect is 0. Note that I placed a dotted horizontal red line at a p-value of 0.025 to obtain approximately 95% coverage on my confidence interval.<br /> <br /></div> <pre class="brush: plain">ggplot(data = CI.2c, mapping = aes(x = tau.vector, y = p.vector)) + geom_line(color="black") + scale_y_continuous(limits = c(0,max(CI.2c$p.vector)),name="p-value") + scale_x_continuous(name="Difference in Means") + layer(data = CI.2c, mapping = aes(x=ifelse((lb.2c &lt; tau.vector &amp; tau.vector &lt; ub.2c),tau.vector,lb.2c), y=p.vector), geom = "area", geom_params=list(fill="yellow",alpha=.3)) + geom_hline(aes(yintercept=0.025), color="red", linetype="dashed") + geom_text(mapping=aes(x2,y2,label = text2), data=data.frame(x2=-100, y2=0.1, text2="95% CI: [-83,6.33]"), size=5) </pre> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMvALWXpH6M0GVeDGdSTZvSIXDqdiqc8957IL61cVVL5ZUnRpsi6GpZgyUCrkbibHWFBYcJECoxqTaUAe1ZxdvOhM0a-lpM3LLT-2l6avC9mOdgVelHH4N7KUa__JMr17qn0hwisYSBy0o/s1600/density2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhMvALWXpH6M0GVeDGdSTZvSIXDqdiqc8957IL61cVVL5ZUnRpsi6GpZgyUCrkbibHWFBYcJECoxqTaUAe1ZxdvOhM0a-lpM3LLT-2l6avC9mOdgVelHH4N7KUa__JMr17qn0hwisYSBy0o/s320/density2.jpg" width="320" /></a></div> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: center;"> <br /></div> <div class="separator" style="clear: both; text-align: center;"> </div> <div class="separator" style="clear: both; text-align: left;"> Here's a very simple histogram example. Suppose that <code>pval.5</code> contains a vector of p-values across all possible assignment vectors. We want to plot the distribution of Fisher's exact p-values, which should be uniformly distributed between 0 and 1 under the null (and generally when dealing with continuous test statistics). We can do so simply using ggplot as:</div> <div class="separator" style="clear: both; text-align: left;"> <br /></div> <div class="separator" style="clear: both; text-align: left;"> </div> <pre class="brush:plain">ggplot() + geom_histogram(aes(x = pval.5), binwidth=0.1, linetype=1, alpha=.25, color="black", fill="red") + scale_y_continuous(name="Count") + scale_x_continuous(name="p-value") </pre> <br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIvK0kxD6hHs4pv212bcxA2rQ385Xy7X6T-GwxhXMZMyZBiL72NPNd0-9gDrD-r7frw1L8M1udVWGyBuHYGHzOI5lswY2DSttcW-pYrEv5BdwY8mhBOYXb34Kh_aGLFzkA4YOB7w5FcmtJ/s1600/hist1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhIvK0kxD6hHs4pv212bcxA2rQ385Xy7X6T-GwxhXMZMyZBiL72NPNd0-9gDrD-r7frw1L8M1udVWGyBuHYGHzOI5lswY2DSttcW-pYrEv5BdwY8mhBOYXb34Kh_aGLFzkA4YOB7w5FcmtJ/s320/hist1.jpg" width="320" /></a></div> <div class="separator" style="clear: both; text-align: center;"> </div> <div class="separator" style="clear: both; text-align: center;"> </div> <div class="separator" style="clear: both; text-align: center;"> </div> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'><span class='post-author vcard'> Posted by <span class='fn'> <a href='https://www.blogger.com/profile/14524776572196611296' rel='author' title='author profile'> Konstantin Kashin </a> </span> </span> <span class='post-timestamp'> at <a class='timestamp-link' href='http://codealamode.blogspot.com/2012/04/density-plots-and-histograms-in-ggplot2.html' rel='bookmark' title='permanent link'><abbr class='published' title='2012-04-07T15:24:00-04:00'>3:24&#8239;PM</abbr></a> </span> <span class='post-comment-link'> <a class='comment-link' href='http://codealamode.blogspot.com/2012/04/density-plots-and-histograms-in-ggplot2.html#comment-form' onclick=''>0 comments</a> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2049479395'> <a href='https://www.blogger.com/post-edit.g?blogID=1300235002903206586&postID=2331542766402140157&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> <a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=2331542766402140157&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=2331542766402140157&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=2331542766402140157&target=twitter' target='_blank' title='Share to X'><span class='share-button-link-text'>Share to X</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=2331542766402140157&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=2331542766402140157&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a> </div> </div> <div class='post-footer-line post-footer-line-2'><span class='post-labels'> Labels: <a href='http://codealamode.blogspot.com/search/label/causal%20inference' rel='tag'>causal inference</a>, <a href='http://codealamode.blogspot.com/search/label/data%20visualization' rel='tag'>data visualization</a>, <a href='http://codealamode.blogspot.com/search/label/ggplot2' rel='tag'>ggplot2</a>, <a href='http://codealamode.blogspot.com/search/label/R' rel='tag'>R</a> </span> </div> <div class='post-footer-line post-footer-line-3'></div> </div> </div> </div> </div></div> <div class="date-outer"> <h2 class='date-header'><span>4/02/2012</span></h2> <div class="date-posts"> <div class='post-outer'> <div class='post hentry'> <a name='8888277838697303667'></a> <h3 class='post-title entry-title'> <a href='http://codealamode.blogspot.com/2012/04/factor-orderings-in-ggplot2.html'>Factor Orderings in ggplot2</a> </h3> <div class='post-header'> <div class='post-header-line-1'></div> </div> <div class='post-body entry-content' id='post-body-8888277838697303667'> One of the occasionally annoying features of R and thus <code>ggplot2</code> is dealing with factors. In this post, I'll go through how to handle ordering of factors in ggplot2 and the manual assignment of colors to those categories.<br /> <br /> For this example, I'm going to use data collected by <a href="http://www.people.fas.harvard.edu/~dziblatt/">Daniel Ziblatt</a> on the European revolutions of 1848. Specifically, for each county in the data, we can code the county as having a revolution, having a constitutional concession, or no revolution. For each county, we also know distance from Paris (in kilometers) and the price shock in wheat in 1847. These are meant to test arguments made about the locations of of the 1848 revolutions: that countries that experienced them were closer to Paris and the French Revolution (diffusion argument) and/or revolutions were the result of economic hardships.<br /> <br /> An initial attempt at plotting this may look something like this:<br /> <br /> <pre class="brush: plain">ggplot(my.data, aes(Distance, PriceSpike, color=Revolution)) + geom_point(shape=20, size=5) + geom_text(aes(label=Country), size=3, hjust=-0.15) + scale_x_continuous("Distance from Paris (km)") + scale_y_continuous("Price Shock in 1847 \n (Price of Wheat in 1847/Average Price of Wheat 1830-1845)") + theme_bw() + opts(title="Food Price Shock and Distance from Paris in the Revolutions of 1848") + opts(legend.position=c(.12,.9), legend.background=theme_rect(fill="white")) </pre> <br /> <br /> The result is:<br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVlUqsXYgUgvFL_8vIzsdivOanp_Ru8i_8db05fgx2LdTZUCJCFG_PU81aMNd9UguG_1UPW7l_cHfz_EA4E_EeAYkakuO488-G7yrjx9-CgcSw-DuTxljZ7k7BoPhzdf5bKp9ILtCA8P_k/s1600/ziblatt-default.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="348" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhVlUqsXYgUgvFL_8vIzsdivOanp_Ru8i_8db05fgx2LdTZUCJCFG_PU81aMNd9UguG_1UPW7l_cHfz_EA4E_EeAYkakuO488-G7yrjx9-CgcSw-DuTxljZ7k7BoPhzdf5bKp9ILtCA8P_k/s640/ziblatt-default.jpg" width="640" /></a></div> <br /> <br /> Note that the factor variable that we are plotting is Revolution and <code>ggplot2</code> automatically orders it in the legend (alphabetically) and assigns it default colors. Also note that I manually positioned the legend within the plotting space.<br /> <br /> What if we wanted "Revolution" to come first in the legend, followed by "Constitutional Concession", then "No Revolution"? Moreover, what if we wanted the color for "Revolution" to be red and the color for "No Revolution" to be green?<br /> <br /> We can create an additional vector of the length of the number of observations in the&nbsp;<span class="s1">my.data dataframe called Revolution.order:</span><br /> <pre class="brush: plain">Revolution.order &lt;- my.data$Revolution Revolution.order[Revolution.order=="Revolution"] &lt;- 1 Revolution.order[Revolution.order=="Constitutional Concession"] &lt;- 2 Revolution.order[Revolution.order=="No Revolution"] &lt;- 3 Revolution.order &lt;- as.integer(Revolution.order) </pre> <div class="p1"> <span class="s1"><br /></span><br /> <span class="s1">This vector now contains the ordering of the factor variable which we can feed into <code>ggplot</code> through the <code>reorder()</code> function:</span></div> <div class="p1"> <br /></div> <pre class="brush: plain">ggplot(my.data, aes(Distance, PriceSpike, color=reorder(Revolution,Revolution.order))) + geom_point(shape=20, size=5) + geom_text(aes(label=Country), size=3, hjust=-0.15) + scale_x_continuous("Distance from Paris (km)") + scale_y_continuous("Price Shock in 1847 \n (Price of Wheat in 1847/Average Price of Wheat 1830-1845)") + theme_bw() + opts(title="Food Price Shock and Distance from Paris in the Revolutions of 1848") + scale_color_manual(name="", values=c("#D7301F", "#0570B0", "#2CA25F")) + opts(legend.position=c(.12,.9), legend.background=theme_rect(fill="white")) </pre> <div class="p2"> <span class="s1"><br /></span></div> <div class="p2"> <span class="s1">Note that we can manually supply the colors for the factor through <code>scale_color_manual()</code>.</span></div> <div class="p2"> <span class="s1"><br /></span></div> <div class="p2"> <span class="s1">The result is:</span></div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhT92Pgu-q-Xp4GnRPd5soH4uOU68FgJjZ8ZvaHeqtmih3qYORpgSruCz4D-dJJuHzxZIddS4nRTCPnVJJYvm9kIgJ-gaQ0argIU-kDW6v1SbY81vpPYM2dgnDVENdr1hh-eQFX5rpwMIt/s1600/ziblatt_full.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="448" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhhT92Pgu-q-Xp4GnRPd5soH4uOU68FgJjZ8ZvaHeqtmih3qYORpgSruCz4D-dJJuHzxZIddS4nRTCPnVJJYvm9kIgJ-gaQ0argIU-kDW6v1SbY81vpPYM2dgnDVENdr1hh-eQFX5rpwMIt/s640/ziblatt_full.jpg" width="640" /></a></div> <div class="p2"> <span class="s1"><br /></span></div> <br /> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'><span class='post-author vcard'> Posted by <span class='fn'> <a href='https://www.blogger.com/profile/14524776572196611296' rel='author' title='author profile'> Konstantin Kashin </a> </span> </span> <span class='post-timestamp'> at <a class='timestamp-link' href='http://codealamode.blogspot.com/2012/04/factor-orderings-in-ggplot2.html' rel='bookmark' title='permanent link'><abbr class='published' title='2012-04-02T01:16:00-04:00'>1:16&#8239;AM</abbr></a> </span> <span class='post-comment-link'> <a class='comment-link' href='http://codealamode.blogspot.com/2012/04/factor-orderings-in-ggplot2.html#comment-form' onclick=''>0 comments</a> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2049479395'> <a href='https://www.blogger.com/post-edit.g?blogID=1300235002903206586&postID=8888277838697303667&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> <a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8888277838697303667&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8888277838697303667&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8888277838697303667&target=twitter' target='_blank' title='Share to X'><span class='share-button-link-text'>Share to X</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8888277838697303667&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=8888277838697303667&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a> </div> </div> <div class='post-footer-line post-footer-line-2'><span class='post-labels'> Labels: <a href='http://codealamode.blogspot.com/search/label/data%20visualization' rel='tag'>data visualization</a>, <a href='http://codealamode.blogspot.com/search/label/ggplot2' rel='tag'>ggplot2</a>, <a href='http://codealamode.blogspot.com/search/label/R' rel='tag'>R</a> </span> </div> <div class='post-footer-line post-footer-line-3'></div> </div> </div> </div> </div></div> <div class="date-outer"> <h2 class='date-header'><span>2/25/2012</span></h2> <div class="date-posts"> <div class='post-outer'> <div class='post hentry'> <a name='6352815811227066293'></a> <h3 class='post-title entry-title'> <a href='http://codealamode.blogspot.com/2012/02/trellis-graphs-in-ggplot2.html'>Trellis graphs in ggplot2</a> </h3> <div class='post-header'> <div class='post-header-line-1'></div> </div> <div class='post-body entry-content' id='post-body-6352815811227066293'> Trellis graphs are an informative way of visualizing relationships between variables conditional on other variable(s). In R, one can make trellis plots either through the <code>lattice</code> package, or <code>ggplot2</code>. In this post, I'll show how to make such graphs in <code>ggplot2</code>.<br /> <br /> I'm still going to use the&nbsp;<a href="http://www.hks.harvard.edu/fs/pnorris/Data/Data.htm">Pippa Norris' dataset "Democracy Crossnational Data"</a>&nbsp;for this example. It has a variety of political, economic, and social variables for 191 countries. Suppose we want to plot the relationship between&nbsp;GDP per capita in 2000 as the independent variable (transformed via natural logarithm) and the Polity score of democracy in 2000 as the dependent variable (0=most authoritarian, 20=most democratic), <i>conditional</i> upon the region (Region8a variable).<br /> <br /> The more common way to do so is using the <code>lattice</code> package in R:<br /> <br /> <pre class="brush: plain">library(lattice) xyplot(Polity3 ~ log(GDP2000) | Region8a, data = norris.data,&nbsp; xlab = "Logarithm of GDP (PPP annual, WB)",&nbsp; ylab = "Polity Score (0=Full Authoritarianism, 20=Full Democracy)",&nbsp; scales="free", pch=19, panel = function(x, y) { panel.xyplot(x,y,pch=19, cex=0.5, col="black") panel.lmline(x,y,col="red", lty=2) }) </pre> <br /> The result is:<br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpeBo0iSvyboG1uug-oM9k1cck_92e5rTvQYsjuvSoDqj0mc2bUKIM6-26DNUfTm-tq86Wi0Y5U7sEc6tX6vPdQ7D8e1eIBESlledgadSjrQVqwRXUxT8onDYPNqn23AHzu3AfLG5PXFtF/s1600/lattice-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="400" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgpeBo0iSvyboG1uug-oM9k1cck_92e5rTvQYsjuvSoDqj0mc2bUKIM6-26DNUfTm-tq86Wi0Y5U7sEc6tX6vPdQ7D8e1eIBESlledgadSjrQVqwRXUxT8onDYPNqn23AHzu3AfLG5PXFtF/s400/lattice-1.jpg" width="400" /></a></div> <div class="separator" style="clear: both; text-align: center;"> </div> <br /> Note that the <code>scales="free"</code> option fits <code>xlim</code> and <code>ylim</code> for each region independently.&nbsp; <code>panel.xyplot()</code> plots the points and&nbsp; <code>panel.lmline()</code> fits the linear regression line for each region. Now, suppose we wanted to do this in <code>ggplot</code>. We are going to make use of the <code> facet_wrap() </code> command to create a trellis plot.<br /> <br /> The syntax will be:<br /> <div class="p1"> <pre class="brush: plain">ggplot(norris.data, aes(log(GDP2000), Polity3)) + geom_point(shape=20, size=3) + facet_wrap(~Region8a, ncol=4, scales="free") + scale_x_continuous("Logarithm of GDP (PPP annual, WB)") + scale_y_continuous("Polity Score (0=Full Authoritarianism, 20=Full Democracy)") + geom_smooth(method="lm", color="red") </pre> <div class="p2"> <br /> The result is:<br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8Ttv9T75gCLW9tCLhqSWMjziMY7ArY0K3FLSosNZDKsgEFVJJu99EFgFzzWZvGNPaE23FgjFYaTCEzehAriuK30gaxSP73DyYbo0XZna_wO0Ev5OQl08LHQO6OUWKEtwOjDgW13s1_o9u/s1600/trellis-2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="374" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh8Ttv9T75gCLW9tCLhqSWMjziMY7ArY0K3FLSosNZDKsgEFVJJu99EFgFzzWZvGNPaE23FgjFYaTCEzehAriuK30gaxSP73DyYbo0XZna_wO0Ev5OQl08LHQO6OUWKEtwOjDgW13s1_o9u/s640/trellis-2.jpg" width="640" /></a></div> <br /> <br /> One could remove the confidence intervals by including&nbsp;<code>se=FALSE</code> within the <code>geom_smooth()</code> function. One could also force the regression lines to extrapolate beyond the data by including&nbsp;<code>fullrange=TRUE</code> within the <code>geom_smooth()</code> function. Finally, to change the color of the confidence interval from the default gray, we can specify&nbsp;including&nbsp;<code>fill="blue"</code>, for example,&nbsp;within the&nbsp;<code>geom_smooth()</code>&nbsp;function. Note that in general,&nbsp;the&nbsp;<code>geom_smooth()</code>&nbsp;function is quite versatile and allows a variety of smoothing methods: you can specify <code>lm</code>, <code>glm</code>, <code>gam</code>, <code>loess</code>, or <code>rlm</code> (not exhaustive list) for the <code>method</code> argument. <br /> <br /></div> <div class="p2"> An alternative, more flexible, way to do this is to first create a dataframe using the <code>plyr</code> package that contains the intercepts and slopes for the linear regression for each region. So instead of automatically&nbsp;passing down the aesthetics on to the&nbsp;<code>geom_smooth()</code>&nbsp;function as in the previous example, we can manually specify the aesthetics of the lines we want to draw using&nbsp;the&nbsp;<code>geom_abline()</code>&nbsp;function.&nbsp;</div> <div class="p2"> <br /> <pre class="brush: plain">library(plyr) reg.df &lt;- ddply(norris.data, .(Region8a), function(i) lm(Polity3 ~ log(GDP2000), data = i)$coefficients[1:2]) names(reg.df)[2] &lt;- "intercept" names(reg.df)[3] &lt;- "logGDP2000" </pre> <br /> Then we make the trellis plot as:<br /> <pre class="brush: plain">ggplot(norris.data, aes(log(GDP2000), Polity3)) + geom_point(shape=20, size=3) + facet_wrap(~Region8a, ncol=4, scales="free") + scale_x_continuous("Logarithm of GDP (PPP annual, WB)") + scale_y_continuous("Polity Score (0=Full Authoritarianism, 20=Full Democracy)") + geom_abline(aes(intercept = intercept, slope = logGDP2000), color = "red", data = reg.df) </pre> <div class="p1"> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7XBEUCSnarssRd3XXW3s6xyaP1Z2k6YdW4Q-8amT32JOe28JOyOA44d6K17YByqCFauBiN1y7TgZQccQG-UZG9YEoucaE_1RVdQC2onsQb-rvcUHRaqTP4p1Ot9-4iFa4DaL4_KQHVB8/s1600/trellis-1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="386" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjB7XBEUCSnarssRd3XXW3s6xyaP1Z2k6YdW4Q-8amT32JOe28JOyOA44d6K17YByqCFauBiN1y7TgZQccQG-UZG9YEoucaE_1RVdQC2onsQb-rvcUHRaqTP4p1Ot9-4iFa4DaL4_KQHVB8/s640/trellis-1.jpg" width="640" /></a></div> <span class="s1"> </span></div> <div class="p1"> <span class="s1"> </span></div> </div> <br /> A slightly different example of how to create trellis plots comes from the time series version of the Pippa Norris data. Suppose we want to look at the relationship between economic development and democracy over time just within easy country Central and Eastern Europe. One way to do so (as an exploratory tool at least) would be as follows: <br /> <br /> <pre class="brush: plain">CEE.lattice &lt;- ggplot(norris.ts.CEE.data[norris.ts.CEE.data$Year&gt;=1980,], aes(logGDP_WB, Polity1, color=Year)) + geom_path(color="black") + geom_point(shape=20, size=3) + facet_wrap(~Nation, ncol=5, scales="free") + scale_colour_gradient(breaks=c(1980,1990,1995,2000), low="yellow", high="blue")&nbsp;+ scale_x_continuous("Logarithm of GDP (PPP annual, WB)") + scale_y_continuous("Polity Score (0=Full Authoritarianism, 20=Full Democracy)") + opts(title="Relationship between Econ. Development and Democracy in Central and Eastern Europe") </pre> <br /> The result is: <br /> <div class="separator" style="clear: both; text-align: center;"> </div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4Z9iZJSyVe7nGceLOIgpJcrISZ_saCZhy-D1Xmxlej6y9O3aG4Reu1s7hymU_64Zmlne-XDyLvtMmrmnrxxRK8UZ_32cRi8dvCrd3A34ekie0ObErnJM3SGmuQAUaLNtj1sRfKCc9vEQW/s1600/trellis2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="308" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEh4Z9iZJSyVe7nGceLOIgpJcrISZ_saCZhy-D1Xmxlej6y9O3aG4Reu1s7hymU_64Zmlne-XDyLvtMmrmnrxxRK8UZ_32cRi8dvCrd3A34ekie0ObErnJM3SGmuQAUaLNtj1sRfKCc9vEQW/s640/trellis2.jpg" width="640" /></a></div> <div class="separator" style="clear: both; text-align: center;"> </div> <br /> Note that <code> scale_colour_gradient(breaks=c(1980,1990,1995,2000), low="yellow", high="blue") </code> colors the points with a gradient from yellow to blue based on the year. I manually set the breaks in the gradient. Also, note that I use <code> geom_path() </code> to connect the points using the order of the observations. This is different than the <code> geom_line() </code> command, which would connect the points in order of the x-variable, which is GDP in this case (and not time). <br /> <br /> Finally, and this holds across all of <code>ggplot</code>, if we wanted to make the background layer of the plots white instead of gray, we could simply pass the following argument to <code>ggplot</code> as a layer:<br /> <br /> <pre class="brush: plain">CEE.lattice + theme_bw() </pre> <div class="p1"> <span class="s1"> </span><br /> <span class="s1"><br /></span><br /> <span class="s1">The result is:</span></div> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjvwxaxOAIwuesk0bvP6sgs7KuMsvBQYvfjQTwYZyCZmnAawW7KmCRTUTQ7_yU8qdNDMq7DzQUg_aPH-3bjfJ-4WXd7P_jN40rEqRxSGNdboLzZRuu4KDR0q9XCexSjY9-548Y0n2ayXtO/s1600/trellis3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="310" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjjvwxaxOAIwuesk0bvP6sgs7KuMsvBQYvfjQTwYZyCZmnAawW7KmCRTUTQ7_yU8qdNDMq7DzQUg_aPH-3bjfJ-4WXd7P_jN40rEqRxSGNdboLzZRuu4KDR0q9XCexSjY9-548Y0n2ayXtO/s640/trellis3.jpg" width="640" /></a></div> </div> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'><span class='post-author vcard'> Posted by <span class='fn'> <a href='https://www.blogger.com/profile/14524776572196611296' rel='author' title='author profile'> Konstantin Kashin </a> </span> </span> <span class='post-timestamp'> at <a class='timestamp-link' href='http://codealamode.blogspot.com/2012/02/trellis-graphs-in-ggplot2.html' rel='bookmark' title='permanent link'><abbr class='published' title='2012-02-25T14:07:00-05:00'>2:07&#8239;PM</abbr></a> </span> <span class='post-comment-link'> <a class='comment-link' href='http://codealamode.blogspot.com/2012/02/trellis-graphs-in-ggplot2.html#comment-form' onclick=''>1 comments</a> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2049479395'> <a href='https://www.blogger.com/post-edit.g?blogID=1300235002903206586&postID=6352815811227066293&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> <a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=6352815811227066293&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=6352815811227066293&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=6352815811227066293&target=twitter' target='_blank' title='Share to X'><span class='share-button-link-text'>Share to X</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=6352815811227066293&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=6352815811227066293&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a> </div> </div> <div class='post-footer-line post-footer-line-2'><span class='post-labels'> Labels: <a href='http://codealamode.blogspot.com/search/label/data%20visualization' rel='tag'>data visualization</a>, <a href='http://codealamode.blogspot.com/search/label/ggplot2' rel='tag'>ggplot2</a>, <a href='http://codealamode.blogspot.com/search/label/R' rel='tag'>R</a> </span> </div> <div class='post-footer-line post-footer-line-3'></div> </div> </div> </div> </div></div> <div class="date-outer"> <h2 class='date-header'><span>2/12/2012</span></h2> <div class="date-posts"> <div class='post-outer'> <div class='post hentry'> <a name='4696181333095615375'></a> <h3 class='post-title entry-title'> <a href='http://codealamode.blogspot.com/2012/02/better-graphics-in-r-intro-to-ggplot2.html'>Better graphics in R: intro to ggplot2</a> </h3> <div class='post-header'> <div class='post-header-line-1'></div> </div> <div class='post-body entry-content' id='post-body-4696181333095615375'> I believe visualization of data is extremely important and not emphasized quite enough in the social sciences. At the very least, exploratory visualization of a data set should be a part of any thorough analysis, even if it doesn't make it into the final paper. On this note, I want to showcase the use of <a href="http://had.co.nz/ggplot2/"><code>ggplot2</code></a> as an amazing package for visualization in R. Although start-up costs are higher than using the base package for plots, the final product is better in my opinion (another common package for visualization of multivariate data is <code>lattice</code>).<br /> <br /> Let's look at the "grammar" of <code>ggplot2</code>. The 2 basic types of input into <code>ggplot2</code> are:<br /> <ul> <li>Aesthetics: x position, y position, shape / size / color of elements</li> <li>Elements / geometric shapes: points, lines, bars, text, etc. </li> </ul> A ggplot is invoked using the <code>ggplot2()</code> command. The aesthetics are typically passed to the plotting function using the <code>aes()</code> function. The logic behind <code>ggplot2</code> is that once the aesthetics are passed through, each type of element is plotted as a separate layer. The <i>layering </i>property of <code>ggplot2</code> will become apparent in the examples.<br /> <br /> There is also a quick plotting function withing <code>ggplot2</code> known as <code>qplot()</code>. It is an easier place to start because it is meant to resemble the syntax of the <code>plot()</code> command within the base package in R. Let's start with one example of <code>qplot()</code> and then shift to <code>ggplot()</code> since the functionality is so much richer in the latter.<br /> <br /> I decided to draw upon <a href="http://www.hks.harvard.edu/fs/pnorris/Data/Data.htm">Pippa Norris' dataset "Democracy Crossnational Data"</a> for this example. It has a variety of political, economic, and social variables for 191 countries. Specifically, given that I just re-read some arguments on the connection between democracy and economic development (<a href="http://books.google.com/books?id=uiFH5dh12p0C">see Przeworski et al., 2000</a>), I will focus on GDP per capita in 2000 as the independent variable (transformed via natural logarithm) and the Polity score of democracy in 2000 as the dependent variable (0=most authoritarian, 20=most democratic). The variables are called GDP2000 and Polity3 in Norris' dataset, respectively.<br /> <br /> We can create a simple scatterplot as follows:<br /> <pre class="brush: plain">qplot(log(GDP2000), Polity3, data=norris.data, ylab="Polity Score, 2000 (0=Authoritarian, 20=Democracy)", xlab="Log of Per Capita GDP (2000)", pch=19, size=2, color=I("darkblue"), alpha=.75) + opts(legend.position="none") </pre> <br /> This syntax should be quite similar to that of <code>plot()</code>, the only real difference being that we use <code>size</code> instead of <code>cex</code> to control point size, use an <code>I()</code> around the color name to indicate that it's a user-inputted constant (as opposed to a function of the data), and finally specify <code>alpha=.75</code> to create some transparency in the points. These are <code>ggplot</code> aesthetic names and they differ from those in <code>plot()</code>. Also note that we must add <code> + opts(legend.position="none") </code> to remove the legend that would otherwise show up to the right of the plot. The result is: <br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX23b63sS3EI1nAh5dLaAM45vP7YSfq2_ZmVd_GSA7bnhkt__31y9MampweQQ3sc2h-yuK_xe5N34Am7oGTlrIhG67XNZg45iJfMtw-wl8mtyF4-PICpu1G97YaeJqurD5hKxluZOVne2Z/s1600/figure1.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhX23b63sS3EI1nAh5dLaAM45vP7YSfq2_ZmVd_GSA7bnhkt__31y9MampweQQ3sc2h-yuK_xe5N34Am7oGTlrIhG67XNZg45iJfMtw-wl8mtyF4-PICpu1G97YaeJqurD5hKxluZOVne2Z/s320/figure1.jpg" width="320" /></a></div> <br /> <br /> Now, we can make the exact same graph, but using <code>ggplot()</code>:<br /> <br /> <pre class="brush: plain">plot2 &lt;- ggplot(norris.data, aes(log(GDP2000), Polity3)) plot2 + geom_point(shape=20, size=4, color="darkblue", alpha=.5) + scale_y_continuous("Polity Score, 2000 (0=Authoritarian, 20=Democracy)") + scale_x_continuous("Log of Per Capita GDP (2000)") </pre> <br /> In this case, note how we first define <code>plot2</code> as the data (norris.data) and the variables which provide the x location and y location (part of the aesthetics). Then, we call <code>plot2</code> and graph the points, y-axis, and x-axis, all as separate layers. Note that we can pass layer-specific aesthetics to the plotting tools (point shape, size, and color, in this case). The result is the same as above.<br /> <br /> We can add a regression line to the previous plot as yet another layer as follows:<br /> <pre class="brush: plain">plot2 + geom_point(shape=20, size=4, color="darkblue", alpha=.5) + scale_y_continuous("Polity Score, 2000 (0=Authoritarian, 20=Democracy)") + scale_x_continuous("Log of Per Capita GDP (2000)") + geom_abline(color="red") </pre> <br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHUG-6_mBGz5JFT5HYFjO0PDCdPfJCkc-Oiq-nh5OTpTStnF3Bdppj3Ljr9ppu7qlR4r0b6x_YsdZ8_OCcVmsr43q7uAILF_JHzw5QsNvK_gpOpSelBiVv_KmDqqvHSVJyKoiyOHLPJPv3/s1600/figure2.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjHUG-6_mBGz5JFT5HYFjO0PDCdPfJCkc-Oiq-nh5OTpTStnF3Bdppj3Ljr9ppu7qlR4r0b6x_YsdZ8_OCcVmsr43q7uAILF_JHzw5QsNvK_gpOpSelBiVv_KmDqqvHSVJyKoiyOHLPJPv3/s320/figure2.jpg" width="320" /></a></div> <br /> <br /> Suppose we want to demarcate the regions in the world the countries are located in. We can make the color aesthetic a function of the Region8a variable in the data (factor variable with 8 regions) as follows:<br /> <pre class="brush: plain">ggplot(norris.data, aes(log(GDP2000), Polity3, color=Region8a)) + geom_point(shape=20, size=3) + scale_y_continuous("Polity Score, 2000 (0=Authoritarian, 20=Democracy)") + scale_x_continuous("Log of Per Capita GDP (2000)") + scale_color_discrete(name = "Regions") </pre> <br /> Note that specifically we passed <code>color=Region8a</code> as an argument to the initial <code>aes()</code> function. <code>scale_color_discrete(name = "Regions"</code> was invoked to change the name of the legend.<br /> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxOtFGg4CQOuXjKMPAWnoo2J0vm7Sr-_k70PREhyphenhyphenOk73bJ_9Jin7qB33X-Nb7hrMc_ocR1Y3PK1JGHJXPRnIomDtDRheKucG3IGMn1tF1ziDSiyxykihV8gaBX5Efav6LCPSxLnjCvU2jS/s1600/figure3.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhxOtFGg4CQOuXjKMPAWnoo2J0vm7Sr-_k70PREhyphenhyphenOk73bJ_9Jin7qB33X-Nb7hrMc_ocR1Y3PK1JGHJXPRnIomDtDRheKucG3IGMn1tF1ziDSiyxykihV8gaBX5Efav6LCPSxLnjCvU2jS/s320/figure3.jpg" width="320" /></a></div> <br /> <br /> We can add country names just below the points in the previous plot as follows (again, separate layer):<br /> <pre class="brush: plain">ggplot(norris.data, aes(log(GDP2000), Polity3, color=Region8a)) +geom_point(shape=20, size=3) + geom_text(aes(label=Natmap), size=2.5, vjust=2.5) + scale_y_continuous("Polity Score, 2000 (0=Authoritarian, 20=Democracy)") + scale_x_continuous("Log of Per Capita GDP (2000)")+ scale_color_discrete(name = "Regions")</pre> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEje4CgZF6OzB9M0Kx1KoLzgtIzA-iuhwljyj21ywY1tRvBwSSHestBtHvyzD-eFuGmIJ2tGLuNVfUTmpESorObLTQp9FYSwfAW-5K4yQNT-KPa3eGt-G6dvGJJ6JSF-3NieYc9tgdYwvTXY/s1600/figure4.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEje4CgZF6OzB9M0Kx1KoLzgtIzA-iuhwljyj21ywY1tRvBwSSHestBtHvyzD-eFuGmIJ2tGLuNVfUTmpESorObLTQp9FYSwfAW-5K4yQNT-KPa3eGt-G6dvGJJ6JSF-3NieYc9tgdYwvTXY/s320/figure4.jpg" width="320" /></a></div> <br /> <br /> If we just want country names and no points:<br /> <pre class="brush: plain">ggplot(norris.data, aes(log(GDP2000), Polity3, color=Region8a))+ geom_text(aes(label=Natmap), size=2.5) + scale_y_continuous("Polity Score, 2000 (0=Authoritarian, 20=Democracy)") + scale_x_continuous("Log of Per Capita GDP (2000)")+ scale_color_discrete(name = "Regions") </pre> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVZv45R_-Oyg7z37sPNrGYutVzM63rVNyfmSEG_m96XCGPNt6ZFjDdeVHEBQ_2a2ngJg5vX8qBxqKwHJyO6RN_-3YtlKtU4QaKoRHtkd1tzb7ou_uNAv3UF8HJl-_Lg-HNLssdrAiBXgr-/s1600/figure5.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjVZv45R_-Oyg7z37sPNrGYutVzM63rVNyfmSEG_m96XCGPNt6ZFjDdeVHEBQ_2a2ngJg5vX8qBxqKwHJyO6RN_-3YtlKtU4QaKoRHtkd1tzb7ou_uNAv3UF8HJl-_Lg-HNLssdrAiBXgr-/s320/figure5.jpg" width="320" /></a></div> <br /> Finally, another example of how we can make aesthetics of the plot a function of the data is by controlling the size of the plotted elements. Let's make the size of the points proportion to the amount of official development aid the countries received in 2002 as a proportion of their GDP (Aid2002 variable in Norris' data). We do so by specifying <code>size=Aid2002</code> in the original aesthetics:<br /> <br /> <pre class="brush: plain">ggplot(norris.data, aes(log(GDP2000), Polity3, size=Aid2002)) + scale_y_continuous("Polity Score, 2000 (0=Authoritarian, 20=Democracy)") + scale_x_continuous("Log of Per Capita GDP (2000)") + geom_text(aes(label=Natmap), color=I("black")) + labs(size="2002 Aid as \n % of GDP")</pre> <div class="separator" style="clear: both; text-align: center;"> <a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfmUdduYFveLIQYImhhuiuS5XCAhi7AnxgKgWybGnQJLBoWfNLwRKdYF51zc7No7GhCYIFvkjPrH5fkgMci878Vsnkeb02JY-SpdkL3jkbuztV33xP4py8KUjMwkPrUvtuLRjsYOC91XQq/s1600/figure6.jpg" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" height="320" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgfmUdduYFveLIQYImhhuiuS5XCAhi7AnxgKgWybGnQJLBoWfNLwRKdYF51zc7No7GhCYIFvkjPrH5fkgMci878Vsnkeb02JY-SpdkL3jkbuztV33xP4py8KUjMwkPrUvtuLRjsYOC91XQq/s320/figure6.jpg" width="320" /></a></div> <div style='clear: both;'></div> </div> <div class='post-footer'> <div class='post-footer-line post-footer-line-1'><span class='post-author vcard'> Posted by <span class='fn'> <a href='https://www.blogger.com/profile/14524776572196611296' rel='author' title='author profile'> Konstantin Kashin </a> </span> </span> <span class='post-timestamp'> at <a class='timestamp-link' href='http://codealamode.blogspot.com/2012/02/better-graphics-in-r-intro-to-ggplot2.html' rel='bookmark' title='permanent link'><abbr class='published' title='2012-02-12T14:28:00-05:00'>2:28&#8239;PM</abbr></a> </span> <span class='post-comment-link'> <a class='comment-link' href='http://codealamode.blogspot.com/2012/02/better-graphics-in-r-intro-to-ggplot2.html#comment-form' onclick=''>0 comments</a> </span> <span class='post-icons'> <span class='item-control blog-admin pid-2049479395'> <a href='https://www.blogger.com/post-edit.g?blogID=1300235002903206586&postID=4696181333095615375&from=pencil' title='Edit Post'> <img alt='' class='icon-action' height='18' src='https://resources.blogblog.com/img/icon18_edit_allbkg.gif' width='18'/> </a> </span> </span> <div class='post-share-buttons goog-inline-block'> <a class='goog-inline-block share-button sb-email' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=4696181333095615375&target=email' target='_blank' title='Email This'><span class='share-button-link-text'>Email This</span></a><a class='goog-inline-block share-button sb-blog' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=4696181333095615375&target=blog' onclick='window.open(this.href, "_blank", "height=270,width=475"); return false;' target='_blank' title='BlogThis!'><span class='share-button-link-text'>BlogThis!</span></a><a class='goog-inline-block share-button sb-twitter' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=4696181333095615375&target=twitter' target='_blank' title='Share to X'><span class='share-button-link-text'>Share to X</span></a><a class='goog-inline-block share-button sb-facebook' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=4696181333095615375&target=facebook' onclick='window.open(this.href, "_blank", "height=430,width=640"); return false;' target='_blank' title='Share to Facebook'><span class='share-button-link-text'>Share to Facebook</span></a><a class='goog-inline-block share-button sb-pinterest' href='https://www.blogger.com/share-post.g?blogID=1300235002903206586&postID=4696181333095615375&target=pinterest' target='_blank' title='Share to Pinterest'><span class='share-button-link-text'>Share to Pinterest</span></a> </div> </div> <div class='post-footer-line post-footer-line-2'><span class='post-labels'> Labels: <a href='http://codealamode.blogspot.com/search/label/data%20visualization' rel='tag'>data visualization</a>, <a href='http://codealamode.blogspot.com/search/label/ggplot2' rel='tag'>ggplot2</a>, <a href='http://codealamode.blogspot.com/search/label/R' rel='tag'>R</a> </span> </div> <div class='post-footer-line post-footer-line-3'></div> </div> </div> </div> </div></div> </div> <div class='blog-pager' id='blog-pager'> <span id='blog-pager-older-link'> <a class='blog-pager-older-link' href='http://codealamode.blogspot.com/search/label/ggplot2?updated-max=2012-02-12T14:28:00-05:00&amp;max-results=20&amp;start=20&amp;by-date=false' id='Blog1_blog-pager-older-link' title='Older Posts'>Older Posts</a> </span> <a class='home-link' href='http://codealamode.blogspot.com/'>Home</a> </div> <div class='clear'></div> <div class='blog-feeds'> <div class='feed-links'> Subscribe to: <a class='feed-link' href='http://codealamode.blogspot.com/feeds/posts/default' target='_blank' type='application/atom+xml'>Posts (Atom)</a> </div> </div> </div></div> </div> </div> <div class='column-left-outer'> <div class='column-left-inner'> <aside> </aside> </div> </div> <div class='column-right-outer'> <div class='column-right-inner'> <aside> <div class='sidebar section' id='sidebar-right-1'><div class='widget BlogArchive' data-version='1' id='BlogArchive1'> <h2>Blog Archive</h2> <div class='widget-content'> <div id='ArchiveList'> <div id='BlogArchive1_ArchiveList'> <ul class='hierarchy'> <li class='archivedate expanded'> <a class='toggle' href='javascript:void(0)'> <span class='zippy toggle-open'> &#9660;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2013/'> 2013 </a> <span class='post-count' dir='ltr'>(5)</span> <ul class='hierarchy'> <li class='archivedate expanded'> <a class='toggle' href='javascript:void(0)'> <span class='zippy toggle-open'> &#9660;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2013/09/'> September </a> <span class='post-count' dir='ltr'>(1)</span> <ul class='posts'> <li><a href='http://codealamode.blogspot.com/2013/09/note-on-bivariate-regression-connecting.html'>Note on Bivariate Regression: Connecting Practice ...</a></li> </ul> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2013/08/'> August </a> <span class='post-count' dir='ltr'>(1)</span> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2013/07/'> July </a> <span class='post-count' dir='ltr'>(1)</span> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2013/06/'> June </a> <span class='post-count' dir='ltr'>(2)</span> </li> </ul> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2012/'> 2012 </a> <span class='post-count' dir='ltr'>(9)</span> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2012/08/'> August </a> <span class='post-count' dir='ltr'>(3)</span> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2012/04/'> April </a> <span class='post-count' dir='ltr'>(3)</span> </li> </ul> <ul class='hierarchy'> <li class='archivedate collapsed'> <a class='toggle' href='javascript:void(0)'> <span class='zippy'> &#9658;&#160; </span> </a> <a class='post-count-link' href='http://codealamode.blogspot.com/2012/02/'> February </a> <span class='post-count' dir='ltr'>(3)</span> </li> </ul> </li> </ul> </div> </div> <div class='clear'></div> </div> </div><div class='widget Profile' data-version='1' id='Profile1'> <h2>About Me</h2> <div class='widget-content'> <dl class='profile-datablock'> <dt class='profile-data'> <a class='profile-name-link g-profile' href='https://www.blogger.com/profile/14524776572196611296' rel='author' style='background-image: url(//www.blogger.com/img/logo-16.png);'> Konstantin Kashin </a> </dt> <dd class='profile-textblock'>PhD Student in the Department of Government at Harvard University, specializing in political methodology and comparative politics.</dd> </dl> <a class='profile-link' href='https://www.blogger.com/profile/14524776572196611296' rel='author'>View my complete profile</a> <div class='clear'></div> </div> </div></div> </aside> </div> </div> </div> <div style='clear: both'></div> <!-- columns --> </div> <!-- main --> </div> </div> <div class='main-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> <footer> <div class='footer-outer'> <div class='footer-cap-top cap-top'> <div class='cap-left'></div> <div class='cap-right'></div> </div> <div class='fauxborder-left footer-fauxborder-left'> <div class='fauxborder-right footer-fauxborder-right'></div> <div class='region-inner footer-inner'> <div class='foot no-items section' id='footer-1'></div> <!-- outside of the include in order to lock Attribution widget --> <div class='foot section' id='footer-3'><div class='widget Attribution' data-version='1' id='Attribution1'> <div class='widget-content' style='text-align: center;'> &#169; 2012 Konstantin Kashin. Awesome Inc. theme. Theme images by <a href='http://www.istockphoto.com/googleimages.php?id=9171196&platform=blogger&langregion=en' target='_blank'>rajareddychadive</a>. Powered by <a href='https://www.blogger.com' target='_blank'>Blogger</a>. </div> <div class='clear'></div> </div></div> </div> </div> <div class='footer-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </footer> <!-- content --> </div> </div> <div class='content-cap-bottom cap-bottom'> <div class='cap-left'></div> <div class='cap-right'></div> </div> </div> </div> <script type='text/javascript'> window.setTimeout(function() { document.body.className = document.body.className.replace('loading', ''); }, 10); </script> <script type="text/javascript" src="https://www.blogger.com/static/v1/widgets/2725212210-widgets.js"></script> <script type='text/javascript'> window['__wavt'] = 'AOuZoY4ekE2EZ0Bpkxtoz1kPo0SsDoUd2Q:1740598596947';_WidgetManager._Init('//www.blogger.com/rearrange?blogID\x3d1300235002903206586','//codealamode.blogspot.com/search/label/ggplot2','1300235002903206586'); _WidgetManager._SetDataContext([{'name': 'blog', 'data': {'blogId': '1300235002903206586', 'title': 'Code \xe0 la Mode', 'url': 'http://codealamode.blogspot.com/search/label/ggplot2', 'canonicalUrl': 'http://codealamode.blogspot.com/search/label/ggplot2', 'homepageUrl': 'http://codealamode.blogspot.com/', 'searchUrl': 'http://codealamode.blogspot.com/search', 'canonicalHomepageUrl': 'http://codealamode.blogspot.com/', 'blogspotFaviconUrl': 'http://codealamode.blogspot.com/favicon.ico', 'bloggerUrl': 'https://www.blogger.com', 'hasCustomDomain': false, 'httpsEnabled': true, 'enabledCommentProfileImages': true, 'gPlusViewType': 'FILTERED_POSTMOD', 'adultContent': false, 'analyticsAccountNumber': '', 'encoding': 'UTF-8', 'locale': 'en', 'localeUnderscoreDelimited': 'en', 'languageDirection': 'ltr', 'isPrivate': false, 'isMobile': false, 'isMobileRequest': false, 'mobileClass': '', 'isPrivateBlog': false, 'isDynamicViewsAvailable': true, 'feedLinks': '\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Code \xe0 la Mode - Atom\x22 href\x3d\x22http://codealamode.blogspot.com/feeds/posts/default\x22 /\x3e\n\x3clink rel\x3d\x22alternate\x22 type\x3d\x22application/rss+xml\x22 title\x3d\x22Code \xe0 la Mode - RSS\x22 href\x3d\x22http://codealamode.blogspot.com/feeds/posts/default?alt\x3drss\x22 /\x3e\n\x3clink rel\x3d\x22service.post\x22 type\x3d\x22application/atom+xml\x22 title\x3d\x22Code \xe0 la Mode - Atom\x22 href\x3d\x22https://www.blogger.com/feeds/1300235002903206586/posts/default\x22 /\x3e\n', 'meTag': '\x3clink rel\x3d\x22me\x22 href\x3d\x22https://www.blogger.com/profile/14524776572196611296\x22 /\x3e\n', 'adsenseHostId': 'ca-host-pub-1556223355139109', 'adsenseHasAds': false, 'adsenseAutoAds': false, 'boqCommentIframeForm': true, 'loginRedirectParam': '', 'view': '', 'dynamicViewsCommentsSrc': '//www.blogblog.com/dynamicviews/4224c15c4e7c9321/js/comments.js', 'dynamicViewsScriptSrc': '//www.blogblog.com/dynamicviews/f04e4d11442969dd', 'plusOneApiSrc': 'https://apis.google.com/js/platform.js', 'disableGComments': true, 'interstitialAccepted': false, 'sharing': {'platforms': [{'name': 'Get link', 'key': 'link', 'shareMessage': 'Get link', 'target': ''}, {'name': 'Facebook', 'key': 'facebook', 'shareMessage': 'Share to Facebook', 'target': 'facebook'}, {'name': 'BlogThis!', 'key': 'blogThis', 'shareMessage': 'BlogThis!', 'target': 'blog'}, {'name': 'X', 'key': 'twitter', 'shareMessage': 'Share to X', 'target': 'twitter'}, {'name': 'Pinterest', 'key': 'pinterest', 'shareMessage': 'Share to Pinterest', 'target': 'pinterest'}, {'name': 'Email', 'key': 'email', 'shareMessage': 'Email', 'target': 'email'}], 'disableGooglePlus': true, 'googlePlusShareButtonWidth': 0, 'googlePlusBootstrap': '\x3cscript type\x3d\x22text/javascript\x22\x3ewindow.___gcfg \x3d {\x27lang\x27: \x27en\x27};\x3c/script\x3e'}, 'hasCustomJumpLinkMessage': false, 'jumpLinkMessage': 'Read more', 'pageType': 'index', 'searchLabel': 'ggplot2', 'pageName': 'ggplot2', 'pageTitle': 'Code \xe0 la Mode: ggplot2'}}, {'name': 'features', 'data': {}}, {'name': 'messages', 'data': {'edit': 'Edit', 'linkCopiedToClipboard': 'Link copied to clipboard!', 'ok': 'Ok', 'postLink': 'Post Link'}}, {'name': 'template', 'data': {'name': 'custom', 'localizedName': 'Custom', 'isResponsive': false, 'isAlternateRendering': false, 'isCustom': true, 'variant': 'icy', 'variantId': 'icy'}}, {'name': 'view', 'data': {'classic': {'name': 'classic', 'url': '?view\x3dclassic'}, 'flipcard': {'name': 'flipcard', 'url': '?view\x3dflipcard'}, 'magazine': {'name': 'magazine', 'url': '?view\x3dmagazine'}, 'mosaic': {'name': 'mosaic', 'url': '?view\x3dmosaic'}, 'sidebar': {'name': 'sidebar', 'url': '?view\x3dsidebar'}, 'snapshot': {'name': 'snapshot', 'url': '?view\x3dsnapshot'}, 'timeslide': {'name': 'timeslide', 'url': '?view\x3dtimeslide'}, 'isMobile': false, 'title': 'Code \xe0 la Mode', 'description': 'Assortment of R, Python, and other code with applications to the social sciences', 'url': 'http://codealamode.blogspot.com/search/label/ggplot2', 'type': 'feed', 'isSingleItem': false, 'isMultipleItems': true, 'isError': false, 'isPage': false, 'isPost': false, 'isHomepage': false, 'isArchive': false, 'isSearch': true, 'isLabelSearch': true, 'search': {'label': 'ggplot2', 'resultsMessage': 'Showing posts with the label ggplot2', 'resultsMessageHtml': 'Showing posts with the label \x3cspan class\x3d\x27search-label\x27\x3eggplot2\x3c/span\x3e'}}}]); _WidgetManager._RegisterWidget('_NavbarView', new _WidgetInfo('Navbar1', 'navbar', document.getElementById('Navbar1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_HeaderView', new _WidgetInfo('Header1', 'header', document.getElementById('Header1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogView', new _WidgetInfo('Blog1', 'main', document.getElementById('Blog1'), {'cmtInteractionsEnabled': false, 'navMessage': 'Showing posts with label \x3cb\x3eggplot2\x3c/b\x3e. \x3ca href\x3d\x22http://codealamode.blogspot.com/\x22\x3eShow all posts\x3c/a\x3e', 'lightboxEnabled': true, 'lightboxModuleUrl': 'https://www.blogger.com/static/v1/jsbin/177302283-lbx.js', 'lightboxCssUrl': 'https://www.blogger.com/static/v1/v-css/1964470060-lightbox_bundle.css'}, 'displayModeFull')); _WidgetManager._RegisterWidget('_BlogArchiveView', new _WidgetInfo('BlogArchive1', 'sidebar-right-1', document.getElementById('BlogArchive1'), {'languageDirection': 'ltr', 'loadingMessage': 'Loading\x26hellip;'}, 'displayModeFull')); _WidgetManager._RegisterWidget('_ProfileView', new _WidgetInfo('Profile1', 'sidebar-right-1', document.getElementById('Profile1'), {}, 'displayModeFull')); _WidgetManager._RegisterWidget('_AttributionView', new _WidgetInfo('Attribution1', 'footer-3', document.getElementById('Attribution1'), {}, 'displayModeFull')); </script> </body> </html>

Pages: 1 2 3 4 5 6 7 8 9 10