knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>",
  fig.path = "man/figures/README-",
  out.width = "100%"
)
# Copy this into YAML header in order to update github pages
# knit: (function(input_file, encoding) {
#   out_dir <- 'docs';
#   rmarkdown::render(input_file,
#  encoding=encoding,
#  output_file=file.path(dirname(input_file), out_dir, 'index.md'))})

bsscol ReadMe

bsscol allows for a simple integration of the BSS base colors into ggplot2 and alike.

installation and loading

You can install the released version of bsscol from Github with:

library(devtools) # install devtool first if not installed
install_github("qwertzlbry/bsscol")

Or just copy the package folder from: "G:/BSS/Administration/05_BSS_Vorlagen/Farben_R_Stata/bsscol" into your local R library and load it.

With a future CRAN approval the package could be installed with:

install.packages("bsscol") # currently the installation works only over github

In order to run the following examples you'll also need:

install.packages("ggplot2")
install.packages("plotrix")
install.packages("dplyr")
install.packages("hues")
install.packages("ggthemes")
install.packages("DT")
install.packages("kableExtra")
library(bsscol)
library(ggplot2)
library(plotrix)
library(hues)
library(ggthemes)
library(dplyr, warn.conflicts=F, quietly=T)
library(kableExtra, warn.conflicts=F, quietly=T)

the colors

all colors

The following table contains all the BSS colors. When the package is loaded this table can be called with the bss_colors command.

#the colors, RGB and hex codes
bss_colors <- bss_colors %>% dplyr::arrange(col_pal) %>% dplyr::select(-colour)
bss_dt <- bss_colors
bss_dt$hashed_hex = cell_spec(
  bss_dt$hashed_hex, color = "white", align = "c", #angle = 2,
  background = factor(bss_dt$excel_form,bss_dt$excel_form, bss_dt$hashed_hex))

    kbl(bss_dt, escape = F, format = "html") %>%
      kable_paper("striped", full_width = F)   
          # save_kable("inst/color_table.png")

Filtering the table by e.g. palettes (col_pal)

choice <- bss_colors %>% filter(col_pal == 7)
swatch(choice$hashed_hex)

basic colors

The following diagram contains all basic bss colors. They are stored in a vector with the corresponding names. Unlike to the bss_colors table, these colors can therefore be called in functions by their names. When the package is loaded the vector can be called with the basic_colors command. However, the basic bss colors can also be found in the bigger above shown bss_colors table, as it includes all colors.

swatch(basic_colors)
basic_colors

function: bss_cols() - examples

The bss_cols function allows you to get and reference hex colors in a robust and flexible way for the basic_colors. As you can see in the example below, I am calling the colors by their names. Hence, this function does not work with the hex codes of the big, above showed bss_colors table.

# to get the basic colors
bss_cols()
# to get the information of a color
bss_cols("rot")
# or to just use a color in a plot
ggplot(mtcars, aes(hp, mpg)) +
  geom_point(color = bss_cols("rot"),
             size = 2, alpha = .8)+ 
  theme_hc()

function: bss_pal() - examples

With the command bss_palettes you can see all the coded palettes stored in a list.

# the following subset color palettes are available 
bss_palettes
#e.g.
swatch(bss_palettes$red_green)

The function bss_pal() allows to interpolate the palette colors for a certain number of levels, making it possible to create shades between the original colors using colorRampPalette from grDevices. This command can be used to extend to numbers of colors in a palette, however the hex codes of some color shades can then deviate from the above bss_colors table. Therefore, I recommend to use the coded palettes mentioned above, if possible.

# interpolate the "main_only_color" palette (which only includes five colors) to a length of 20:
bss_pal("main_only_color")(20)

The function bss_pal gets a palette by name from the list above ("main_only_color" by default) and has a boolean condition (rev =T/F) determining whether to reverse the order of colors or not.

# Example of pie chart extending monochrome_yellow palettes to 50 shades.
pie3D(rep(5, 50),explode=0, theta=1.2, col=bss_pal(rev = T,"monochrome_yellow")(50),main="bss_pal('monochrome_yellow')(50)")

function: scale_color_bss() - example

scale_color_bss() is a custom color scale function for ggplot2 plots. The same could also be achieved with the function scale_colour_manual of the ggplot2 package. However, the usage of the bsscol palettes makes this process more tidy and efficient. In the function two boolean parameters discrete = T/F, reverse = T/F can be choosen.

# Color by discrete variable using default palette main_only_color
ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Species)) +
  geom_point(size = 2) +
  scale_color_bss() +
  theme_hc()

# Color by numeric variable with monochrome_red palette
ggplot(iris, aes(Sepal.Width, Sepal.Length, color = Sepal.Length)) +
  geom_point(size = 2, alpha = .6) +
  scale_color_bss(discrete = FALSE, palette = "monochrome_red")+
  theme_hc()

function: scale_fill_bss() - example

scale_fill_bss() is a custom fill scale function for ggplot2 plots. The same could also be achieved with the function scale_fill_manual of the ggplot2 package. However, the usage of the bsscol palettes makes this process more tidy and efficient. The shades of colors are automatically extended when using more colors than a standard palettes has to offer. In the function two boolean parameters discrete = T/F, reverse = T/F can be chosen.

# Fill by discrete variable with the main palette + remove legend (guide)
ggplot(mpg, aes(model          , fill = model         )) +
  geom_bar() +
  theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
  scale_fill_bss(palette = "main", guide = "none")+
  theme_hc()


qwertzlbry/bsscol documentation built on July 28, 2021, 12:51 a.m.