plotHelper: Plot colour helper

View source: R/plotHelper.R

plotHelperR Documentation

Plot colour helper

Description

Allows you to interactively pick combinations of colours, to help you choose colours to use in your plots. The plot updates in real-time as you pick colours.

If you often find yourself spending a lot of time re-creating the same plot over and over with different colours to try to find the best colours, then the Plot Colour Helper can help you immensely.

Important: The colours you pick will be available as a variable called CPCOLS, so you can use CPCOLS in your plot code. See the example below.

Usage

plotHelper(code = "", colours = NULL, returnCode = FALSE)

Arguments

code

Code for a plot. You can use the variable CPCOLS in this code to refer to the colours that you will pick. If you do not provide any code, the plot helper will initialize with sample code. The code can be provided as text or as R code.

colours

A vector of colours to use as the initial colours in the tool, or an integer. If an integer is provided instead of colours, the tool will load with that number of colours, and default colours will be used initially. If you do not provide this parameter, the tool will attempt to guess how many colours are needed in the code and initialize that many colours.

returnCode

If TRUE, return the plot code and the CPCOLS variable as text. If FALSE (default), return the vector of selected colours.

Details

There are many keyboard shortcuts to help you be more efficient. For example, pressing spacebar adds a new colour, left/right keys let you navigate between the selected colours, 1-9 let you select any of the first 9 colours. For a full list of keyboard shortcuts, click on Show keyboard shortcuts.

Value

When this function is called using plotHelper(), the chosen colours are returned as a vector of colours. When this is run as an RStudio addin (through the Addins menu), the resulting code that includes the colour vector gets inserted into the R document. As a side effect, CPCOLS gets assigned in the global environment to the value of the selected colours.

Examples

if (interactive()) {
  cols <- plotHelper()
  cols <- plotHelper(colours = c("red", "blue"))
  cols <- plotHelper(colours = 5)

  library(ggplot2)
  cols <- plotHelper(ggplot(mtcars, aes(mpg,wt)) +
                     geom_point(aes(col = as.factor(cyl)))+
                     scale_colour_manual(values = CPCOLS))
}

colourpicker documentation built on Aug. 21, 2023, 9:11 a.m.