Ben
Ben

Reputation: 103

How to create colorbars in ggplot similar to those created by Lattice

I want colourbars created with ggplot to be similar to what spplot function (from lattice package) creates. Something like the attached image with each finite number of colours being assigned to rectangular blocks, instead of creating a continuous spectrum of colours. I need to be able to define the outline colour of the colourbar and also the format of the ticks.

An example of what I wish to have

I put this simple example together. How can I change this into something similar to this attached image? For example, I want the legend to start from -3 and end at 3 with 10 blocks of colours. I already tried 'nbin' in the function 'guides'. But I need the labels to be put at the 'edges' of the colour blocks instead of at the middle of them (i.e. centre of the bins).

ps: And sometimes ggplot creates a labels beyond the length of the colourbar!

library(ggplot2)

dat <- data.frame(x = rnorm(100), y = rnorm(100), col=rnorm(100))

ggplot(dat, aes(x,y,color=col)) + 
  geom_point() + 
  scale_color_gradient2(limits=c(-3,3), midpoint=0) +             
  guides(color=guide_colourbar(nbin=10, raster=FALSE))

Upvotes: 3

Views: 5492

Answers (1)

Roman
Roman

Reputation: 76

I think what you ask for is not possible using the latest (public) version of ggplot2.

Ugly method, do at your own discretion

However, if you install the development version (this led to some version conflicts with other packages on my machine and I guess some things are not fully working yet) using

devtools::install_github("tidyverse/ggplot2")
library(ggplot2)

You will get some more options to modify guides such as ticks.colour, frame.colour or frame.linewidth which lets you customize the colorbar according to your requirements:

set.seed(6)
dat <- data.frame(x = rnorm(100), y = rnorm(100), z=rnorm(100))
ggplot(dat, aes(x,y,color=z)) + geom_point() +
  scale_color_gradientn(colours=c("blue","gray80","red"), limits=c(-3,3),
    breaks=c(-3/9*8,-3/9*4,0,3/9*4,3/9*8), labels=c(-2.4,-1.2,0,1.2,2.4), na.value = "green",
    guide=guide_colorbar(nbin=10, raster=F, barwidth=20, frame.colour=c("black"),
                         frame.linewidth=1, ticks.colour="black",  direction="horizontal")) +
  theme(legend.position = "bottom") 
  • Use colours = c() to specify a vector of colors
  • Use breaks together with labels to manually assign labels at the correct positions along the colorbar. EDIT: We can easily compute the required position along the colorbar by dividing 3 (the length of one half along the colorbar) by 9 (there are 9 half-boxes from the middle of the bar to the centre of the first box) and multiplying that by the number of half-boxes where we want the label to appear.
  • Values outside of limits will be colored according to na.value
  • You could additionally specify name = "Your Variable Name" to replace the z next to the colorbar

Colorbar with values between colors and a black contour around bar

I see no way to put -3 / 3 at the very ends of the color bar, other than manually placing a text element at the correct position in the plot (which I would strongly advice against).

Upvotes: 3

Related Questions