CRAN_Status_Badge Build Status R build status lifecycle

1 Introduction

1.1 Motivation

Data visualization is essential to communicating scientific findings in biostatistics and data science. Plots with appropriate colors better represent the data and are easier to read.

However, selecting appropriate colors is not always an easy topic. Although many packages offer palettes and automatic color filling, users may want to have a way to preview the colors immediately, which is the exact issue that our package {seecolor} is able to solve. For instance,

## Preview colors using seecolor package
seecolor::print_color(c("cyan", "#0000FF", "yellow"), type = "r")
## 
##  ------ c cyan #0000FF yellow ------
## cyan                
## #0000FF             
## yellow              

1.2 Overview

In , colors may be specified either by

  • name (e.g. “blue”)
  • hexadecimal code (e.g. “#FF1234”),
  • a palette (e.g. offered in the viridis, RColorBrewer and ggsci packages)
  • or being contained in a plot object.

To this end, our seecolor package provides corresponding ways visualizing colors used in a literals, a palette, or a ggplot2 object in the console. Specifically,

  • users may use pick_color() to interactively preview the colors
  • and use print_color() to print the colors programmingly.

1.3 Installation

seecolor is now available on CRAN as version 0.1.0. You may want to access

  • the Package page
  • the CRAN page
  • the Github page .
  • the vignettes .

Code below can be used to install the package.

# install from CRAN
install.packages("seecolor")

# install from GitHub
devtools::install_github("lovestat/seecolor")

# load package
library(seecolor)

2 Methods

The package is designed to detect color literals and hexadecimal color codes, as well as colors used in a ggplot object. Under the hood, the package uses the feature of the ASCII colorful codes to make print colors in console possible. The origin functionality is implemented by the package {crayon}.

Since detecting strings are required, we utilized much on regular expression which is offered by package {stringr}.

The pick_color function invokes an interactive mode in RStudio. When the function is executed and running, use the cursor to highlight a text in an R or RMD file. If the highlighted text is a command to create a ggplot object, the color used in the object will be shown in the console in real time. If the highlighted text is a color name (e.g.: “blue”) or a hexadecimal code (e.g. : “#FF1234”), the corresponding color will be shown in the console. The function detects highlighted texts using the power of the RStudio API rstudioapi::getActiveDocumentContext.

The print_color function takes an input of characters or a ggplot object. If the input character includes color names or hexadecimal color codes, the function prints real colors bars corresponding to the color names or hexadecimal codes. If the input is a ggplot object, the function prints all the colors used in the plot.

The open_sample function opens an R file that includes sample codes. Readers are encouraged to try functions in this package.

The functions z_pick_color_addin_ribbon(), z_pick_color_addin_mosaic() are two auxiliary functions for RStudio Addins, which let user invode the functions by clicking the button instead of type codes.

3 Results

3.2 Interactive mode

The pick_color function creates an interactive mode in RStudio. The code below demonstrates the usage of this function.

# Step 1: run the function
pick_color()

# Step 2: select some texts with cursor
## ggplot object
p <- data.frame(x = rnorm(100), y = as.factor(rep(1:5, times = 20))) %>% 
  ggplot(aes(x = x, fill = y, group=y)) +
    geom_boxplot()
## color name
  "blue red yellow violet"
## hexadecimal code
  "#FF2481"
p <- data.frame(x = rnorm(100), y = as.factor(rep(1:5, times = 20))) %>% 
  ggplot(aes(x = x, fill = y, group=y)) +
    geom_boxplot()

print_color(p)
## 
##  ---ggplot object--- 
##                   

There is a gif to demonstrate the interactive usage.

3.3 Sample file

The package provides some sample colors for users to play with. If you want to try the pick_color() function, I strongly suggest you to use this sample file. - The file is created in the tmp path, which means it will not bother your regular files.

# Open the pre-defined test file
open_sample()

3.4 Addins

Like we mentioned earlier, seecolor has the RStudio addins to make invoke pick_color() more handy.

4 Conclusion

The package is created to facilitate color preview in R. It extracts colors used in a plot easily. The print_color and pick_color functions can detect colors in literals, palettes or ggplot objects and prints them in the console in two different ways, i.e. programmingly and interactively. Also, we offer a series of helpful functions to make this convienient.

4.1 Wishlist

  • Add the support for other plot objects
  • Integrate with highlight color with RStudio

4.2 Discussion

For further discussion, you may use GitHub issues to contact the author. And for the further contribution, please submit your suggestion by GitHub pull request.