Skip to content
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

label_ functions for degrees (temp/lat/long); superscript notation for axes #260

Closed
bbolker opened this issue Feb 22, 2020 · 5 comments
Closed
Labels
feature a feature request or enhancement

Comments

@bbolker
Copy link

bbolker commented Feb 22, 2020

Copied from here in case it's appropriate/welcome here. I'd be happy to make a PR if it's of interest.

Some miscellaneous functionality here for

  • scientific notation with superscripts (i.e. 10^3, 10^4, etc.)
  • latitude/longitude scales

These could be further improved by

  • allowing for a LaTeX (intended for tikzDevice, e.g. $10^2$) vs plotmath output (e.g. parse(text="10^2"));
  • reducing code duplication in lat/long, e.g. generating lat/long scales from a single factory function that determines whether to use E/W or N/S legends
  • could further improve lat/long by allowing it to (optionally) express as degrees/minutes/seconds rather than decimal degrees
  • make a temperature scale function

Does this functionality exist somewhere else in the gg-verse?

@dpseidel
Copy link
Collaborator

We have considered longitude and latitude labels in the past (#180) and previously supported a wrapper of number_format (now superseded by label_number) called degree_format (#178; now retired). In ggplot2, latitude and longitude scales are handled primarily by the sf package and the st_graticule function and so we haven't historically incorporated a standalone function here in scales. You can of course use the label_parse function or the label_number function to help format custom labels when working outside of a spatial object or with other plotting packages.

demo_continuous(c(49.67890), labels = label_number(suffix ="\u00b0"))
#> scale_x_continuous(labels = label_number(suffix = "°"))

graticule_labels <- c("180*degree", "160*degree*W", "140*degree*W")
demo_discrete(graticule_labels, labels = label_parse())
#> scale_x_discrete(labels = label_parse())

Scientific notation with superscripts is now supported by default with the label_math() function.

library(scales) #scales 1.1.0
demo_continuous(c(1, 2, 3, 4), labels = label_math())
#> scale_x_continuous(labels = label_math())

Created on 2020-02-22 by the reprex package (v0.3.0)

@bbolker
Copy link
Author

bbolker commented Feb 23, 2020

These are definitely good, it would be nice if they were more visible. A few minor points for discussion:

  • For log scales, I was looking for something like this:
lm <- function(x) label_math()(log10(x))
demo_log10(10^(2:5),label=lm)
  • My solution posted on the gist is a little bit fancier: the solution above doesn't work for other sets of breaks:
demo_log10(seq(100,1000,length=100)),label=lm)  ## ugly
## load scientific_10() from gist ...
demo_log10(seq(100,1000)) + scale_x_log10(label=scientific_10)  ## nicer
  • does the unicode-based solution above work on Windows? (I don't have a system to test on before, but students of mine have encountered apparently unsurmountable problems with some Unicode characters [up- and down-arrows] on Windows ...)
  • For my own use I would supplement or extend label_math with a version that would work with LaTeX formatting (for use with TikZ), but I recognize that may be a niche requirement ...

@hadley hadley added the feature a feature request or enhancement label Apr 3, 2020
@teunbrand
Copy link
Contributor

Small comment on the first point; the log scale you're proposing can be achieved by providing the log10() function as format argument to label_math(), i.e.:

demo_log10(10^(2:5), label = label_math(format = log10))

image

Though I agree a convenience wrapper would be nice.

@earthcli
Copy link

earthcli commented Mar 3, 2021

@dpseidel It is great. So how can I use this within scale_x_continuous(), for example, which is the right way to put the graticule_labels into scale_x_continuous ?
graticule_labels <- c("180degree", "160degreeW", "140degree*W")
scale_x_continuous(labels=label_parse())

@hadley
Copy link
Member

hadley commented Mar 17, 2022

Now a duplicate of #311, and will be resolved by #312.

@hadley hadley closed this as completed Mar 17, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature a feature request or enhancement
Projects
None yet
Development

No branches or pull requests

5 participants