Skip to content

Custom tick marks and borders in guide_colorbar #1530

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 4 commits into from

Conversation

jeffreyhanson
Copy link

Custom tick marks and borders in guide_colorbar

This pull request allows the user to manually change the appearance of tick marks and borders around colorbar legends. Following a discussion on stackoverflow, a feature request was raised, but there was not enough resources to implement it at the time.

The original problem is shown below. The use of white in a color ramp results in a legend which seems to disappear at the end. Additionally, the ticks marks are hard to see.

#  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") +
  theme_bw() +
  theme(line = element_line(colour = "#0000FF")) +
  guides(fill = guide_colourbar(barheight = unit(3, "in")))

The pull request updates that allow the user to specify element_line objects that dictate the appearance of the legend. By using features in the pull request, we can specify black tick marks and borders to fix this issue.

# Plot with better legend
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')))

This is my first "real" pull request on Github, so please let me know if I'm doing anything wrong :)


EDIT: fix typo

@jeffreyhanson jeffreyhanson changed the title Custom tick marks and borders in guide_color Custom tick marks and borders in guide_colorbar Feb 3, 2016
# create data frame for tick display
breaks <- scale$get_breaks()
if (length(breaks) == 0 || all(is.na(breaks)))
return()

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please remove these minor changes that aren't related to your new functionality? That makes it easier for me to review.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump

#' @param ticks A logical specifying if tick marks on colorbar should be
#' visible.
#' @param ticks A \code{\link{element_line}} object specifying the appearance
#' of the tick marks.' For backwards compatibility, a logical can also be
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra '

@jeffreyhanson
Copy link
Author

I've made the changes - let me know if there's anything else I can do.


...) {

if (!is.null(barwidth) && !is.unit(barwidth)) barwidth <- unit(barwidth, default.unit)
if (!is.null(barheight) && !is.unit(barheight)) barheight <- unit(barheight, default.unit)

# if logical argument supplied to tick then use defaults
if (identical(ticks, TRUE))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're currently specifying the default in two places - here and above in the parameter list. I think it would be better to specify only in one place.

I'd recommend restructuring this if block to cover all cases:

if (identical(ticks, TRUE)) { 

} else if (identical(ticks, FALSE)) {

} else if (inherits(ticks, "element")) {

} else {
  stop("`ticks` should be ...", call. = FALSE)
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, thinking about it more, shouldn't the default come from the theme system?

@hadley hadley added feature a feature request or enhancement in progress labels Jul 29, 2016
@hadley
Copy link
Member

hadley commented Sep 23, 2016

Closing due to apparent lack of interest

@hadley hadley closed this Sep 23, 2016
@DanOvando
Copy link

I'd like to add my vote for this feature if interest arises again, I've been trying to do this for quite a long time. Let me know if I can be of help getting this in place

@PeteCowman
Copy link

I would like to see this feature also!

@ko-ichi-h
Copy link

Me too. I really hope we can use this feature!

@komodovaran
Copy link

Would love to have it re-opened. ggplot's colorbar is doesn't seem quite right compared to colorbars from Matlab or Python.

@jdeut
Copy link

jdeut commented Sep 13, 2017

I also opt for this feature! White on white sucks..

@srgorelik
Copy link

I would really like this feature as well. It would be very useful to be able to change the color of the colorbar border and tick marks.

@johnbaums
Copy link

+1 - This is the one thing that stops me from using ggplot for spatial viz.

@josetanago
Copy link

+1, same problem, white ticks in colorbars with light colors are not visible!

@batpigandme
Copy link
Contributor

HI @DanOvando @josetanago @johnbaums @srgorelik @nichtleiter @komodovaran @ko-ichi-h @PeteCowman ,

The best way to add your +1 is by adding it (the +1 reaction, 👍) to the original issue. It's easiest for us to keep track of!

Thanks

@clauswilke
Copy link
Member

This effect can now be achieved, using a slightly different syntax: #2541
I only found this abandoned pull request now, that mine has been merged. Arguably, the API of this one was a bit cleaner.

@johnbaums
Copy link

Hallelujah! This was a long time coming. Thanks to all that drove this.

@jeffreyhanson
Copy link
Author

Awesome! Thank you so much for implementing this functionality @clauswilke!

@lock
Copy link

lock bot commented Oct 30, 2018

This old issue has been automatically locked. If you believe you have found a related problem, please file a new issue (with reprex) and link to this issue. https://reprex.tidyverse.org/

@lock lock bot locked and limited conversation to collaborators Oct 30, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
feature a feature request or enhancement
Projects
None yet
Development

Successfully merging this pull request may close these issues.