lbl [1] "0...">

How to use labeller in facet_wrap correctly

I have a chart below, and I would like the facet chart labels to be those in "lbl" =

> lbl
[1] "0% - 10%"  "10% - 20%"

How can I add a label to facet_wrap to get this text, and how does the etaller correctly handle the order that is derived from the labeller function? those. if I have 20 charts, how does the label correctly label the charts in the correct order? Thank.

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"))
+4
source share
1 answer

You just need to create a label; read ?labellerhere ?as_labellerfor reference. All you need to add is labeller = as_labeller(setNames(lbl, sort(unique(group))))(or a suitable vector constructed as 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"))

graph with corresponding face labels

+2

All Articles