1

I have matrix data and would like to create heat map using geom_tile(). geom_tile() successfully functioned, but the limit of "value" seemed to be lengthout. (For example, over 100 value is the same color breaks) . I would like to compare the result with other dataset, so I think the same color breaks among graphs is better.

γ€€Var1 <- c(0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162,0,3,31,316,3162)
  Var2 <- c(0,0,0,0,0,3,3,3,3,3,31,31,31,31,31,316,316,316,316,316, 3162, 3162, 3162, 3162, 3162)
  value <- c(67,79,90,20, 5,10,88,82,11, 1, 2,17, 107,41, 4, 3,15,81,66,12, 1, 3,14,16,23)
  longData <- data.frame(cbind(Var1, Var2, value))      

ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) + 
      geom_tile(aes(fill=value)) + 
      scale_fill_steps(low="grey90", high="red",  breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
      labs(x="2000", y="2007", title="Matrix")+
      geom_text(aes(label = value))

enter image description here

-----update-----

I would like to compare these two graphs, but the indicated color is different depending on the value.

Var1 <- c(3, 31,316,3162,3,31,316,3162,3,31, 316,3162,3,31,316, 3162, 31,316,3162)
Var2 <- c(0,0,0,0,3,3,3,3,31,31,31,31,316,316,316,316,3162,3162,3162)
value <- c(36,34,9,4,129,59,12,2,37,277,50,10,3,23,106,22,5,9, 31)
longData2 <- data.frame(cbind(Var1, Var2, value))

ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) + 
      geom_tile(aes(fill=value)) + 
      scale_fill_steps(low="grey90", high="red",  breaks=c(0, 10, 25, 50, 100, 150, 200, 300)) +
      labs(x="2007", y="2012", title="Matrix")+
      geom_text(aes(label = value)) 

enter image description here

marie
  • 315
  • 1
  • 9
  • I don't understand what you are asking. You seem to be setting the breaks explicitly with `breaks=c(0, 10, 25, 50, 100, 150, 200, 300)`. Are those not the breaks you want? – MrFlick Jun 23 '21 at 05:06
  • Did you also use `breaks=c(0, 10, 25, 50, 100, 150, 200, 300)` in your second plot? It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input that can be used to test and verify possible solutions. Share data with a `dput()` so we can copy/paste it into R. – MrFlick Jun 23 '21 at 05:58
  • Thank you for comments! Yes, I used the same breaks () in the second plot. I updated to include the actual data. – marie Jun 23 '21 at 06:26

1 Answers1

2

This does seem to be harder than I would have thought. It seems you need to also take care of the rescaler. To keep both the same, you can define the scale once, for example

my_fill <- scale_fill_steps(low="grey90", high="red",  
                         breaks=c(0, 10, 25, 50, 100, 150, 200, 300), 
                         rescale=function(x, ...) scales::rescale(x, from=c(0, 300)),
                         limits=c(0,300))

And then use that for both plots

ggplot(longData, aes(x = as.character(Var1), y = as.character(Var2))) + 
  geom_tile(aes(fill=value)) + 
  my_fill +   
  labs(x="2007", y="2012", title="Matrix")+
  geom_text(aes(label = value))
ggplot(longData2, aes(x = as.character(Var1), y = as.character(Var2))) + 
  geom_tile(aes(fill=value)) + 
  my_fill + 
  labs(x="2007", y="2012", title="Matrix")+
  geom_text(aes(label = value)) 
MrFlick
  • 195,160
  • 17
  • 277
  • 295