teunbrand / ggh4x

ggplot extension: options for tailored facets, multiple colourscales and miscellaneous
https://teunbrand.github.io/ggh4x/
Other
563 stars 33 forks source link

Adding example for annotate with geom_pointpath #83

Closed brunomioto closed 1 year ago

brunomioto commented 1 year ago

Hi!

I created an example for answering one question on OverStackOverflow. Maybe it (or something like this) can be added as an example in documentation of geom_pointpath

https://stackoverflow.com/questions/35904363/offset-geom-segment-in-ggplot/74447630#74447630

teunbrand commented 1 year ago

Hi Bruno,

Thanks for the suggestion, an annotation example might be helpful. In your example, did annotate(geom = "pointpath", ...) not work (if ggh4x is loaded)? I generally prefer internal objects like the GeomPointPath class to remain 'invisible' for typical use, but accessible to extend (which is why it is exported).

brunomioto commented 1 year ago

Hello,

annotate(geom = "pointpath", ...) didn't work. I used ggh4x::GeomPointPath as showed on this example (the last one) of nflplotR by @mrcaseb

teunbrand commented 1 year ago

Thanks for the response; I'll try to figure out why the canonical thing won't work and add some example to the docs.

mrcaseb commented 1 year ago

Thanks for the response; I'll try to figure out why the canonical thing won't work and add some example to the docs.

The string "pointpath" doesn't work because of the class name you chose in the ggproto object here: https://github.com/teunbrand/ggh4x/blob/e240d75b0e2a7d934e83cb33dbfeb733c9078130/R/geom_pointpath.R#L64

ggplot2 tries to construct that class name internally with the following call:

paste0("Geom", ggplot2:::camelize("pointpath", first = TRUE))
#> [1] "GeomPointpath"

So if you rename your class to "GeomPointpath" it will work (I just confirmed this locally)

mrcaseb commented 1 year ago

This is just pseudo elegant in my opinion which is why I encourage using the exported object explicitly, i.e. writing annotate(ggh4x::GeomPointPath, ...)

teunbrand commented 1 year ago

Thanks for catching this mrcaseb, I think I'll re-assign the ggproto class to both GeomPointPath and GeomPointpath so that annotate() will work.

teunbrand commented 1 year ago

Should soon work properly

devtools::load_all("~/packages/ggh4x")
#> ℹ Loading ggh4x
#> Loading required package: ggplot2

ggplot() +
  annotate(
    "pointpath",
    x = pressure$temperature,
    y = pressure$pressure
  )

Created on 2023-03-28 with reprex v2.0.2