I have the plot below and I would like the facet plot labels to be what is in "lbl" =
> lbl
[1] "0% - 10%" "10% - 20%"
How can labeller be added to the facet_wrap to get that text to show up and how does labeller correctly handle the ordering of that is output from the labeller function? i.e. If I have 20 plots how does labeller correctly label the plots in the right order? Thank you.
here is the code:
x = c( rep(c(1,2,3,4,5),4) )
group = c( rep(c(10,10,10,10,10),2),rep(c(20,20,20,20,20),2) )
lbl = paste0( c("0%", paste0( unique(group)[1:(length(unique(group))-1)] ,"%" ) )
," - ",
paste0(unique(group),"%"))
lbl
value = rnorm(20)
dat = data.frame( x= x , group = group, value = value)
dat = dat %>% # create the mu, sd, q1 and q3 aggregates
group_by(group,x) %>%
summarise(mu = round(mean(value),2),
sd= sqrt(round(sd(value),2)),
Q1 = quantile(value)[2],
Q3 = quantile(value)[4],
count = n())
dat
dat2 = dat %>% gather (key = Metric, value= Value,c(mu, sd, Q1, Q3)) #melt the data
as.data.frame(dat2)
ggplot(data=dat2 , aes(x=x, y=Value, group = Metric,colour = Metric,linetype = Metric)) +
geom_line() + geom_point() + ylab("value") +
xlab("v") +
scale_x_discrete(breaks = c( seq(1,5,1) ) ) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous(breaks = c( seq(-3,3,.25) ) ) +
scale_colour_manual(values=c(mu = "blue", sd = "blue", Q1 = "red", Q3 = "red")) +
scale_linetype_manual(values =c(mu = "dashed", sd= "solid", Q1 = "solid", Q3 = "solid")) +
facet_wrap(~ group, scales = "free",ncol=3) +
theme(strip.text.x = element_text(size=10, angle=0),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(colour="red", fill="#CCCCFF"))
Facet labels can be modified using the option labeller , which should be a function. In the following R code, facets are labelled by combining the name of the grouping variable with group levels. The labeller function label_both is used.
The facet_grid() function will produce a grid of plots for each combination of variables that you specify, even if some plots are empty. The facet_wrap() function will only produce plots for the combinations of variables that have values, which means it won't produce any empty plots.
You just need to build a labeller; read ?labeller
and here, ?as_labeller
for help. All you really need to add is labeller = as_labeller(setNames(lbl, sort(unique(group))))
(or a suitably named vector, constructed how you like) to facet_wrap
:
ggplot(data=dat2 , aes(x=x, y=Value, group = Metric,colour = Metric,linetype = Metric)) +
geom_line() + geom_point() + ylab("value") +
xlab("v") +
scale_x_discrete(breaks = c( seq(1,5,1) ) ) +
theme(axis.text.x = element_text(angle = 90, hjust = 1)) +
scale_y_continuous(breaks = c( seq(-3,3,.25) ) ) +
scale_colour_manual(values=c(mu = "blue", sd = "blue", Q1 = "red", Q3 = "red")) +
scale_linetype_manual(values =c(mu = "dashed", sd= "solid", Q1 = "solid", Q3 = "solid")) +
facet_wrap(~ group, scales = "free",ncol=3,
# add a labeller here:
labeller = as_labeller(setNames(lbl, sort(unique(group))))) +
theme(strip.text.x = element_text(size=10, angle=0),
strip.text.y = element_text(size=12, face="bold"),
strip.background = element_rect(colour="red", fill="#CCCCFF"))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With