Capital of Statistics » R Language

  1. yihui
    Key Master

    Sometimes we need to fill in our graphical elements with gradient colors according to a certain variable (say, z). A special case is, z consists of a majority of small values and a few large ones. If we use the linear gradient (default in scale_fill_gradient() or scale_colour_gradient(), i.e. trans = "identity"), we will not be able to tell the differences among colors for points with many small z values. E.g.

    ## make up some data
    x = c(rnorm(100), 5:10)
    ## z contains 6 large values
    z = c(runif(100, 0, 1.5), runif(6, 10, 15))
    
    ## gradient from red to green
    qplot(x, z, colour = z) + scale_colour_gradient(low = "red", high = "green")
    ## cannot tell the difference even if using scale_colour_gradient2()
    qplot(x, z, colour = z) + scale_colour_gradient2(low = "red",
        mid = "yellow", high = "green", midpoint = 1.5)

    However, we can log-transform z to enlarge the difference among small values and make extremely large values much smaller. In this case, we can observe the gradient colors now:

    qplot(x, z, colour = z) + scale_colour_gradient2(low = "red",
        mid = "yellow", high = "green", midpoint = log(1.5), trans = "log")
    ## or equivalently
    qplot(x, z, colour = log(z)) + scale_colour_gradient2(low = "red",
        mid = "yellow", high = "green", midpoint = log(1.5)) 

    Essentially we are using a nonlinear color interpolation (linear to log(z), nonlinear to z).

    Posted 9 months ago #
  2. digitalcat
    Member

    Good tip. I had a similar solution once but instead of tinkering with the color gradient I just log transformed my data.

    Posted 2 weeks ago #

RSS feed for this topic

Reply

You must log in to post.