This RMarkdown document is part of the Generic Skills Component (GSK) of the Course of the Foundation Studies Programme at Srishti Manipal Institute of Art, Design, and Technology, Bangalore India. The material is based on A Layered Grammar of Graphics by Hadley Wickham. The course is meant for First Year students pursuing a Degree in Art and Design.
The intent of this GSK part is to build Skill in coding in R, and also appreciate R as a way to metaphorically visualize information of various kinds, using predominantly geometric figures and structures.
All RMarkdown files combine code, text, web-images, and figures developed using code. Everything is text; code chunks are enclosed in fences (```)
The method followed will be based on PRIMM:
parameters
of the code
do and write comments to explain. What bells and
whistles can you see?parameters
code provided to
understand the options
available. Write
comments to show what you have aimed for and achieved.In the following, there is some boiler plate code demonstrating the use of colour palettes in R. There are places where YOUR TURN is mention; copy and play with the boiler plate code to see what happens !
We will use the penguins
dataset built into the
palmerpenguins
package. Your should try other datasets
too!
Here is a glimpse of the data:
glimpse(penguins)
Rows: 344
Columns: 8
$ species <fct> Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie, Adelie…
$ island <fct> Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Torgersen, Tor…
$ bill_length_mm <dbl> 39.1, 39.5, 40.3, NA, 36.7, 39.3, 38.9, 39.2, 34.1, 42.0, 37.8, 37.8, 41.1, 38.6, 34.6, 36.6, 38.7, 42…
$ bill_depth_mm <dbl> 18.7, 17.4, 18.0, NA, 19.3, 20.6, 17.8, 19.6, 18.1, 20.2, 17.1, 17.3, 17.6, 21.2, 21.1, 17.8, 19.0, 20…
$ flipper_length_mm <int> 181, 186, 195, NA, 193, 190, 181, 195, 193, 190, 186, 180, 182, 191, 198, 185, 195, 197, 184, 194, 174…
$ body_mass_g <int> 3750, 3800, 3250, NA, 3450, 3650, 3625, 4675, 3475, 4250, 3300, 3700, 3200, 3800, 4400, 3700, 3450, 45…
$ sex <fct> male, female, female, NA, female, male, female, male, NA, NA, NA, NA, female, male, male, female, fema…
$ year <int> 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, 2007, …
Note that the unit of observation here is one-row-per-penguin.
Variables you need for this lab:
Fill and colour scales in ggplot2 can use the same palettes. Some shapes such as lines only accept the colour aesthetic, while others, such as polygons, accept both colour and fill aesthetics. In the latter case, the colour refers to the border of the shape, and the fill to the interior.
## A look at all 25 symbols
df <- data.frame(x = 1:5,
y = rep(rev(seq(0, 24, by = 5)), each = 5),
z = 1:25)
s <- ggplot(df, aes(x = x, y = y)) +
geom_text(aes(label = z, y = y - 1)) +
theme_void()
s + geom_point(aes(shape = z), size = 4) + scale_shape_identity()
All symbols have a foreground colour, so if we add
color = "navy"
, they all are affected.
s + geom_point(aes(shape = z), size = 4, colour = "blue") + scale_shape_identity()
While all symbols have a foreground colour, symbols 21-25 also take a
background colour (fill). So if we add fill = "orchid"
,
only the last row of symbols are affected.
s + geom_point(aes(shape = z), size = 4, colour = "blue", fill = "orchid") + scale_shape_identity()
WHAT IS THE DIFFERENCE BETWEEN CATEGORICAL, ORDINAL AND INTERVAL VARIABLES?
In order to use color with your data, most importantly, you need to know if you’re dealing with discrete or continuous variables.
We have the following example packages that offer palettes in R:
RColorBrewer
wesanderson
paletteer
colorspace
See Appendix for a detailed graphical analysis of these palette packages.
These palettes can be:
Sequential (type = “seq”) palettes are suited to ordered data that progress from low to high. Lightness steps dominate the look of these schemes, with light colors for low data values to dark colors for high data values. (for numerical data, that are ordered)
Diverging (type = “div”) palettes put equal emphasis on mid-range critical values and extremes at both ends of the data range. The critical class or break in the middle of the legend is emphasized with light colors and low and high extremes are emphasized with dark colors that have contrasting hues.(for numerical data that can be positive or negative, often representing deviations from some norm or baseline)
Qualitative (type = “qual”) palettes do not imply magnitude differences between legend classes, and hues are used to create the primary visual differences between classes. Qualitative schemes are best suited to representing nominal or categorical data. (for qualitative unordered data)
We will create simple base plots in ggplot
and see how
we may alter the colour scales using palettes.
names(penguins)
[1] "species" "island" "bill_length_mm" "bill_depth_mm" "flipper_length_mm" "body_mass_g"
[7] "sex" "year"
p1 <- penguins %>%
drop_na() %>%
# pipe data into ggplot
# after removing data rows that have missing ( NA ) values
ggplot(aes(y = body_mass_g, x = flipper_length_mm,
color = species # COLOUR = DISCRETE/QUAL VARIABLE
)) +
geom_point() +
labs(title = "Default Colours in ggplot",
subtitle = "P1: DISCRETE/QUAL Colour Palette")
p2 <-
penguins %>%
drop_na() %>%
# pipe the data into ggplot,
# after removing data rows that have missing ( NA ) values
ggplot(aes(y = body_mass_g, x = flipper_length_mm,
color = bill_length_mm # COLOUR = CONT/QUANT VARIABLE
)) +
geom_point() +
labs(title = "Default Colours in ggplot",
subtitle = "P2: CONTINUOUS/QUANT Colour Palette")
p1
p2
Note that these use the default colours in R.
The commands below are used to fill colours based on Qualitative Variables:
scale_colour/fill_discrete
scale_colour/fill_brewer
# RColorBrewerNow to use these!
RColorBrewer
RColorBrewer::brewer.pal.info
maxcolors category colorblind
BrBG 11 div TRUE
PiYG 11 div TRUE
PRGn 11 div TRUE
PuOr 11 div TRUE
RdBu 11 div TRUE
RdGy 11 div FALSE
RdYlBu 11 div TRUE
RdYlGn 11 div FALSE
Spectral 11 div FALSE
Accent 8 qual FALSE
Dark2 8 qual TRUE
Paired 12 qual TRUE
Pastel1 9 qual FALSE
Pastel2 8 qual FALSE
Set1 9 qual FALSE
Set2 8 qual TRUE
Set3 12 qual FALSE
Blues 9 seq TRUE
BuGn 9 seq TRUE
BuPu 9 seq TRUE
GnBu 9 seq TRUE
Greens 9 seq TRUE
Greys 9 seq TRUE
Oranges 9 seq TRUE
OrRd 9 seq TRUE
PuBu 9 seq TRUE
PuBuGn 9 seq TRUE
PuRd 9 seq TRUE
Purples 9 seq TRUE
RdPu 9 seq TRUE
Reds 9 seq TRUE
YlGn 9 seq TRUE
YlGnBu 9 seq TRUE
YlOrBr 9 seq TRUE
YlOrRd 9 seq TRUE
RColorBrewer::display.brewer.all()
p1 +
# default palette = "Blues"
scale_colour_brewer() +
labs(title = "Brewer Palette = Blues")
p1 +
scale_color_brewer(palette = "Spectral") +
labs(title = "Brewer Palette = Spectral")
wesanderson
paletteswesanderson::wes_palettes %>% names()
[1] "BottleRocket1" "BottleRocket2" "Rushmore1" "Rushmore" "Royal1" "Royal2" "Zissou1"
[8] "Darjeeling1" "Darjeeling2" "Chevalier1" "FantasticFox1" "Moonrise1" "Moonrise2" "Moonrise3"
[15] "Cavalcanti1" "GrandBudapest1" "GrandBudapest2" "IsleofDogs1" "IsleofDogs2"
p1 +
scale_colour_discrete(type = wes_palette(name = "GrandBudapest1",
n = 3)) +
labs(title = "Wes Anderson Palette: GrandBudapest")
# We can also specify colour codes ourselves with scale_x_discrete.
# Use argument "values" instead of "type"
manual_colours <- c("#afc4b8", "#f1a4b2", "#ffb1e1")
manual_colours
[1] "#afc4b8" "#f1a4b2" "#ffb1e1"
p1 +
scale_colour_manual(values = manual_colours) +
labs(title = "Manual Colours")
RColorBrewer
# scale_x_brewer() for DISCRETE data
p1 +
scale_colour_brewer(palette = "Spectral") +
labs(title = "RColorBrewer Palette = Spectral")
paletteer
palettespalettes_d_names
# A tibble: 2,037 × 5
package palette length type novelty
<chr> <chr> <int> <chr> <lgl>
1 awtools a_palette 8 sequential TRUE
2 awtools ppalette 8 qualitative TRUE
3 awtools bpalette 16 qualitative TRUE
4 awtools gpalette 4 sequential TRUE
5 awtools mpalette 9 qualitative TRUE
6 awtools spalette 6 qualitative TRUE
7 basetheme brutal 10 qualitative TRUE
8 basetheme clean 10 qualitative TRUE
9 basetheme dark 10 qualitative TRUE
10 basetheme deepblue 10 qualitative TRUE
# … with 2,027 more rows
# ℹ Use `print(n = ...)` to see more rows
palettes_dynamic_names
package palette length type
1 cartography blue.pal 20 sequential
2 cartography orange.pal 20 sequential
3 cartography red.pal 20 sequential
4 cartography brown.pal 20 sequential
5 cartography green.pal 20 sequential
6 cartography purple.pal 20 sequential
7 cartography pink.pal 20 sequential
8 cartography wine.pal 20 sequential
9 cartography grey.pal 20 sequential
10 cartography turquoise.pal 20 sequential
11 cartography sand.pal 20 sequential
12 cartography taupe.pal 20 sequential
13 cartography kaki.pal 20 sequential
14 cartography harmo.pal 20 sequential
15 cartography pastel.pal 20 qualitative
16 cartography multi.pal 20 qualitative
17 ggthemes_ptol qualitative 12 qualitative
18 ggthemes_solarized yellow 8 qualitative
19 ggthemes_solarized orange 8 qualitative
20 ggthemes_solarized red 8 qualitative
21 ggthemes_solarized magenta 8 qualitative
22 ggthemes_solarized violet 8 qualitative
23 ggthemes_solarized blue 8 qualitative
24 ggthemes_solarized cyan 8 qualitative
25 ggthemes_solarized green 8 qualitative
paletteer_d("dutchmasters::pearl_earring")
<colors>
#A65141FF #E7CDC2FF #80A0C7FF #394165FF #FCF9F0FF #B1934AFF #DCA258FF #100F14FF #8B9DAFFF #EEDA9DFF #E8DCCFFF
paletteer_dynamic("ggthemes_ptol::qualitative", n = 3)
<colors>
#4477AAFF #DDCC77FF #CC6677FF
p1 +
scale_colour_paletteer_d("ggthemes_ptol::qualitative",
dynamic = TRUE) +
labs(title = "ggThemes Palette: Qualitative",
subtitle = "")
# I like Vermeer's "Girl with the Pearl Earring"!
p1 +
scale_colour_paletteer_d("dutchmasters::pearl_earring",
dynamic = FALSE) +
labs(title = "Palettes from `paletteer`",
subtitle = " Palette from Vermeer: Girl with Pearl Earring")
The commands below are used to fill colours based on Quantitative Variables:
scale_colour/fill_gradient
(Two colour gradient)scale_colour/fill_gradient2
(Three colour
gradient)scale_colour/fill_gradientn
(Specify Palette, from
other packages also, like wesanderson
)scale_colour/fill_distiller
(Palettes from
RColorBrewer)Creates a pallete containing continuous shades between two colours:
p2 +
scale_color_gradient(
low = "yellow", # Play with this in the chunk below
high = "purple") + # Play with this in the chnk below
labs(title = "Two Colour Gradients",
subtitle = "P2: Continuous 2-Colour Pallete")
Sometimes we want a palette this way: a midpoint colour, and colours for the two extremes of a continuous variable:
colour_midpoint <- mean(penguins$bill_length_mm,
na.rm = TRUE) # remove missing values
# Struggled all morning on 22 Aug 2020 to get at this ;-D
# Play with the function: 0/mean/median/mode/max/min
p2 +
scale_colour_gradient2(
low = "brown", # Play with this in the chunk below
mid = "white", # Play with this in the chunk below
high = "purple", # Play with this in the chunk below
midpoint = colour_midpoint, # see above
space = "Lab", # don't mess with this!
na.value = "grey50") +
labs(title = "Three colour continuous gradient",
subtitle = "Mid Colour mapped to midpoint of data variable",
caption = "Colours inspired by my favourite cocker spaniel, Lord Chestnut") # Play with these
# grDevices Palettes
p2 +
scale_colour_gradientn(
colours = terrain.colors(10)) +
# Try these:
# heat.colors() / topo.colors() / cm.colors() / rainbow()
labs(title = "N-colour continuous gradients",
subtitle = "Palettes from grDevices",
caption = "Palette: terrain.colors")
wesanderson
Paletteswes_palettes
$BottleRocket1
[1] "#A42820" "#5F5647" "#9B110E" "#3F5151" "#4E2A1E" "#550307" "#0C1707"
$BottleRocket2
[1] "#FAD510" "#CB2314" "#273046" "#354823" "#1E1E1E"
$Rushmore1
[1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A" "#F2300F"
$Rushmore
[1] "#E1BD6D" "#EABE94" "#0B775E" "#35274A" "#F2300F"
$Royal1
[1] "#899DA4" "#C93312" "#FAEFD1" "#DC863B"
$Royal2
[1] "#9A8822" "#F5CDB4" "#F8AFA8" "#FDDDA0" "#74A089"
$Zissou1
[1] "#3B9AB2" "#78B7C5" "#EBCC2A" "#E1AF00" "#F21A00"
$Darjeeling1
[1] "#FF0000" "#00A08A" "#F2AD00" "#F98400" "#5BBCD6"
$Darjeeling2
[1] "#ECCBAE" "#046C9A" "#D69C4E" "#ABDDDE" "#000000"
$Chevalier1
[1] "#446455" "#FDD262" "#D3DDDC" "#C7B19C"
$FantasticFox1
[1] "#DD8D29" "#E2D200" "#46ACC8" "#E58601" "#B40F20"
$Moonrise1
[1] "#F3DF6C" "#CEAB07" "#D5D5D3" "#24281A"
$Moonrise2
[1] "#798E87" "#C27D38" "#CCC591" "#29211F"
$Moonrise3
[1] "#85D4E3" "#F4B5BD" "#9C964A" "#CDC08C" "#FAD77B"
$Cavalcanti1
[1] "#D8B70A" "#02401B" "#A2A475" "#81A88D" "#972D15"
$GrandBudapest1
[1] "#F1BB7B" "#FD6467" "#5B1A18" "#D67236"
$GrandBudapest2
[1] "#E6A0C4" "#C6CDF7" "#D8A499" "#7294D4"
$IsleofDogs1
[1] "#9986A5" "#79402E" "#CCBA72" "#0F0D0E" "#D9D0D3" "#8D8680"
$IsleofDogs2
[1] "#EAD3BF" "#AA9486" "#B6854D" "#39312F" "#1C1718"
names(wes_palettes)
[1] "BottleRocket1" "BottleRocket2" "Rushmore1" "Rushmore" "Royal1" "Royal2" "Zissou1"
[8] "Darjeeling1" "Darjeeling2" "Chevalier1" "FantasticFox1" "Moonrise1" "Moonrise2" "Moonrise3"
[15] "Cavalcanti1" "GrandBudapest1" "GrandBudapest2" "IsleofDogs1" "IsleofDogs2"
p2 +
scale_colour_gradientn(
colors = wes_palette(name = "GrandBudapest1",
n = 4), # Keep an eye on "n".
na.value = "grey") +
# Try these:
# "BottleRocket1" "BottleRocket2" "Rushmore1"
# "Rushmore" "Royal1" "Royal2"
# "Zissou1" "Darjeeling1" "Darjeeling2"
# "Chevalier1" "FantasticFox1" "Moonrise1"
# "Moonrise2" "Moonrise3" "Cavalcanti1"
# "GrandBudapest1" "GrandBudapest2" "IsleofDogs1"
# "IsleofDogs2"
# Keep an eye on "n".
labs(title = "N-colour continuous gradients",
subtitle = "Palettes from wesanderson",
caption = "Palette: GrandBudapest1") # Change this caption based on palette choice
RColorBrewer
Recall Palette types
seq
for continuous data mapped to colourqual
for categorical data mapped to colour (
discrete)div
continuous data mapped to colour, that has pos and
neg extremes from a middle valuebrewer.pal.info
maxcolors category colorblind
BrBG 11 div TRUE
PiYG 11 div TRUE
PRGn 11 div TRUE
PuOr 11 div TRUE
RdBu 11 div TRUE
RdGy 11 div FALSE
RdYlBu 11 div TRUE
RdYlGn 11 div FALSE
Spectral 11 div FALSE
Accent 8 qual FALSE
Dark2 8 qual TRUE
Paired 12 qual TRUE
Pastel1 9 qual FALSE
Pastel2 8 qual FALSE
Set1 9 qual FALSE
Set2 8 qual TRUE
Set3 12 qual FALSE
Blues 9 seq TRUE
BuGn 9 seq TRUE
BuPu 9 seq TRUE
GnBu 9 seq TRUE
Greens 9 seq TRUE
Greys 9 seq TRUE
Oranges 9 seq TRUE
OrRd 9 seq TRUE
PuBu 9 seq TRUE
PuBuGn 9 seq TRUE
PuRd 9 seq TRUE
Purples 9 seq TRUE
RdPu 9 seq TRUE
Reds 9 seq TRUE
YlGn 9 seq TRUE
YlGnBu 9 seq TRUE
YlOrBr 9 seq TRUE
YlOrRd 9 seq TRUE
# scale_color_distiller() and scale_fill_distiller()
# are used to apply the ColorBrewer colour scales
# to continuous data.
p2 +
scale_colour_distiller(
palette = "YlGnBu") + # Play with this palette
labs(title = "RColorBrewer Palette")
paletteer
palettesThis palette seems to have everything accessible in a simple way!
NOTE: In order to access some palettes in paletteer
, you
may be asked to install other packages. E.g. harrypotter
or
scico
. These need not be brought into your session using
library()
but are accessed directly by
paletteer
which is very convenient!!
# What continuous palettes are there in paletteer?
paletteer::palettes_c_names
# A tibble: 330 × 3
package palette type
<chr> <chr> <chr>
1 gameofthrones targaryen sequential
2 gameofthrones targaryen2 sequential
3 gameofthrones stark sequential
4 gameofthrones stark2 sequential
5 gameofthrones lannister sequential
6 gameofthrones martell sequential
7 gameofthrones tully diverging
8 gameofthrones greyjoy sequential
9 gameofthrones baratheon sequential
10 gameofthrones baratheon2 sequential
# … with 320 more rows
# ℹ Use `print(n = ...)` to see more rows
OK, one of the Games of Thrones Palettes, and Harry Potter!
p2 +
scale_colour_paletteer_c("gameofthrones::jon_snow") +
labs(title = "Using Paletteer",
subtitle = "Continuous Palette-Game of Thrones: Jon Snow",
caption = "Oh you awful Srishti people...") +
# Harry Potter Gryffindor Palette.
# Will ask for `harrypotter` package to be installed. Say yes!
p2 +
scale_colour_paletteer_c("harrypotter::gryffindor") +
labs(title = "Using Paletteer",
subtitle = "Continuous Palette-Harry Potter:Gryffindor")