18006

How can I create a custom colour scale using ggplot2 and geom_tile?

Question:

I would like to modify the colour gradient in order to match a set of predefined thresholds/cutpoints and colours. How can I do this?

Cutoff values: -0.103200, 0.007022, 0.094090, 0.548600 Colors: "#EDF8E9", "#BAE4B3", "#74C476", "#238B45"

#Create sample data pp <- function (n,r=4) { x <- seq(-r*pi, r*pi, len=n) df <- expand.grid(x=x, y=x) df$r <- sqrt(df$x^2 + df$y^2) df$z <- cos(df$r^2)*exp(-df$r/6) df } pp(20)->data #create the plot library(ggplo2) p <- ggplot(pp(20), aes(x=x,y=y)) p + geom_tile(aes(fill=z)) #Generate custom colour ramp library(RColorBrewer) cols <- brewer.pal(4, "Greens")

Answer1:

You may try scale_fill_brewer. First, bin your z values:

df <- pp(20) df$z_bin <- cut(df$z, breaks = c(-Inf, -0.103200, 0.007022, 0.094090, 0.548600))

Plot:

ggplot(data = df, aes(x = x, y = y, fill = z_bin)) + geom_tile() + scale_fill_brewer(palette = "Greens")

<img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/oTRGF.jpg" data-original="https://i.stack.imgur.com/oTRGF.jpg" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" />

Answer2:

Use cut and match the bins to your colors. My code assumes -0.103200 is minimum of your vector (to sort the number of bins).

trh <- c(-0.103200, 0.007022, 0.094090, 0.548600, Inf) colors <- c("#EDF8E9", "#BAE4B3", "#74C476", "#238B45") x <- runif(30, min = -0.103200, max = 1) xc <- cut(x, breaks = trh) colors[xc]

Recommend

  • How to read text file's key, value pair using pandas?
  • Special functions in Matlab
  • Cosine similarity yields 'nan' values
  • Calculate the point of intersection of circle and line through the center
  • MPI_Gather() the central elements into a global matrix
  • Prime numbers using Sieve of Atkin with BigInteger
  • Computing CCA through three approaches
  • how can I solve transcendental equation?
  • MATLAB Heart Curve
  • not able to Open Powerpoint using VBA
  • CMCalibratedMagneticField doesn't update
  • Parallelize an R Script
  • Omit template arguments when create an instance of template class from another instance of template
  • How do I conditionally select a field from one of two tables?
  • Python multiprocessing of a sum
  • Read the values from XML file
  • C# fibonacci function returning errors
  • Reading a file into a multidimensional array
  • Android application: how to use the camera and grab the image bytes?
  • How to clear text inside text field when radio button is select
  • Scrapy recursive link crawler
  • Hits per day in Google Big Query
  • how does django model after text[] in postgresql [duplicate]
  • embed rChart in Markdown
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • costura.fody for a dll that references another dll
  • Binding checkboxes to object values in AngularJs
  • Observable and ngFor in Angular 2
  • How can I use `wmic` in a Windows PE script?
  • UserPrincipal.Current returns apppool on IIS
  • Unable to use reactive element in my shiny app
  • java string with new operator and a literal
  • Net Present Value in Excel for Grouped Recurring CF
  • jQuery Masonry / Isotope and fluid images: Momentary overlap on window resize
  • How to load view controller without button in storyboard?