This brief example shows the importance of careful colour selection.
It uses the goby example from Chapter 10, which compared gonad mass for
fish from five rivers, while adjusting for fish size (as total
length).
Load standard package set for examples
Load colorBlindness package
library(colorBlindness)
Import data and produce standard ggplot scatterplot, plus basic
adjustments from theme “qk”. Use river to identify groups and generate
colours. For clarity, particularly with colorBlindness, we’ll hide the
legend as well.
bleeker1 <- read_csv("../data/bleeker1.csv")
Rows: 69 Columns: 3── Column specification ───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Delimiter: ","
chr (1): river
dbl (2): tl, gm
ℹ Use `spec()` to retrieve the full column specification for this data.
ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
cbt<-ggplot(data=bleeker1, aes(x=tl,y=gm, color=river))+
geom_point() +
geom_smooth(method="lm", se=FALSE)+
labs(x = "Total length", y = "Gonad mass",
)+
theme_qk()+ theme(legend.position="none")
cbt
Note that this plot just uses ggplot default colours.
Now see how this graph looks with various forms of
colourblindness
cvd<-cvdPlot(cbt)
`geom_smooth()` using formula = 'y ~ x'
cvd
Now change plot to colour palette that is more compatible with a wide
range of viewers. We’ll use the viridis package, and its default
settings, though there are several other options in other packages, such
as rcolorBrewer and ggsci.
cbt2<-cbt+scale_color_viridis(discrete=TRUE)
cbt2
This plot is OK on screen, but when projected, in, e.g. a lecture,
the yellow tends to fade into the backgroun, so we’d use a different
palette.
On the plus side, this palette still works with several forms of
colorblindness:
cvd2<-cvdPlot(cbt2)
`geom_smooth()` using formula = 'y ~ x'
cvd2
Here’s another option, using the u_chicago palette from
ggsci
cbt3<-cbt+scale_color_uchicago()
cbt3
This palette still works with several forms of colorblindness:
cvd3<-cvdPlot(cbt3)
`geom_smooth()` using formula = 'y ~ x'
cvd3
For teaching, export these plots.
ggsave("cbt.pdf", plot = cbt)
ggsave("cbt2.pdf", plot = cbt2)
ggsave("cvd.pdf", plot = cvd)
ggsave("cvd2.pdf", plot = cvd2)
ggsave("cbt3.pdf", plot = cbt3)
ggsave("cvd2.pdf", plot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