In the following use of facet_wrap
, both the year
and model
are displayed in the plot labels.
library(tidyverse)
mpg %>%
filter(manufacturer=='audi')%>%
ggplot(aes(cty, hwy))
geom_point(aes(col = model))
facet_wrap(year~model)
We already colored the points by model
and it is shown in the legend, so we dont really need model
in each facet label. How can we remove model
from the labels?
CodePudding user response:
The easiest way would be to adjust the labeler function to only extract labels for the first variable. You can do that with
mpg %>%
filter(manufacturer=='audi')%>%
ggplot(aes(cty, hwy))
geom_point(aes(col = model))
facet_wrap(~year model, labeller=function(x) {x[1]})
The other way is to create an interaction variable so you are only faceting on one variable and then you can change the labeller to strip out the name of the second value. That would look like this
mpg %>%
filter(manufacturer=='audi')%>%
ggplot(aes(cty, hwy))
geom_point(aes(col = model))
facet_wrap(~interaction(year,model), labeller=as_labeller(function(x) gsub("\\..*$", "", x)))