更改构面图上的标签

Changing labels on a facet plot

我目前有一个分面图,我希望将每个分面的标题更改为新名称。有人知道怎么做吗?

#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'))

所以我要将 1 更改为 'cluster 1',将 2 更改为 'cluster 2',依此类推

一个选项是添加 as_labeller 作为 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'))

或者我们可以使 cluster 成为一个因素并定义标签。然后,我们可以使用 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'))

输出

数据

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))