10

In some of my plots I find it hard to see the tick marks in the colour bar. I haven't been able to find a documented way to change the colour of the ticks. All the examples seem to focus on changing the labels or not drawing ticks at all. Is it possible?

#  Data
require(ggplot2)
require(grid)
n <- 100
x <- y <- seq(-4*pi, 4*pi, len=n)
r <- cos( sqrt( outer(x^2, y^2, "+") ) ^ 2 )
df <- data.frame( x = rep( x , each = n) , y = rep( y , times = n ) , val = c(r) )

#  Plot
ggplot( df , aes( x , y , fill = val ) )+
  geom_raster()+
  scale_fill_gradient( low = "#FFFFFF" , high = "#de2d26" )+
  guides( fill = guide_colourbar( barheight = unit( 3 , "in" ) ) )+
  theme_bw()+
  theme( line = element_line( colour = "#0000FF" ) )

enter image description here

How can I make the ticks in the colourbar be plotted in black rather than white, without changing other elements of the plot?


p.s. kudos to this question for the function to create the example data

Community
  • 1
  • 1
Simon O'Hanlon
  • 58,647
  • 14
  • 142
  • 184
  • 1
    it's [hardcoded to white](https://github.com/hadley/ggplot2/blob/master/R/guide-colorbar.r#L336), unfortunately. You could send a feature request, it sounds like a reasonable option to add in `guide_colourbar`. – baptiste Jan 13 '14 at 17:21
  • @baptiste [I have posted](https://groups.google.com/forum/#!topic/ggplot2/nbs0aC3sB8A) to the ggplot2 mailing group, with such a post! Thanks. – Simon O'Hanlon Jan 13 '14 at 17:22
  • not sure posting there will get developers' attention, better way is a new issue on github. – baptiste Jan 13 '14 at 17:24
  • @baptiste Ok, [**FR: 896**](https://github.com/hadley/ggplot2/issues/896) has been raised. Hopefully this is something the devs will consider. Thanks! – Simon O'Hanlon Jan 13 '14 at 21:51

3 Answers3

10

I usually find what I need to change by extensive use of str. I'm sure others can do it more elegantly.

g <- ggplotGrob(p)
g$grobs[[8]][[1]][[1]]$grobs[[5]]$gp$col <- "black"

library(grid)
grid.draw(g)

enter image description here

baptiste
  • 75,767
  • 19
  • 198
  • 294
Roland
  • 127,288
  • 10
  • 191
  • 288
10

EDIT: Please refer to the answer below by Claus Wilke.


I have included the original answer below, but please note that it is now outdated and I do not recommend using it.

I included the functionality to customize tick marks and legend borders in my fork of ggplot2. I have submitted a pull request, but thought I would let people know here in case they stumble on this question.

Install my fork using the following code,

if (!require(devtools))
  install.packages('devtools')
install_github('paleo13/ggplot2')

We can specify black marks using the following code,

ggplot(df, aes( x, y, fill = val)) +
  geom_raster() +
  scale_fill_gradient(low = "#FFFFFF", high = "#de2d26") +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF")) +
  guides(fill = guide_colourbar(barheight = unit(3, "in"),
   ticks=element_line(color='black'), border=element_line(color='black')))

black ticks and color ramp border

(I would have included this as a comment but I lack the reputation to do so, if anyone with sufficient privileges wants to delete this answer and move the contents to the comments then feel free)

paleo13
  • 1,349
  • 2
  • 11
  • 9
  • This is perfectly suitable as an answer. I'm looking forward to seeing this (hopefully) incorporated into ggplot. Will external ticks be possible? – jbaums Feb 27 '16 at 13:44
7

The pull request mentioned in another answer never made it into the ggplot2 code base, but this is now possible in a slightly different way in the development version (slated to be released as ggplot2 2.3):

ggplot(df, aes(x, y, fill = val)) +
  geom_raster() +
  scale_fill_gradient(low = "#FFFFFF", high = "#de2d26") +
  guides(fill = guide_colourbar(barheight = unit( 3 , "in" ),
                                ticks.colour = "black",
                                ticks.linewidth = 1)) +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF"))

enter image description here

You can also add a frame, which may be useful when some of the colors in the colorbar are very light.

ggplot(df, aes(x, y, fill = val)) +
  geom_raster() +
  scale_fill_gradient(low = "#FFFFFF", high = "#de2d26") +
  guides(fill = guide_colourbar(barheight = unit( 3 , "in" ),
                                ticks.colour = "black",
                                ticks.linewidth = 1,
                                frame.colour = "black",
                                frame.linewidth = 1)) +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF"))

enter image description here

Claus Wilke
  • 16,992
  • 7
  • 53
  • 104