Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had to change the <code>qplot</code> loop call slightly (i.e. put the factors in the data frame) as it was throwing a mismatched size error. I'm not including that bit since that part is obviously working in your environment or it was an errant paste.</p> <p>Try adjusting your <code>widths</code> units like this:</p> <pre><code>widths=unit(c(1000,50),"pt") </code></pre> <p>And you'll get something a bit closer to what you were probably expecting:</p> <p>And, I can paste code now a few months later :-)</p> <pre><code>library(ggplot2) library(gridExtra) df &lt;- data.frame(price=matrix(sample(1:1000, 100, replace = TRUE), ncol = 1)) df$size1 = 1:nrow(df) df$size1 = cut(df$size1, breaks=11) df=df[sample(nrow(df)),] df$size2 = 1:nrow(df) df$size2 = cut(df$size2, breaks=11) df=df[sample(nrow(df)),] df$clarity = 1:nrow(df) df$clarity = cut(df$clarity, breaks=6) # Create one graph for each size1, plotting the median price vs. the size2 by clarity: for (c in 1:length(table(df$size1))) { mydf = df[df$size1==names(table(df$size1))[c],] mydf = aggregate(mydf$price, by=list(mydf$size2, mydf$clarity),median); names(mydf)[1] = 'size2' names(mydf)[2] = 'clarity' names(mydf)[3] = 'median_price' mydf$clarity &lt;- factor(mydf$clarity) assign(paste("p", c, sep=""), qplot(data=mydf, x=as.numeric(size2), y=median_price, group=clarity, geom="line", colour=clarity, xlab = "number of samples", ylab = "median price", main = paste("region number is ",c, sep=''), plot.title=element_text(size=10)) + scale_colour_discrete(name = "clarity") + theme_bw() + theme(axis.title.x=element_text(size = rel(0.8)), axis.title.y=element_text(size = rel(0.8)), axis.text.x=element_text(size=8), axis.text.y=element_text(size=8) )) } # Use gridExtra to arrange the 11 plots: g_legend&lt;-function(a.gplot){ tmp &lt;- ggplot_gtable(ggplot_build(a.gplot)) leg &lt;- which(sapply(tmp$grobs, function(x) x$name) == "guide-box") legend &lt;- tmp$grobs[[leg]] return(legend)} mylegend&lt;-g_legend(p1) grid.arrange(arrangeGrob(p1 + theme(legend.position="none"), p2 + theme(legend.position="none"), p3 + theme(legend.position="none"), p4 + theme(legend.position="none"), p5 + theme(legend.position="none"), p6 + theme(legend.position="none"), p7 + theme(legend.position="none"), p8 + theme(legend.position="none"), p9 + theme(legend.position="none"), p10 + theme(legend.position="none"), p11 + theme(legend.position="none"), top ="Main title", left = ""), mylegend, widths=unit(c(1000,50),"pt"), nrow=1) </code></pre> <p><img src="https://i.stack.imgur.com/JbOqL.png" alt="preview here"></p> <p><strong>Edit</strong> (16/07/2015): with <code>gridExtra</code> >= 2.0.0, the <code>main</code> parameter has been renamed <code>top</code>. </p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload