ggplot2 icon indicating copy to clipboard operation
ggplot2 copied to clipboard

Documentation feature request: Include examples in autoplot's documentation.

Open ReeceGoding opened this issue 2 years ago • 3 comments

autoplot looks like great fun to play around with, but I see two major issues:

  1. Little help for it seems to be easily available online
  2. The documentation contains no examples whatsoever

I believe that adding some examples to its documentation would greatly improve it. At present, the user is largely left to guess what sort of inputs it will accept.

ReeceGoding avatar Mar 27 '22 00:03 ReeceGoding

I think this is a good idea, but the problem is that ggplot2 doesn't include any of the actual method implementation. So, I'm afraid this is difficult to do within ggplot2's document.

https://github.com/tidyverse/ggplot2/blob/21df8dc88e9b736a136bc39362defe2943008767/NAMESPACE#L17

yutannihilation avatar Apr 23 '22 09:04 yutannihilation

Maybe we could write a vignette describing the main use cases.

Happy to send a PR if that's the case.

92amartins avatar Sep 25 '23 14:09 92amartins

I think a vignette might be overdoing it a bit, but it probably could get a separate docpage together with autolayer() and maybe fortify(). In the example section, you could simply implement dummy methods for a matrix or something, see below.

library(ggplot2)
#> Warning: package 'ggplot2' was built under R version 4.3.1

mtx <- matrix(1:12, 3, 4)

# Matrix as tidy data
fortify.matrix <- function(model, ...) {
  dims <- dim(model)
  data.frame(
    row   = as.vector(.row(dims)),
    col   = as.vector(.col(dims)),
    value = as.vector(model)
  )
}

ggplot(mtx, aes(col, row, fill = value)) +
  geom_raster()


# Converting matrices to a raster layer
autolayer.matrix <- function(object, ...) {
  # We can use `data = object` here because we've defined a `fortify()`
  # method above
  geom_raster(aes(x = col, y = row, fill = value), data = object, ...)
}

ggplot() + autolayer(mtx)

# Converting matrices to a plot
autoplot.matrix <- function(object, ...) {
  ggplot(...) +
    autolayer(object) +
    # Adding a scale for the matrix
    scale_fill_viridis_c()
}

autoplot(mtx)

Created on 2023-09-25 with reprex v2.0.2

teunbrand avatar Sep 25 '23 15:09 teunbrand