1 #+date: 2016-10-05 21:48:11 +0800
2 #+title: Facet labels in R
3 #+filetags: R graph code
5 Getting used to the grammar of ggplot2 takes some time, but so far it's not been disappointing. Wanting to split a
6 scatterplot by segment, I used =facet_grid=. That by default shows a
7 label on each subplot, using the values in the variable by which the
10 As that often isn't very descriptive in itself, there needs to be a way
11 to re-label these subplots. That way is =as_labeller=, as shown in the
16 ggplot(outputs, aes(x = date_var,y = value_var), alpha = 0.8) +
17 geom_point(aes(y = value_var, colour = colour_var)) +
19 theme(legend.position = "none",
20 axis.text.y = element_blank(),
21 axis.text.x = element_blank()) +
22 scale_x_date(date_breaks = '1 week') +
23 labs(y = "Value", x = "Date",
25 scale_colour_manual("Legend", values = named_coloring_vector)) +
26 scale_fill_manual(“", values = c("grey12”)) + facet_grid(. ~ Segment,
27 labeller = as_labeller(c("yes" = "Segment A", "no" = "Segment B")))
31 #+CAPTION: Example plot with 2 facets labelled Segment B and Segment A.
32 #+ATTR_HTML: :class img-fluid :alt Example plot with 2 facets labelled Segment B and Segment A.
33 [[file:assets/2016-10-05_R-facet.png]]