]> git.vanrenterghem.biz Git - www2.vanrenterghem.biz.git/blob - source/posts/facet_labels_in_R.org
609a8ee2f740914a109b33cd1ee291d84e0696e0
[www2.vanrenterghem.biz.git] / source / posts / facet_labels_in_R.org
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
8 plot is faceted.
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
12 example code below.
14 Example:
15 #+BEGIN_SRC R
16 ggplot(outputs, aes(x = date_var,y = value_var), alpha = 0.8) + 
17   geom_point(aes(y = value_var, colour = colour_var)) + 
18   geom_smooth() + 
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",
24   title = "Example") +
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")))
28 #+END_SRC
30 Output: 
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]]