{"id":232,"date":"2011-07-28T14:56:38","date_gmt":"2011-07-28T18:56:38","guid":{"rendered":"http:\/\/zhanxw.com\/blog\/2011\/07\/r-graphics\/"},"modified":"2011-09-11T15:41:28","modified_gmt":"2011-09-11T19:41:28","slug":"r-graphics","status":"publish","type":"post","link":"https:\/\/zhanxw.com\/blog\/2011\/07\/r-graphics\/","title":{"rendered":"R Graphics"},"content":{"rendered":"<p>Plot legend outside of the plot area<\/p>\n<p>primitive method:<\/p>\n<p>method1, hard code layout (<br \/><a href=\"https:\/\/stat.ethz.ch\/pipermail\/r-help\/2007-May\/132466.html\">https:\/\/stat.ethz.ch\/pipermail\/r-help\/2007-May\/132466.html<\/a>)<\/p>\n<p>layout(matrix(c(2,1), byrow = T), height=c(2,10))<br \/>par(mar=c(5,3,0,2))<br \/>plot(rnorm(100))<br \/>grid(10,10)<br \/>plot.new()<br \/>par(mar=c(0,0,0,0))<br \/>plot.window(c(0,1), c(0,1))<br \/>lsize = legend(0.5, .5, &quot;text&quot;, pch=&#8217;o&#8217;)<br \/>box(&quot;figure&quot;)<br \/>box(, lty = 2)<br \/>box(&quot;plot&quot;, col = &quot;red&quot;)<br \/>points(rnorm(100), type=&quot;n&quot;)<br \/># layout.show(2) : display the layout<\/p>\n<p>method 2, refer to:<\/p>\n<p>use xpd = NA options will not clip the legend outside of the plot area, however, you need to manual place the legend, that makes plot less pleasant. However, the folloing example works:<\/p>\n<p>data(iris)<br \/>par(xpd = NA, mai=rep(.5,4), mfrow=c(2,2))<br \/>for(i in 1:4)<br \/> boxplot(iris[,i]~iris[,5], main=colnames(iris)[i], col=2:4, xaxt=&quot;n&quot;)<br \/>\u00a0<br \/>width &lt;- 6 # width of the plot device in inches (it should be 7 inches by<br \/> # default but Windows does not implement this correctly)<br \/>leg &lt;- legend(0, 0, legend=levels(iris$Species), fill=2:4, hor=TRUE, plot=FALSE)<br \/>xcoord &lt;- grconvertX(width\/2, &quot;inches&quot;, &quot;user&quot;)-leg$rect$w\/2<br \/>ycoord &lt;- grconvertY(0, &quot;inches&quot;, &quot;user&quot;)+leg$rect$h<br \/>\u00a0<br \/>legend(xcoord, ycoord, legend=levels(iris$Species), fill=2:4, bty=&quot;n&quot;, hor=TRUE)<\/p>\n<\/p>\n<p>package ggplot2:<\/p>\n<p>Example:<\/p>\n<p>&gt; ggplot(dat,aes(x=x)) + <br \/>+ geom_histogram(aes(y=..density..,fill=&quot;Histogram&quot;),binwidth=0.5) + <br \/>+ stat_function(fun = dnorm, aes(colour= &quot;Density&quot;)) +<br \/>+ scale_x_continuous(&#8216;x&#8217;, limits = c(-4, 4)) + <br \/>+ opts(title = &quot;Histogram with Overlay&quot;) +<br \/>+ scale_fill_manual(name=&quot;&quot;,value=&quot;blue&quot;) + <br \/>+ scale_colour_manual(name=&quot;&quot;,value=&quot;red&quot;) + <br \/>+ scale_y_continuous(&#8216;Frequency&#8217;)+<br \/>+ opts(legend.key=theme_rect(fill=&quot;white&quot;,colour=&quot;white&quot;))+<br \/>+ opts(legend.background = theme_blank())<\/p>\n<p>package Lattice:<\/p>\n<p>Specify space = &quot;bottom&quot;, or space = &quot;right&quot; in the auto.keys or key parameter<\/p>\n<p>Example:<\/p>\n<p>&gt; xyplot( lat ~ long , data = quakes, key = list( points = list( col=c(&quot;orange&quot;,&quot;white&quot;,&quot;blue&quot;)), text = list( c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) ), space=&quot;bottom&quot;))<br \/>&gt; xyplot( lat ~ long , data = quakes, key = list( points = list( col=c(&quot;orange&quot;,&quot;white&quot;,&quot;blue&quot;)), text = list( c(&quot;a&quot;,&quot;b&quot;,&quot;c&quot;) ), space=&quot;right&quot;))<br \/>&gt; barchart(yield ~ variety | site, data = barley,<br \/> groups = year, layout = c(1,6), stack = TRUE,<br \/> auto.key = list(space = &quot;right&quot;),<br \/> ylab = &quot;Barley Yield (bushels\/acre)&quot;,<br \/> scales = list(x = list(rot = 45)))<\/p>\n<p><a href=\"https:\/\/stat.ethz.ch\/pipermail\/r-help\/2005-July\/075953.html\">https:\/\/stat.ethz.ch\/pipermail\/r-help\/2005-July\/075953.html<\/a><\/p>\n<p style=\"color:#008;text-align:right;\"><small><em>Powered by<\/em> <a href=\"http:\/\/www.qumana.com\/\">Qumana<\/a><\/small><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Plot legend outside of the plot area primitive method: method1, hard code layout (https:\/\/stat.ethz.ch\/pipermail\/r-help\/2007-May\/132466.html) layout(matrix(c(2,1), byrow = T), height=c(2,10))par(mar=c(5,3,0,2))plot(rnorm(100))grid(10,10)plot.new()par(mar=c(0,0,0,0))plot.window(c(0,1), c(0,1))lsize = legend(0.5, .5, &quot;text&quot;, pch=&#8217;o&#8217;)box(&quot;figure&quot;)box(, lty = 2)box(&quot;plot&quot;, col = &quot;red&quot;)points(rnorm(100), type=&quot;n&quot;)# layout.show(2) : display the layout method 2, refer to: use xpd = NA options will not clip the legend outside of the plot [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[15],"tags":[],"class_list":["post-232","post","type-post","status-publish","format-standard","hentry","category-statistics"],"_links":{"self":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/232","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/comments?post=232"}],"version-history":[{"count":0,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/posts\/232\/revisions"}],"wp:attachment":[{"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/media?parent=232"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/categories?post=232"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/zhanxw.com\/blog\/wp-json\/wp\/v2\/tags?post=232"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}