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
In , colors may be specified either by
viridis
, RColorBrewer
and ggsci
packages)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,
pick_color()
to interactively preview the colorsprint_color()
to print the colors programmingly.seecolor
is now available on CRAN as version 0.1.0. You may want to access
Code below can be used to install the package.
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.
Our package is mainly optimzed for console output. Since the html supports minor colors used in R, you may see some consecutive colors which will be fine if viewed in the R console.
The print_color
function can visualize colors in the console. The type
argument can be used to specify type of printing. The default is mosaic
. The following code prints color names and hexadecimal color codes in texts.
# print color in the default moasic type
print_color(c("red", "navy", "pink", "#E58700", "limegreen"))
##
## ------ c red navy pink #E58700 limegreen ------
##
# print color in ribbon type
print_color(c("red", "navy", "pink", "#E58700", "limegreen"),
type = "r")
##
## ------ c red navy pink #E58700 limegreen ------
## red
## navy
## pink
## #E58700
## limegreen
The below code prints colors included in a palette. The blank.len
argument can specify the length of color mosaic.
##
## ------ palette rainbow(6) ------
##
##
## ------ palette rainbow(6) ------
## red
## yellow
## green
## cyan
## blue
## magenta
The code below prints colors used in a ggplot
object.
##
## ---ggplot object---
##
## ---ggplot object---
## #F8766D
## #E58700
## #C99800
## #A3A500
## #6BB100
## #00BA38
## #00BF7D
## #00C0AF
## #00BCD8
## #00B0F6
## #619CFF
## #B983FF
## #E76BF3
## #FD61D1
## #FF67A4
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.
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.
Like we mentioned earlier, seecolor has the RStudio addins to make invoke pick_color()
more handy.
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.
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.