Home > other >  Changing labels on a facet plot
Changing labels on a facet plot

Time:04-23

I currently have a facet plot where i wish to change the heading of each facet to a new name. Does anyone know how to do this?

#data snipet
 Substation       Date cluster  Time     Value  weekday 
1     511016 2013-01-17       1 00:00 0.6215941 Thursday       
2     511029 2013-01-17       1 00:00 0.5677445 Thursday       
3     511030 2013-01-17       1 00:00 0.6065458 Thursday       
4     511033 2013-01-08       2 00:00 0.3090885  Tuesday       
5     511034 2013-01-17       1 00:00 0.5263230 Thursday       
6     511035 2013-01-17       1 00:00 0.5267718 Thursday       



#current code

ggplot(substation_average_long, aes(Time, Value, group = Substation, colour = cluster))  
  geom_line(colour = 'grey')  
  facet_wrap(.~cluster)  
  ylab('Substation power output')  
  ggtitle('Substation power output by cluster')  
  theme(plot.title = element_text(hjust = 0.5, face = 'bold'))

enter image description here

So i with to change 1 to 'cluster 1', 2 to 'cluster 2' and so on

CodePudding user response:

One option is to add in as_labeller as an argument in facet_wrap.

library(tidyverse)

ggplot(df, aes(Time, Value, group = Substation, colour = cluster))  
  geom_line(colour = 'grey')  
  facet_wrap(.~cluster, 
             labeller = as_labeller(c(`1` = "Cluster 1", `2` = "Cluster 2",
                                      `3` = "Cluster 3", `4` = "Cluster 4")))  
  ylab('Substation power output')  
  ggtitle('Substation power output by cluster')  
  theme(plot.title = element_text(hjust = 0.5, face = 'bold'))

Or we can make cluster a factor and define the labels. Then, we can use labeller = label_parsed.

df %>%
    mutate(cluster = factor(cluster, labels = paste0("`", unique(
      paste("Cluster", df$cluster)), "`"))) %>%
    ggplot(aes(Time, Value, group = Substation, colour = cluster))  
    geom_line(colour = 'grey')  
    facet_wrap(. ~ cluster,
               labeller = label_parsed)  
    ylab('Substation power output')  
    ggtitle('Substation power output by cluster')  
    theme(plot.title = element_text(hjust = 0.5, face = 'bold'))

Output

enter image description here

Data

df <- structure(list(Substation = c(511016L, 511029L, 511030L, 511033L, 
511034L, 511035L, 511016L, 511029L, 511030L, 511033L, 511034L, 
511035L, 511016L, 511029L, 511030L, 511033L, 511034L, 511035L
), Date = c("2013-01-17", "2013-01-17", "2013-01-17", "2013-01-08", 
"2013-01-17", "2013-01-17", "2013-01-17", "2013-01-17", "2013-01-17", 
"2013-01-08", "2013-01-17", "2013-01-17", "2013-01-17", "2013-01-17", 
"2013-01-17", "2013-01-08", "2013-01-17", "2013-01-17"), cluster = c(1L, 
1L, 1L, 2L, 1L, 1L, 1L, 1L, 1L, 2L, 1L, 3L, 3L, 3L, 3L, 4L, 4L, 
4L), Time = c("00:00", "00:00", "00:00", "00:00", "00:00", "01:00", 
"01:00", "01:00", "01:00", "01:00", "02:00", "00:00", "01:00", 
"02:00", "03:00", "01:00", "02:00", "03:00"), Value = c(0.6215941, 
0.5677445, 0.6065458, 0.3090885, 0.526323, 0.5267718, 0.6215941, 
0.5677445, 0.6065458, 0.3090885, 0.526323, 0.5267718, 0.6215941, 
0.5677445, 0.6065458, 0.3090885, 0.526323, 0.5267718), weekday = c("Thursday", 
"Thursday", "Thursday", "Tuesday", "Thursday", "Thursday", "Thursday", 
"Thursday", "Thursday", "Tuesday", "Thursday", "Thursday", "Thursday", 
"Thursday", "Thursday", "Tuesday", "Thursday", "Thursday")), class = "data.frame", row.names = c(NA, 
-18L))
  • Related