0

I have been trying to figure out if I can diverge viridis from the black line, geom_contour(breaks = 5E-14). In essence, the black line is the midpoint and viridis palette would diverge there. Is this possible?

Thank you!

Design.matrix <- expand.grid(
A=seq(0.1E-9, 2E-9, by=0.05E-9), 
B=seq(1E-6, 100.E-6, by=2.5E-6))
Design.matrix$C <-Design.matrix$A * Design.matrix$B
Design.matrix$D <-(Design.matrix$A * Design.matrix$B) * Design.matrix$B
Design.matrix$Ratio <- Design.matrix$C/Design.matrix$D

library(ggplot2)
library(viridis)
(test <- ggplot(Design.matrix, aes(x = A, y = B, z = C, fill = C)) + 
theme(legend.title=element_blank()) +  
geom_raster(interpolate = T) + 
scale_fill_gradientn(colours = rev(viridis(8))) +
geom_contour(breaks = 5E-14, colour="black", size=1)) 
  • 1
    Please, make life easier for anyone trying to help you: provide a [complete reproducible example](https://stackoverflow.com/help/mcve) – Carlos Eduardo Lagosta Oct 12 '18 at 23:58
  • Viridis and all other palettes in the viridis package are sequential, they don't have a midpoint. In spite of that, there's probably some way to set a specific midpoint in a n colors palette using colorRamps or something like that, but will be much easier to use `scale_fill_gratient2` with colors similar to those used in viridis. – Carlos Eduardo Lagosta Oct 13 '18 at 01:30

1 Answers1

0

Here's one approach - you could just fill based on distance from C:

ggplot(Design.matrix, aes(x = A, y = B, z = C, fill = abs(C-5E-14))) + 
  theme(legend.title=element_blank()) +  
  geom_raster(interpolate = T) + 
  scale_fill_gradientn(colours = rev(viridis(8))) +
  geom_contour(breaks = 5E-14, colour="black", size=1)

enter image description here

Or, if you want to keep the fill axis as a scale measuring value, you could construct an artificial palette consisting of both viridis and its reverse. I used differing numbers of colors in each to approximate so that the divergent point would correspond with the black line.

ggplot(Design.matrix, aes(x = A, y = B, z = C, fill = C)) + 
  theme(legend.title=element_blank()) +  
  geom_raster(interpolate = T) + 
  scale_fill_gradientn(colours = c(viridis(3),rev(viridis(8)))) +
  geom_contour(breaks = 5E-14, colour="black", size=1)

enter image description here

Jon Spring
  • 55,165
  • 4
  • 35
  • 53