0

I have the following code which generates a list of plots of k=2 classes.

library(ggplot2)
library(cumplyr)
library(scales)
library(reshape2)
library(RColorBrewer)
library(tools)
library(gridExtra)

jet.colors <- colorRampPalette(c("#00007F", "blue", "#007FFF", "cyan","#7FFF7F", "yellow", "#FF7F00", "red", "#7F0000"))
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))

x   = 1:50
y   = 1:50
pts = cartesian_product(c('x','y'))


make_df <- function(i) {
    k = if(i <= 5) 1 else 2
    d1 = cbind(pts, runif(nrow(pts),min=0,max=10*i), 1)
    colnames(d1) = c("x","y","val", "k")
    return(d1)
}

dflist = lapply(as.list(1:10),make_df)


make_plot <- function(data, gmin = 0, gmax = 100) {
    p1 <- ggplot(data)
    p1 <- p1 + geom_tile(aes(x = x, y = y, fill = val))
    p1 <- p1 + scale_fill_gradientn(colours = myPalette(gmax), limits=c(gmin,gmax))
    return(p1)
}

plotlist = lapply(dflist, make_plot)
g = do.call(arrangeGrob, plotlist)

I am having trouble with the final arranging of the plot. The plots can be of type k=1 or type k = 2. I would like to:

  • arrange the plots in a 2 x 5 grid,
  • have only 1 legend
  • Label each row of plots with the appropriate k value
  • Have the plots along the row quote close to each other, perhaps just a small gap between each

I have been spinning my wheels with this for a few hours to no avail!

Thanks!

stevejb
  • 2,414
  • 5
  • 26
  • 41
  • Is there a specific reason why you're not using `facet_grid`? It sounds exactly like it would produce what you want. – BrodieG Mar 20 '14 at 20:35
  • BrodieG that specific reason would be my ignorance concerning its existence :). Thank you for the reference. – stevejb Mar 20 '14 at 20:53
  • If you don't want to use `facet_grid` for some reason, you can get some inspiration from [this answer](http://stackoverflow.com/questions/21274527/how-to-show-all-the-labels-in-x-axis-45-degree-in-r-2x2-bar-plot/21349495#21349495) – Jaap Mar 20 '14 at 21:02

2 Answers2

2

This is what I did with facet_wrap (grid doesn't really make sense in this case):

ggplot(df, aes(x=x, y=y, fill=val)) + 
  geom_tile() + 
  scale_fill_gradientn(colours = myPalette(100), limits=c(0,100)) +
  facet_wrap(~ k, nrow=2) +
  theme(axis.text.x=element_text(angle=90))

Produces:

enter image description here

I had to modify your code a little bit too:

make_df <- function(i) {
  k = if(i <= 5) 1 else 2
  d1 = cbind(pts, runif(nrow(pts),min=0,max=10*i), i)  # I think you want the last value to be `i`, but I'm guessing
  colnames(d1) = c("x","y","val", "k")
  return(d1)
}
pts = expand.grid(x, y)  # cartesian_product() didn't work for me, but I think this is equivalent
dflist = lapply(as.list(1:10),make_df)
df <- do.call(rbind, dflist)
BrodieG
  • 51,669
  • 9
  • 93
  • 146
  • it also depends on whether 1,6 2,7 3,8 etc are related. you may want to consider `facet_grid(k~p)` if the columns represent something, e.g. 5 blocks with 2 treatments. – Randy Lai Mar 20 '14 at 21:11
1

If the columns mean something, you can do something like this

library(ggplot2)

nplot = 10

# assume that the first 5 are of the same group, and last 5 are of the same group
dlist = lapply(1:10, function(i){
    x = rnorm(100)
    y = rnorm(100)
    p = i*(i<=5)+(i-5)*(i>5)
    k = 1*(i<=5)+2*(i>5)
    data.frame(x,y,p,k)
})

# create a large dataframe
D = do.call(rbind, dlist)
ggplot(D, aes(x=x,y=y)) + geom_point() + facet_grid(k~p) 

ggplot

Randy Lai
  • 3,084
  • 2
  • 22
  • 23