dataMaid icon indicating copy to clipboard operation
dataMaid copied to clipboard

Feature enhancement: allow changes to plots

Open Ales-G opened this issue 2 years ago • 2 comments

Hello, I love the function makeDataReport and I use it often in my work. One thing that I would really love is the possibility of making changes to the baseline ggplots. For instance, when I have a character vector I often end up with bar plots that are difficult to read, as in the example below.

Screenshot 2022-05-20 at 13 01 24

Having the possibility to do the histogram on the y axis (simply replace aes(x=var) with aes(y=var) in ggplot) would really improve a lot the quality of the reports.

Is this something you are planning to include?

thanks a lot

Ales-G avatar May 20 '22 12:05 Ales-G

Thank you for reaching out!

This can easily be done, and we have provided a guideline for how to add such custom extensions to the built-in dataReporter tools in this vignette: https://cran.r-project.org/web/packages/dataReporter/vignettes/extending_dataReporter.html

Here's a minimal example of how it may be achieved (following the structure of the prettierHist()-example from the vignette):

library(dataReporter)

#Define helper function that does actual plotting
flippedBarplotHelper <- function(v, vnam) {
  p <- ggplot(data.frame(v = v), aes(x = v)) + 
    geom_bar() +
    coord_flip() #flip axes

  p
}

#define visualFunction-style flippedBarplot() function:
flippedBarplot <- function(v, vnam, doEval = TRUE) {
  #define the call
  thisCall <- call("flippedBarplotHelper", v = v, vnam = vnam)
  
  #evaluate or deparse
  if (doEval) {
    return(eval(thisCall))
  } else return(deparse(thisCall))
}

#Make it a proper visualFunction:
flippedBarplot <- visualFunction(flippedBarplot, 
                               description = "ggplot2 barplot with flipped axes",
                               classes = c("character", "factor"))

#Make dataReport using this new visualFunction for factor variables:
makeDataReport(toyData, visuals = setVisuals(factor = "flippedBarplot"))

Note that the function will also be visible in the allVisualFunctions() overview.

We'll think about adding this to the package when we do the next big update. But until then, the solution above should work.

annennenne avatar Jun 07 '22 20:06 annennenne

Oh, and I forgot to mention that the package has been renamed to dataReporter (see https://github.com/ekstroem/dataReporter/). So the dataMaid package is no longer being maintained.

annennenne avatar Jun 07 '22 20:06 annennenne