Sunday, 8 January 2017

r - 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))

No comments:

Post a Comment

c++ - Does curly brackets matter for empty constructor?

Those brackets declare an empty, inline constructor. In that case, with them, the constructor does exist, it merely does nothing more than t...