Edit 2 stat_hex_bin geoms separately ggplot2

I'll start by suggesting my code:

x <- runif(1000,0, 5) y <- c(runif(500, 0, 2), runif(500, 3,5)) A <- data.frame("X"=x,"Y"=y[1:500]) B <- data.frame("X"=x,"Y"=y[501:1000]) ggplot() + stat_bin_hex(data=A, aes(x=X, y=Y), bins=10) + stat_bin_hex(data=B, aes(x=X, y=Y), bins=10) + scale_fill_continuous(low="red4", high="#ED1A3A") 

He creates the following chart: enter image description here

Now I want the lower hexagons to fit a different scale. Namely, from dark green to light green. How can i achieve this?

Update: As you can see from the answers so far, I ask myself if there is a solution without using alpha scales. In addition, the use of two graphs without fields or something like that is not an option for my specific application. Although both of them are legal answers :)

+6
source share
3 answers

Instead of trying to get two different fill scales on the same chart, you can change the colors of the lower values ​​after plotting. The main idea is to have two graphs with different filling scales, and then copy over certain parts from one section to another.

 # Base plot p <- ggplot() + stat_bin_hex(data=A, aes(x=X, y=Y), bins=10) + stat_bin_hex(data=B, aes(x=X, y=Y), bins=10) # Produce two plots with different fill colours p1 <- p + scale_fill_continuous(low="red4", high="#ED1A3A") p2 <- p + scale_fill_continuous(low="darkgreen", high="lightgreen") # Get fill colours for second plot and overwrite the corresponding # values in the first plot g1 <- ggplot_build(p1) g2 <- ggplot_build(p2) g1$data[[1]][,"fill"] <- g2$data[[1]][,"fill"] # You can draw this now but there is only one legend grid.draw(ggplot_gtable(g1)) 

To have two legends, you can join the legends of two graphs together

 # Bind the legends from the two plots together g1 <- ggplot_gtable(g1) g2 <- ggplot_gtable(g2) g1$grobs[[grep("guide", g1$layout$name )]] <- rbind(g1$grobs[[grep("guide", g1$layout$name )]], g2$grobs[[grep("guide", g2$layout$name )]] ) grid.newpage() grid.draw(g1) 

Submission (from set.seed(10) to data generation)

enter image description here

+4
source

This should provide more or less what you want.

  ggplot() + stat_bin_hex(data=A, aes(x=X, y=Y, alpha=..count..), bins=10,fill="green") + stat_bin_hex(data=B, aes(x=X, y=Y, alpha=..count..), bins=10,fill="red") 

enter image description here

To avoid the appearance of gray due to alpha, it was possible to lay the plot with another white plot in the same place and darken the colors a little, as suggested by MOT in the comments

  #just the red to show the impact due to scale_alpha ggplot() +scale_alpha_continuous(range=c(0.5,1))+ stat_bin_hex(data=A, aes(x=X, y=Y), bins=10,fill="white",show.legend = TRUE) + + stat_bin_hex(data=A, aes(x=X, y=Y, alpha=..count..), bins=10,fill="red",show.legend = TRUE) + + stat_bin_hex(data=B, aes(x=X, y=Y, alpha=..count..), bins=10,fill="green", show.legend=TRUE)+guides(fill=FALSE, alpha=FALSE) 

enter image description here

+2
source

Alternatively, if you want more options to play with colors, just create two graphs and delete all the space between the two graphs in combination with grid.arrange() .

 p1 <- ggplot() + stat_bin_hex(data=B, aes(x=X, y=Y), bins=10) + scale_fill_continuous(low="red4", high="#ED1A3A") + xlab("") + theme(axis.text.x=element_blank(), axis.ticks.x=element_blank(), plot.margin=unit(c(1,1,-0.5,1), "cm")) + scale_y_continuous(limits = c(2.5, 5.5)) p2 <- ggplot() + stat_bin_hex(data=A, aes(x=X, y=Y), bins=10) + scale_fill_continuous(low="darkgreen", high="green") + theme(plot.margin=unit(c(-0.5,1,1,1), "cm")) + scale_y_continuous(limits = c(-0.5, 2.5)) grid.arrange(p1,p2) 

enter image description here

+1
source

All Articles