-
Notifications
You must be signed in to change notification settings - Fork 17
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
add vertical or horizontal lines to the side plot #48
Comments
I must have missed these functions in the package. I will see about adding them in the next update when I have more time. For now, please see the reprex on how to create a geom that may be compatible with library(ggside)
#> Loading required package: ggplot2
#> Registered S3 method overwritten by 'ggside':
#> method from
#> +.gg ggplot2
get_formals <- function(fn) {
frml <- ggplot2:::ggproto_formals(fn)
fnames <- names(frml)
for (i in seq_along(fnames)) {
frml[[i]] <- as.name(fnames[i])
}
frml
}
build_ggside_geom <- function(geom, class = NULL, side = c("x","y")) {
force(geom)
use_side_aes <- switch(side, x = ggside::use_xside_aes, y = ggside::use_yside_aes)
new_aes <- ggside:::new_default_aes(aes(xcolour = NA, xfill = NA),
geom$default_aes)
fn_setup_data <- rlang::new_function(
ggplot2:::ggproto_formals(geom$setup_data),
body = expr({
data <- ggside::parse_side_aes(data, params)
geom$setup_data(!!!get_formals(geom$setup_data))
})
)
fn_draw_panel <- rlang::new_function(
ggplot2:::ggproto_formals(geom$draw_panel),
body = expr({
data <- use_side_aes(data)
geom$draw_panel(!!!get_formals(geom$draw_panel))
})
)
fn_draw_key = rlang::new_function(
ggplot2:::ggproto_formals(geom$draw_key),
body = expr({
data <- use_side_aes(data)
geom$draw_key(!!!get_formals(geom$draw_key))
})
)
ggplot2::ggproto(
class,
geom,
default_aes = new_aes,
setup_data = fn_setup_data,
draw_panel = fn_draw_panel,
draw_key = fn_draw_key
)
}
as_ggside_layer <- function(layer, side = c("x", "y")) {
side <- match.arg(side, c("x", "y"))
LayerClass <- switch(side, x = ggside:::XLayer, y = ggside:::YLayer)
geom <- layer$geom
geom <- build_ggside_geom(geom, side = side)
names <- ls(layer)
args <- lapply(names, function(n, x) x[[n]], x = layer)
names(args) <- names
args$geom <- geom
do.call(ggproto, c(list(`_class` = "ggside_layer", `_inherit` = LayerClass),
args))
}
#fails because there is no data passed to the layer
# for it to train the scale on.
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
as_ggside_layer(
geom_vline(xintercept = 3),
side = "x"
)
#> Error in `scale_apply()`:
#> ! `scale_id` must not contain any "NA"
#> Backtrace:
#> ▆
#> 1. ├─base::tryCatch(...)
#> 2. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 3. │ ├─base (local) tryCatchOne(...)
#> 4. │ │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 5. │ └─base (local) tryCatchList(expr, names[-nh], parentenv, handlers[-nh])
#> 6. │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 7. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 8. ├─base::withCallingHandlers(...)
#> 9. ├─base::saveRDS(...)
#> 10. ├─base::do.call(...)
#> 11. ├─base (local) `<fn>`(...)
#> 12. └─global `<fn>`(input = base::quote("flat-ram_reprex.R"))
#> 13. └─rmarkdown::render(input, quiet = TRUE, envir = globalenv(), encoding = "UTF-8")
#> 14. └─knitr::knit(knit_input, knit_output, envir = envir, quiet = quiet)
#> 15. └─knitr:::process_file(text, output)
#> 16. ├─base::withCallingHandlers(...)
#> 17. ├─base::withCallingHandlers(...)
#> 18. ├─knitr:::process_group(group)
#> 19. └─knitr:::process_group.block(group)
#> 20. └─knitr:::call_block(x)
#> 21. └─knitr:::block_exec(params)
#> 22. └─knitr:::eng_r(options)
#> 23. ├─knitr:::in_input_dir(...)
#> 24. │ └─knitr:::in_dir(input_dir(), expr)
#> 25. └─knitr (local) evaluate(...)
#> 26. └─evaluate::evaluate(...)
#> 27. └─evaluate:::evaluate_call(...)
#> 28. ├─evaluate (local) handle(...)
#> 29. │ └─base::try(f, silent = TRUE)
#> 30. │ └─base::tryCatch(...)
#> 31. │ └─base (local) tryCatchList(expr, classes, parentenv, handlers)
#> 32. │ └─base (local) tryCatchOne(expr, names, parentenv, handlers[[1L]])
#> 33. │ └─base (local) doTryCatch(return(expr), name, parentenv, handler)
#> 34. ├─base::withCallingHandlers(...)
#> 35. ├─base::withVisible(value_fun(ev$value, ev$visible))
#> 36. └─knitr (local) value_fun(ev$value, ev$visible)
#> 37. └─knitr (local) fun(x, options = options)
#> 38. ├─base::withVisible(knit_print(x, ...))
#> 39. ├─knitr::knit_print(x, ...)
#> 40. └─knitr:::knit_print.default(x, ...)
#> 41. └─evaluate (local) normal_print(x)
#> 42. ├─base::print(x)
#> 43. └─ggplot2:::print.ggplot(x)
#> 44. ├─ggplot2::ggplot_build(x)
#> 45. └─ggplot2:::ggplot_build.ggplot(x)
#> 46. └─layout$train_position(data, scale_x(), scale_y())
#> 47. └─ggplot2 (local) train_position(..., self = self)
#> 48. └─self$facet$train_scales(...)
#> 49. └─ggplot2 (local) train_scales(...)
#> 50. └─ggplot2:::scale_apply(layer_data, x_vars, "train", SCALE_X, x_scales)
#> 51. └─cli::cli_abort("{.arg scale_id} must not contain any {.val NA}")
#> 52. └─rlang::abort(...)
ggplot(iris, aes(Sepal.Width, Sepal.Length)) +
geom_point() +
geom_xsidehistogram(aes(fill = Species)) +
as_ggside_layer(
geom_vline(xintercept = 3),
side = "x"
)
#> `stat_bin()` using `bins = 30`. Pick better value with `binwidth`. Created on 2023-09-21 with reprex v2.0.2 |
It is working great. Thank you very much! |
Hello, I have tried reproducing the plot after updating the package to v0.3.1. There seems to be some problem.
there seems to be a
On the older version 0.2.2, the workaround suggested still works. |
Hello,
is there a way to add
geom_vline
orgeom_hline
to the side plot?The text was updated successfully, but these errors were encountered: