Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make a color scale with sharp transition in ggplot2

Tags:

r

colors

ggplot2

I am trying to create a color scale with a sharp color transition at one point. What I am currently doing is:

test <- data.frame(x = c(1:20), y = seq(0.01, 0.2, by = 0.01))

cutoff <- 0.10

ggplot(data = test,
       aes(x = as.factor(x), y = y, fill = log(y), width = 1, binwidth = 0)) + 
 geom_bar(stat = "identity") +
 scale_fill_gradientn(colours = c("red", "red", "yellow", "green"), 
                      values = rescale(log(c(0.01, cutoff - 0.0000000000000001, cutoff, 0.2))), 
                      breaks = c(log(cutoff)), label = c(cutoff))

It is producing the plots I want. But the position of the break in colorbar somehow varies depending on the cutoff. Sometimes below the value, sometimes above, sometimes on the line. Here are some plots with different cutoffs (0.05, 0.06, 0.1):

cut off at 0.05cut off at 0.06cut off at 0.10

What am I doing wrong? Or alternatively, is there a better way to create a such a color scale?

like image 400
Hengrui Jiang Avatar asked Jul 19 '15 08:07

Hengrui Jiang


People also ask

How do I specify colors in ggplot2?

To specify colors of the bar in Barplot in ggplot2, we use the scale_fill_manual function of the ggplot2 package. Within this function, we need to specify a color for each of the bars as a vector. We can use colors using names as well as hex codes.

How do you make a color scale?

On the Home tab, click Conditional Formatting. Point to Color Scales, and then click the color scale format that you want. The top color represents larger values, the center color, if any, represents middle values, and the bottom color represents smaller values.

How do you change the gradient color in R?

Key functions to change gradient colors The default gradient colors can be modified using the following ggplot2 functions: scale_color_gradient() , scale_fill_gradient() for sequential gradients between two colors. scale_color_gradient2() , scale_fill_gradient2() for diverging gradients.

How do you add color to a geom point?

To color the points in a scatterplot using ggplot2, we can use colour argument inside geom_point with aes. The color can be passed in multiple ways, one such way is to name the particular color and the other way is to giving a range or using a variable.


2 Answers

Have you looked into scale_colour_steps or scale_colour_stepsn?

Using the option n.break from scale_colour_stepsn you should be able to specify the number of breaks you want and have sharper transitions.

Be sure to use ggplot2 > 3.3.2

like image 180
Ryderc Avatar answered Nov 05 '22 03:11

Ryderc


In case you are still interested in a solution for this, you can add guide = guide_colourbar(nbin = <some arbitrarily large number>) to scale_fill_gradientn(). This increases the number of bins used by the colourbar legend, which makes the transition look sharper.

# illustration using nbin = 1000, & weighted colours below the cutoff
plot.cutoff <- function(cutoff){
  p <- ggplot(data = test,
              aes(x = as.factor(x), y = y, fill = log(y))) + 
    geom_col(width = 1) +
    scale_fill_gradientn(colours = c("red4", "red", "yellow", "green"), 
                         values = scales::rescale(log(c(0.01, cutoff - 0.0000000000000001, 
                                                        cutoff, 0.2))), 
                         breaks = c(log(cutoff)), 
                         label = c(cutoff),
                         guide = guide_colourbar(nbin = 1000))
  return(p)
}

cowplot::plot_grid(plot.cutoff(0.05),
                   plot.cutoff(0.06),
                   plot.cutoff(0.08),
                   plot.cutoff(0.1),
                   ncol = 2)

illustration

(If you find the above insufficiently sharp at very high resolutions, you can also set raster = FALSE in guide_colourbar(), which turns off interpolation & draws rectangles instead.)

like image 37
Z.Lin Avatar answered Nov 05 '22 03:11

Z.Lin