Exercises
- Create an object that represents a pink histogram with 100 bins.
pinkhist <- geom_histogram(fill = "pink", bins = 100)
ggplot(diamonds, aes(x = price)) +
pinkhist +
labs(y = "Frequency",
x = "Price")
2. Create an object that represents a fill scale with the Blues ColorBrewer palette.
blues_fill_scale <- scale_fill_brewer(palette = "Blues")
ggplot(data = diamonds, aes(x = cut, y = price, fill = cut))+
geom_boxplot()+
theme_minimal()+
blues_fill_scale
- Read the source code for theme_grey(). What are its arguments? How does it work?
function (base_size = 11, base_family = "", base_line_size = base_size/22,
base_rect_size = base_size/22)
{
half_line <- base_size/2
t <- theme(line = element_line(colour = "black", linewidth = base_line_size,
linetype = 1, lineend = "butt"), rect = element_rect(fill = "white",
colour = "black", linewidth = base_rect_size, linetype = 1),
text = element_text(family = base_family, face = "plain",
colour = "black", size = base_size, lineheight = 0.9,
hjust = 0.5, vjust = 0.5, angle = 0, margin = margin(),
debug = FALSE), axis.line = element_blank(), axis.line.x = NULL,
axis.line.y = NULL, axis.text = element_text(size = rel(0.8),
colour = "grey30"), axis.text.x = element_text(margin = margin(t = 0.8 *
half_line/2), vjust = 1), axis.text.x.top = element_text(margin = margin(b = 0.8 *
half_line/2), vjust = 0), axis.text.y = element_text(margin = margin(r = 0.8 *
half_line/2), hjust = 1), axis.text.y.right = element_text(margin = margin(l = 0.8 *
half_line/2), hjust = 0), axis.text.r = element_text(margin = margin(l = 0.8 *
half_line/2, r = 0.8 * half_line/2), hjust = 0.5),
axis.ticks = element_line(colour = "grey20"), axis.ticks.length = unit(half_line/2,
"pt"), axis.ticks.length.x = NULL, axis.ticks.length.x.top = NULL,
axis.ticks.length.x.bottom = NULL, axis.ticks.length.y = NULL,
axis.ticks.length.y.left = NULL, axis.ticks.length.y.right = NULL,
axis.minor.ticks.length = rel(0.75), axis.title.x = element_text(margin = margin(t = half_line/2),
vjust = 1), axis.title.x.top = element_text(margin = margin(b = half_line/2),
vjust = 0), axis.title.y = element_text(angle = 90,
margin = margin(r = half_line/2), vjust = 1), axis.title.y.right = element_text(angle = -90,
margin = margin(l = half_line/2), vjust = 1), legend.background = element_rect(colour = NA),
legend.spacing = unit(2 * half_line, "pt"), legend.spacing.x = NULL,
legend.spacing.y = NULL, legend.margin = margin(half_line,
half_line, half_line, half_line), legend.key = NULL,
legend.key.size = unit(1.2, "lines"), legend.key.height = NULL,
legend.key.width = NULL, legend.key.spacing = unit(half_line,
"pt"), legend.text = element_text(size = rel(0.8)),
legend.title = element_text(hjust = 0), legend.ticks.length = rel(0.2),
legend.position = "right", legend.direction = NULL, legend.justification = "center",
legend.box = NULL, legend.box.margin = margin(0, 0, 0,
0, "cm"), legend.box.background = element_blank(),
legend.box.spacing = unit(2 * half_line, "pt"), panel.background = element_rect(fill = "grey92",
colour = NA), panel.border = element_blank(), panel.grid = element_line(colour = "white"),
panel.grid.minor = element_line(linewidth = rel(0.5)),
panel.spacing = unit(half_line, "pt"), panel.spacing.x = NULL,
panel.spacing.y = NULL, panel.ontop = FALSE, strip.background = element_rect(fill = "grey85",
colour = NA), strip.clip = "inherit", strip.text = element_text(colour = "grey10",
size = rel(0.8), margin = margin(0.8 * half_line,
0.8 * half_line, 0.8 * half_line, 0.8 * half_line)),
strip.text.x = NULL, strip.text.y = element_text(angle = -90),
strip.text.y.left = element_text(angle = 90), strip.placement = "inside",
strip.placement.x = NULL, strip.placement.y = NULL, strip.switch.pad.grid = unit(half_line/2,
"pt"), strip.switch.pad.wrap = unit(half_line/2,
"pt"), plot.background = element_rect(colour = "white"),
plot.title = element_text(size = rel(1.2), hjust = 0,
vjust = 1, margin = margin(b = half_line)), plot.title.position = "panel",
plot.subtitle = element_text(hjust = 0, vjust = 1, margin = margin(b = half_line)),
plot.caption = element_text(size = rel(0.8), hjust = 1,
vjust = 1, margin = margin(t = half_line)), plot.caption.position = "panel",
plot.tag = element_text(size = rel(1.2), hjust = 0.5,
vjust = 0.5), plot.tag.position = "topleft", plot.margin = margin(half_line,
half_line, half_line, half_line), complete = TRUE)
ggplot_global$theme_all_null %+replace% t
}
<bytecode: 0x564f93348fd0>
<environment: namespace:ggplot2>
# It creates a theme object called t
# uses %+replace% to replace the existing theme with t
# ggplot_global$theme_all_null %+replace% t
# ggplot_global$theme_all_null doesn't exist globally, so must refer to the current plot that you're adding theme_grey to.
- Create scale_colour_wesanderson(). It should have a parameter to pick the palette from the wesanderson package, and create either a continuous or discrete scale.
library(wesanderson)
scale_colour_wesanderson <- function(palette, type = "discrete", ...){
scale_color_manual(values = wes_palette(name = palette, type = type, ...), ...)
}
ggplot(diamonds, aes(x = carat, y = price, color = cut))+
geom_point()+
scale_colour_wesanderson(palette = "Cavalcanti1")+
theme_minimal()