Skip to content

Instantly share code, notes, and snippets.

@dsparks
Created September 13, 2012 21:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save dsparks/3717985 to your computer and use it in GitHub Desktop.
Save dsparks/3717985 to your computer and use it in GitHub Desktop.
Transforming a color scale
# Transforming a color scale
doInstall <- TRUE # Change to FALSE if you don't want packages installed.
toInstall <- c("ggplot2", "RColorBrewer", "scales")
if(doInstall){install.packages(toInstall, repos = "http://cran.us.r-project.org")}
lapply(toInstall, library, character.only = TRUE)
# Generate some data
nn <- 10000
myData <- data.frame(X = rnorm(nn),
Y = rnorm(nn))
myData$Z <- with(myData, X * Y)
# Make a standard scatterplot with Z encoded into point colour
zp1 <- ggplot(myData,
aes(x = X, y = Y, colour = Z))
zp1 <- zp1 + geom_point()
zp1 <- zp1 + theme_bw()
zp1 <- zp1 + coord_equal()
zp1 <- zp1 + scale_colour_gradientn(colours = colorRampPalette(rev(brewer.pal(11, "Spectral")))(1000),
breaks = c(-2:2 * 2))
zp1 <- zp1 + ggtitle("Untransformed")
print(zp1)
# Define a transformation
norm_trans <- function(){
trans_new('norm', function(x) pnorm(x), function(x) qnorm(x))
}
# Apply transformed colour scale
zp2 <- zp1 + scale_colour_gradientn(colours = colorRampPalette(rev(brewer.pal(11, "Spectral")))(1000),
trans = 'norm', # Apply the transformation here
breaks = c(-2:2)) # Note the change in breaks
zp2 <- zp2 + ggtitle("Transformed")
print(zp2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment