huizezhang-sherry / cubble

A tidy structure for spatio-temporal vector data
https://huizezhang-sherry.github.io/cubble/
Other
55 stars 9 forks source link

Need range01 function #8

Closed dicook closed 1 year ago

dicook commented 1 year ago

In converting from

temp.gly <- glyphs(nasa, "long", "day", "lat",
                   "ozone", height=2.5, 
                   y_scale = range01, polar = TRUE)
ggplot(temp.gly, ggplot2::aes(gx, gy, group = gid)) +
  geom_polygon(data=sth_america, 
            aes(x=long, y=lat, group=group), 
            fill="grey70") +
  add_ref_lines(temp.gly, color = "grey90") +
  add_ref_boxes(temp.gly, color = "grey90") +
  geom_path() +
  labs(x = "", y = "") +
  coord_map() +
  theme_map()  

to

ggplot() +
  geom_polygon(data=sth_america, 
           aes(x=long, y=lat, group=group), 
          fill="#014221", alpha=0.5, colour="#ffffff") +
  geom_glyph_box(data=nasa, aes(x_major = long, x_minor = day,
            y_major = lat, y_minor = ozone), fill=NA) +
  geom_glyph_line(data=nasa, aes(x_major = long, x_minor = day,
            y_major = lat, y_minor = ozone)) +
  geom_glyph(data=nasa, aes(x_major = long, x_minor = day,
            y_major = lat, y_minor = ozone), 
            y_scale = range01, polar = TRUE) +
  theme_map()

will throw an error. cubble is missing the range01 function, that will scale each spatial location data to range between 0 and 1, necessary to make polar glyphs with local scale.

dicook commented 1 year ago

Oh, it is because there is a computing problem, not that it is missing:

Error in `dplyr::mutate()`:
! Problem while computing `y_minor =
  rlang::exec(unique(params$y_scale), .data$y_minor)`.
ℹ The error occurred in group 1: group = -113.8.-21.2.
Caused by error in `unique.default()`:
! unique() applies only to vectors
Run `rlang::last_error()` to see where the error occurred.
huizezhang-sherry commented 1 year ago

x/y_scale needs to be specified as a string rather than a function. Would be worthwhile to document it in the @param:

library(cubble)
library(ggplot2)
range01 <- GGally::range01
#> Registered S3 method overwritten by 'GGally':
#>   method from   
#>   +.gg   ggplot2
GGally::nasa %>%
  ggplot(aes(x_major = long, x_minor = day,
             y_major = lat, y_minor = ozone)) +
  geom_glyph_box(fill=NA) +
  geom_glyph_line() +
  geom_glyph(y_scale = "range01", polar = TRUE) +
  ggthemes::theme_map()

Created on 2022-09-23 by the reprex package (v2.0.1)

schloerke commented 1 year ago

This needs to be a change within {cubble} to support functions directly. Strings should not be documented that they are supported.

Broken example

local({
  library(GGally)
  library(cubble)
  my_custom_function <- GGally::range01
  p <- ggplot(data = GGally::nasa,
        aes(x_major = long, x_minor = day,
            y_major = lat, y_minor = surftemp)) +
    geom_glyph_box() +
    geom_glyph_line() +
    geom_glyph(y_scale = "my_custom_function") +
    theme_bw()
  print(p)
})
#> Error in `dplyr::mutate()`:
#> ℹ In argument: `y_minor = rlang::exec(unique(params$y_scale), .data$y_minor)`.
#> ℹ In group 1: `group = -113.8.-21.2`.
#> Caused by error in `my_custom_function()`:
#> ! could not find function "my_custom_function"
#> Run `rlang::last_error()` to see where the error occurred.

Fixing this would also allow removal of all quosures from https://github.com/ggobi/ggally/pull/433


I'll have a PR and fixes to https://github.com/ggobi/ggally/pull/433 shortly