Skip to content

Commit

Permalink
Merge pull request #6 from StatistikStadtZuerich/feature/palettes
Browse files Browse the repository at this point in the history
Feature/palettes
  • Loading branch information
alfell authored Dec 21, 2022
2 parents 160438c + 9bf64e2 commit 901589a
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 103 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: zuericolors
Title: Statistik Stadt Zuerich Colors (zuericolors)
Version: 0.1.0
Version: 0.2.0
Author: Statistik Stadt Zuerich
Maintainer: Alessandro Feller <[email protected]>
Description: zuericolors contains all colors needed by Statistik Stadt Zürich (SSZ).
Expand Down
28 changes: 16 additions & 12 deletions R/get_zuericolors.R
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,16 @@
#'
#' @return the color(s) as hex strings
#'
#' @examples
#' # Get colors of palette 'harmonic6'
#' get_zuericolors('harmonic6')
#' @examples
#' # Get colors of palette 'qual12'
#' get_zuericolors("qual12")
#'
#' # Get third color value of palette 'harmonic6'
#' get_zuericolors('harmonic6', 3)
#' # Get third color value of palette 'qual12'
#' get_zuericolors("qual12", 3)
#'
#' # Get first three color values of palette 'qual12'
#' get_zuericolors("qual12", 1:3)
#'
#' # Get first three color values of palette 'harmonic6'
#' get_zuericolors('harmonic6', 1:3)
#'
#' @export
get_zuericolors <- function(palette, nth) {

Expand All @@ -36,8 +36,10 @@ get_zuericolors <- function(palette, nth) {

# Return error message if palette argument does not match one of the palettes
if (!(chosen_palette %in% palette_names)) {
warning <- paste0("The palette ", "\"", chosen_palette, "\"",
" does not exist. Have you checked for Typos? Please choose one of the palettes above.")
warning <- paste0(
"The palette ", "\"", chosen_palette, "\"",
" does not exist. Have you checked for Typos? Please choose one of the palettes above."
)
stop(warning, cat(palette_names, sep = "\n"))
}

Expand All @@ -57,8 +59,10 @@ get_zuericolors <- function(palette, nth) {
# Return Error message if position (nth argument) is > than length of chosen
# palette (argument) Else return colors of chosen palette
if (palette_last < n_check) {
warning <- paste0("The palette ", "\"", chosen_palette, "\"", " contains ",
palette_last, " colors. Please change color position parameter (nth).")
warning <- paste0(
"The palette ", "\"", chosen_palette, "\"", " contains ",
palette_last, " colors. Please change color position parameter (nth)."
)
stop(warning)
}

Expand Down
48 changes: 13 additions & 35 deletions R/palettes.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,17 @@
#'
#' @keywords internal
palettes <- list(
gray10 = c("#2a2a2a", "#404040", "#606060", "#7c7c7c", "#989898", "#aaaaaa",
"#c2c2c2", "#d2d2d2", "#e8e8e8", "#f4f4f4"),
gray10cool = c("#001215", "#082429", "#223a41", "#3d575e", "#5c7781",
"#79949f", "#8dafbc", "#b2ccd7", "#dfe9ef", "#f4f7f9"),
gray10warm = c("#131111", "#252323", "#3b3737", "#545050", "#726f6e",
"#8f8b8b", "#aca8a8", "#c9c5c4", "#e4e0df", "#f6f3f2"),
harmonic6 = c("#0f05a0", "#2ac7c7", "#0a8df6", "#a2e5b0", "#3d575e",
"#23c3f1"),
harmonic12 = c("#0f05a0", "#94b7f8", "#009f9d", "#2ac7c7", "#0a8df6",
"#88cfff", "#009755", "#a2e5b0", "#3d575e", "#8dafbc",
"#0076aa", "#23c3f1"),
harmonic6hell = c("#94b7f8", "#7ee7e3", "#88cfff", "#a2e5b0", "#b2ccd7",
"#bbeefe"),
contrasting12 = c("#0f05a0", "#65cd8c", "#960055", "#0098c6", "#fbcb97",
"#53831b", "#fb737e", "#351459", "#2ac7c7", "#b9d55c",
"#46b2ff", "#5c7781"),
contrasting12hell = c("#94b7f8", "#a2e5b0", "#f18785", "#7bdcf9", "#fbcb97",
"#b9d55c", "#f99ec8", "#bc92ff", "#8ecf69", "#b5f4f4",
"#8dafbc", "#c5e7ff"),
sequential9blau = c("#030033", "#080490", "#1e1fc1", "#3e46dd", "#606dee",
"#8391f7", "#a6b2fc", "#c8d0fe", "#e9ecff"),
sequential5blau = c("#080490", "#3e46dd", "#8391f7", "#c8d0fe", "#e9ecff"),
sequential9rot = c("#1a0207", "#450612", "#720b1f", "#9d142e", "#c22340",
"#df3b57", "#f26178", "#fc99a8", "#ffe0e4"),
sequential5rot = c("#720b1f", "#9d142e", "#df3b57", "#fc99a8", "#ffe0e4"),
sequential9ocker = c("#1a1000", "#452b00", "#724600", "#9d6100", "#c27800",
"#df9114", "#f2ac3c", "#fccb7b", "#ffebcc"),
sequential5ocker = c("#724600", "#9d6100", "#df9114", "#fccb7b", "#ffebcc"),
sequential9petrol = c("#001414", "#00312f", "#004d4b", "#006a68", "#008685",
"#18a2a1", "#48bfbe", "#86dbdb", "#d0f7f7"),
sequential5petrol = c("#004d4b", "#008685", "#18a2a1", "#86dbdb", "#d0f7f7"),
diverging5rotgruen = c("#EA4F61", "#F6AFA0", "#F4F7F9", "#B5F4F4", "#2AC7C7"),
diverging5rotblau = c("#EA4F61", "#F6AFA0", "#F4F7F9", "#94B7F8", "#0F05A0"),
gender4 = c("#0F05A0", "#94B7F8", "#EA4F61", "#F6AFA0"),
nation4 = c("#0F05A0", "#94B7F8", "#2AC7C7", "#B5F4F4")
"seq6gry" = c("#FAFAFA", "#EAEAEA", "#D6D6D6", "#B8B8B8", "#7C7C7C", "#545454"),
"qual12br" = c("#5D4BFE", "#4AA9FF", "#55FFFF", "#986AD5", "#FC4C99", "#FF919A", "#349894", "#44B14A", "#B7E14E", "#B97624", "#FF7231", "#FFD736"),
"qual12" = c("#3431DE", "#0A8DF6", "#23C3F1", "#7B4FB7", "#DB247D", "#FB737E", "#007C78", "#1F9E31", "#99C32E", "#9A5B01", "#FF720C", "#FBB900"),
"qual12da" = c("#0017BF", "#0072D7", "#00A5D2", "#5E359A", "#BA0062", "#DA5563", "#00615D", "#00770F", "#7BA600", "#7B4100", "#DC5500", "#DA9C00"),
"qual6" = c("#3431DE", "#DB247D", "#1F9E31", "#FBB900", "#23C3F1", "#FF720C"),
"qual6a" = c("#3431DE", "#0A8DF6", "#23C3F1", "#7B4FB7", "#DB247D", "#FB737E"),
"qual6b" = c("#007C78", "#1F9E31", "#99C32E", "#9A5B01", "#FF720C", "#FBB900"),
"seq9blu" = c("#CADEFF", "#AEC2FF", "#93A6FF", "#778AFF", "#5B6EFF", "#4D59E2", "#3E44C5", "#302FA7", "#211A8A"),
"seq9red" = c("#FED2EE", "#FEAED6", "#F589BE", "#F165A5", "#ED408D", "#D1307B", "#B52069", "#991056", "#7D0044"),
"seq9grn" = c("#CFEED8", "#A8E0B3", "#81D18F", "#5BC36A", "#34B446", "#2A9A3C", "#208032", "#166529", "#0C4B1F"),
"seq9brn" = c("#FCDDBB", "#F7BD8C", "#F39D5E", "#EE7D2F", "#EA5D00", "#C84E00", "#A53E00", "#832F00", "#611F00"),
"div9val" = c("#782600", "#CC4309", "#FF720C", "#FFBC88", "#E4E0DF", "#AECBFF", "#6B8EFF", "#3B51FF", "#2F2ABB"),
"div9ntr" = c("#A30059", "#DB247D", "#FF579E", "#FFA8D0", "#E4E0DF", "#A8DBB1", "#55BC5D", "#1F9E31", "#10652A")
)
44 changes: 28 additions & 16 deletions R/view_zuericolors.R
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@
#' @import ggplot2
#' @import ggpubr
#'
#' @examples
#' view_zuericolors('harmonic6')
#' @examples
#' view_zuericolors("qual12")
view_zuericolors <- function(palette) {

# All Available palettes in zuericolors
Expand All @@ -25,18 +25,24 @@ view_zuericolors <- function(palette) {
color <- palettes[[palette_name]]
value <- seq_len(length(color))
data <- data.frame(value, color)
p <- ggplot(data = data, aes(x = as.factor(value),
fill = as.factor(value),
label = color)) +
p <- ggplot(data = data, aes(
x = as.factor(value),
fill = as.factor(value),
label = color
)) +
scale_fill_manual(values = c(color)) +
scale_y_continuous(limits = c(-1, 1)) +
geom_bar() +
theme_void() +
labs(title = name) +
theme(legend.position = "none",
axis.text.x = element_blank(),
plot.title = element_text(size = 10,
hjust = 0.5))
theme(
legend.position = "none",
axis.text.x = element_blank(),
plot.title = element_text(
size = 10,
hjust = 0.5
)
)
p
})
allplots <- ggarrange(plotlist = plot_list, ncol = 2, nrow = 10)
Expand All @@ -49,8 +55,10 @@ view_zuericolors <- function(palette) {
# Return error message if palette (argument) does not match with available
# palettes
if (!(chosen_palette %in% palette_names)) {
message <- paste0("The palette ", "\"", chosen_palette, "\"",
" does not exist. Have you checked for Typos? Please choose one of the palettes above.")
message <- paste0(
"The palette ", "\"", chosen_palette, "\"",
" does not exist. Have you checked for Typos? Please choose one of the palettes above."
)
stop(message, cat(palette_names, sep = "\n"))
}

Expand All @@ -59,16 +67,20 @@ view_zuericolors <- function(palette) {
color <- palettes[[chosen_palette]]
value <- seq_len(length(color))
data <- data.frame(value, color)
p <- ggplot(data = data, aes(x = as.factor(value),
fill = as.factor(value),
label = color)) +
p <- ggplot(data = data, aes(
x = as.factor(value),
fill = as.factor(value),
label = color
)) +
scale_fill_manual(values = c(color)) +
scale_y_continuous(limits = c(-1, 1)) +
geom_bar() +
geom_text(y = -0.05, angle = 90, hjust = 1) +
theme_void() +
labs(title = name) +
theme(legend.position = "none", axis.text.x = element_blank(),
plot.title = element_text(size = 20, hjust = 0.5))
theme(
legend.position = "none", axis.text.x = element_blank(),
plot.title = element_text(size = 20, hjust = 0.5)
)
return(p)
}
81 changes: 63 additions & 18 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,37 +27,82 @@ To check your version of zuericolors, run:
packageVersion("zuericolors")
```

## Available Palettes

<img src='pictures/Palettes.JPG' />

## Usage
## Examples

```{r, message = FALSE}
library(zuericolors)
# Get all the colors from palette "hamronic 6"
get_zuericolors("harmonic6")
# Get all the colors from palette "qual6"
get_zuericolors("qual6")
[1] "#0f05a0" "#2ac7c7" "#0a8df6" "#a2e5b0" "#3d575e" "#23c3f1"
[1] "#3431DE" "#DB247D" "#1D942E" "#FBB900" "#23C3F1" "#FF720C"
# Get first color from palette "diverging5rotgruen"
get_zuericolors("diverging5rotgruen", nth = 1)
# Get first color from palette "div9ntrgry"
get_zuericolors("div9ntrgry", nth = 1)
[1] "#EA4F61"
"#A30059"
# Get first four colors from palette "contrasting12"
get_zuericolors("contrasting12", nth = 1:4)
# Get first four colors from palette "qual12"
get_zuericolors("qual12", nth = 1:4)
[1] "#0f05a0" "#65cd8c" "#960055" "#0098c6"
[1] "#3431DE" "#0A8DF6" "#23C3F1" "#7B4FB7"
# View color palette "harmonic12"
view_zuericolors("harmonic12")
# View color palette "qual12"
view_zuericolors("qual12")
```


<img src='pictures/harmonic12.JPG' />
<img src='pictures/qual12.JPG' />

## Getting help
## Usage

### Grey Palette
The grey color palette is not a palette in the true sense. However, its individual colors are needed for different features in [sszviz](https://github.com/StatistikStadtZuerich/sszvis):

* `LightGry (#FAFAFA)`: A light grey scale is included for use in cases where a data background should be shaded out.
* `PaleGry (#EAEAEA)`: A pale grey shade which is used for elements which should be in the background, but shouldn’t completely fade out. Currently used as the color of the links in the [sankey diagram](https://statistikstadtzuerich.github.io/sszvis/#/sankey).
* `Gry (#D6D6D6)`: A grey scale is included for use in cases where a data value should be shaded out. Currently, it is used for the color of inactive lines in the coordinated map and line chart example. At the moment, it has only one value.
* `DimGry (#B8B8B8)`: A dim grey shade used for highlighting elements which are normally grey (sszvis.scaleGry()). Currently, it is used as the highlight color in the [bubble map](https://statistikstadtzuerich.github.io/sszvis/#/map-signature).
* `MedGry (#7C7C7C)`: The medium grey color, as a standalone value.
* `DeepGry (#545454)`: The deep grey color, as a standalone value.

<img src='pictures/seq6gry.JPG' />

### Qualitative Palettes
Qualitative palettes are used for data that has distinct categories. They distinguish categories based on color contrast. They should never be used to compare values, use sequential or divergent palettes in this case.

<img src='pictures/qual12.JPG' />

<img src='pictures/qual6.JPG' />

<img src='pictures/qual6a.JPG' />

<img src='pictures/qual6b.JPG' />

<img src='pictures/qual12br.JPG' />

<img src='pictures/qual12da.JPG' />

### Sequential Palettes
Sequential palettes are used to compare values. These palettes are designed to have the same brightness for the same input value.

<img src='pictures/seq9blu.JPG' />

<img src='pictures/seq9red.JPG' />

<img src='pictures/seq9grn.JPG' />

<img src='pictures/seq9brn.JPG' />

### Divergent Palettes
Divergent palettes are used to compare data that has two extremes. These scales are
designed to have the same brightness for the same input value. They come in two color variations:
the valued (red-green) variation is used for data that has negative-positive characteristics, the
neutral (brown-blue) variation is used in cases where no valuation is wanted.

<img src='pictures/div9val.JPG' />

<img src='pictures/div9ntr.JPG' />

## Getting help
If you encounter a bug, please contact [email protected].
12 changes: 6 additions & 6 deletions man/get_zuericolors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/palettes.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion man/view_zuericolors.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file modified pictures/Palettes.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/div9ntr.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/div9val.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file removed pictures/harmonic12.JPG
Binary file not shown.
Binary file added pictures/qual12.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/qual12br.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/qual12da.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/qual6.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/qual6a.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/qual6b.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/seq6gry.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/seq9blu.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/seq9brn.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/seq9grn.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added pictures/seq9red.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
18 changes: 9 additions & 9 deletions tests/testthat/test-get_zuericolors.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ test_that("get entire palette with just palette name", {
})

test_that("special dealing with palette name", {
expect_equal(get_zuericolors("hArmonic6"), palettes[["harmonic6"]])
expect_equal(get_zuericolors(harmonic6), palettes[["harmonic6"]])
expect_equal(get_zuericolors("qUal6"), palettes[["qual6"]])
expect_equal(get_zuericolors(qual6), palettes[["qual6"]])
})

test_that("get specific colors", {
expect_equal(
get_zuericolors("contrasting12hell", nth = 11),
palettes[["contrasting12hell"]][[11]]
get_zuericolors("qual12da", nth = 11),
palettes[["qual12da"]][[11]]
)
expect_equal(
get_zuericolors("sequential9petrol", nth = 7:9),
palettes[["sequential9petrol"]][7:9]
get_zuericolors("div9valgry", nth = 7:9),
palettes[["div9valgry"]][7:9]
)
})

test_that("asking for more colors than exist", {
expect_length(get_zuericolors("harmonic6"), 6)
expect_error(get_zuericolors("harmonic6", nth = 20), "6 colors")
expect_error(get_zuericolors("harmonic6", nth = 5:7), "6 colors")
expect_length(get_zuericolors("qual6a"), 6)
expect_error(get_zuericolors("qual6a", nth = 20), "6 colors")
expect_error(get_zuericolors("qual6a", nth = 5:7), "6 colors")
})

test_that("error is raised with missing arguments", {
Expand Down
8 changes: 4 additions & 4 deletions tests/testthat/test-view_zuericolors.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
test_that("a plot is returned", {

# when a palette is specified
expect_type(view_zuericolors("harmonic6"), "list")
expect_s3_class(view_zuericolors("harmonic6"), c("gg", "ggplot"))
expect_type(view_zuericolors("qual6"), "list")
expect_s3_class(view_zuericolors("qual6"), c("gg", "ggplot"))

# when no palette is specified
expect_type(view_zuericolors(), "list")
Expand All @@ -12,8 +12,8 @@ test_that("a plot is returned", {

# test 2: special handling of palette name argument
test_that("deal with palette names correctly", {
expect_s3_class(view_zuericolors(harmonic6), c("gg", "ggplot"))
expect_s3_class(view_zuericolors(haRmonic6), c("gg", "ggplot"))
expect_s3_class(view_zuericolors(qual6), c("gg", "ggplot"))
expect_s3_class(view_zuericolors(qual6), c("gg", "ggplot"))
})

# test 3: correct colors are returned
Expand Down

0 comments on commit 901589a

Please sign in to comment.