Let’s consider a small report where you present several graphics to your audience. The report is composed of:
# Libraries
library(tidyverse)
library(hrbrthemes)
library(patchwork)
library(viridis)
# create dummy data
data <- data.frame(
product = LETTERS[1:5],
value = c(10, 14, 18, 6, 15)
)
# Plot
a <- data %>%
ggplot( aes(x=product, y=value, fill=product)) +
geom_bar(stat="identity", width=0.5) +
scale_fill_viridis(discrete=TRUE) +
ggtitle("How much the products cost") +
theme_ipsum() +
theme(
plot.title = element_text(size=12),
legend.position = "none"
) +
xlab("") +
ylab("Cost in M$")
# create dummy data
data <- data.frame(
year = rep( seq(1,10), 5 ),
product = rep(LETTERS[1:5], each=10 ),
value = c(
seq(1,10) + sample( 1:3, 10, replace = TRUE),
seq(5,14) + sample( 4:10, 10, replace=TRUE),
seq(10,1)*2 + sample( 3:5, 10, replace=TRUE),
seq(20,11) + sample( 12:17, 10, replace=TRUE),
seq(1,10) + sample( 40:10, 10))
)
# Plot
b <- data %>%
ggplot( aes(x=year, y=value, color=product)) +
geom_line() +
scale_color_discrete() +
scale_x_continuous(breaks=c(1:10)) +
ggtitle("How much the products make") +
theme_ipsum() +
theme(
plot.title = element_text(size=12)
) +
xlab("Year") +
ylab("Benefit in M$")
# Show
a + b
Here, it is very hard to make the link between the two charts, since the color palette used for each chart is different. Product A is in dark purple on the left graphic, and in red on the right one. It takes time to make the link.
When you have several graphic, use the same color for the same item throughout the report:
# Libraries
library(tidyverse)
library(hrbrthemes)
library(patchwork)
library(viridis)
# create dummy data
data <- data.frame(
product = LETTERS[1:5],
value = c(10, 14, 18, 6, 15)
)
# Plot
a <- data %>%
ggplot( aes(x=product, y=value, fill=product)) +
geom_bar(stat="identity", width=0.5) +
scale_fill_viridis(discrete=TRUE) +
ggtitle("How much the products cost") +
theme_ipsum() +
theme(
plot.title = element_text(size=12),
legend.position = "none"
) +
xlab("") +
ylab("Cost in M$")
# create dummy data
data <- data.frame(
year = rep( seq(1,10), 5 ),
product = rep(LETTERS[1:5], each=10 ),
value = c(
seq(1,10) + sample( 1:3, 10, replace = TRUE),
seq(5,14) + sample( 4:10, 10, replace=TRUE),
seq(10,1)*2 + sample( 3:5, 10, replace=TRUE),
seq(20,11) + sample( 12:17, 10, replace=TRUE),
seq(1,10) + sample( 40:10, 10))
)
# Plot
b <- data %>%
ggplot( aes(x=year, y=value, color=product)) +
geom_line() +
scale_color_viridis(discrete=TRUE) +
scale_x_continuous(breaks=c(1:10)) +
ggtitle("How much the products make") +
theme_ipsum() +
theme(
plot.title = element_text(size=12)
) +
xlab("Year") +
ylab("Benefit in M$")
# Show
a + b
That’s better, isn’t it?
Any thoughts on this? Found any mistake? Disagree? Please drop me a word on twitter or in the comment section below:
A work by Yan Holtz for data-to-viz.com