1

So my problem is, I have a data frame that I am plotting with geom_hex from ggplots. and my command looks like this:

ggplot(data, aes(x=var1,y=var2))+geom_hex(bins=20)+facet_grid(fac1 ~ fac2,scales="free")

The problem I am having is that the colouring scheme for the counts is shared across all graphs. I am wondering if there is any quick way to generate a count color scheme per row (or column) of graphs. I tried playing with scales, but seems that that this only works on the scales on y and x axis, and not with the histogram colors and histogram color legend. thnx! Here is an example of the data:

fac1<-c(rep(1, 6000), rep(2, 1000))
fac2<-c(rep("a", 3000), rep("b", 3000),rep("a", 500), rep("b", 500))
var1<-rnorm(7000)
var2<-rnorm(7000)
data<-data.frame(fac1,fac2,var1,var2)
ggplot(data, aes(x=var1,y=var2))+geom_hex(bins=20)+facet_grid(fac1 ~ fac2,scales="free")

Because there is so much more data from one factor, the color scheme is dominated by the first row of graphs, and would like to have the same coloring scheme but adjusted by the counts of every row.

Jorge Kageyama
  • 393
  • 4
  • 17
  • Please make a reproducible example. You may use the following [guide](http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). – tonytonov Feb 04 '15 at 15:27
  • added an example, thank you for the sugestion. – Jorge Kageyama Feb 04 '15 at 16:12
  • There is no simple way to do so. Some related questions: [one](http://stackoverflow.com/questions/4221360/ggplot2-separate-color-scale-per-facet), [two](http://stackoverflow.com/questions/3805029/different-legends-and-fill-colours-for-facetted-ggplot). Solutions are either to separate the first row from the second or experiment with low-level grid approach. I'd go with the first option. – tonytonov Feb 04 '15 at 16:21

2 Answers2

0

Good question; based on this answer from 2010 Different legends and fill colours for facetted ggplot? Hadley Wickham indicates that you cannot have multiple legend scales per plot.

A simple way to get around this issue in your case would be to use the gridExtra package.

require('gridExtra')
p1<-ggplot(data[data$fac1==1 & data$fac2=="a",],  aes(x=var1,y=var2))+geom_hex(bins=20)
p2<-ggplot(data[data$fac1==2 & data$fac2=="a",],  aes(x=var1,y=var2))+geom_hex(bins=20)
p3<-ggplot(data[data$fac1==1 & data$fac2=="b",],  aes(x=var1,y=var2))+geom_hex(bins=20)
p4<-ggplot(data[data$fac1==2 & data$fac2=="b",],  aes(x=var1,y=var2))+geom_hex(bins=20)
grobframe <- arrangeGrob(p1,p2,p3,p4 ,ncol=2, nrow=2,
                         main = textGrob("Plots", gp = gpar(fontsize=12, fontface="bold.italic", fontsize=12)))

printing grobframe produces the following plot, which I believe is what you want.

enter image description here

Community
  • 1
  • 1
bjoseph
  • 2,116
  • 17
  • 24
0

Here's my comment expanded into an answer. Your safest bet is the following approach:

library(gridExtra)
p1 <- ggplot(data[data$fac1==1, ], aes(x=var1,y=var2)) + 
        geom_hex(bins=20) + facet_grid(fac1 ~ fac2,scales="free") + xlab("")
p2 <- ggplot(data[data$fac1==2, ], aes(x=var1,y=var2)) + 
        geom_hex(bins=20) + facet_grid(fac1 ~ fac2,scales="free") + 
        scale_fill_gradient(low = "red", high = "white")
grid.arrange(p1, p2)

enter image description here

tonytonov
  • 25,060
  • 16
  • 82
  • 98