我目前有一個構面圖,我希望將每個構面的標題更改為一個新名稱。有誰知道如何做到這一點?
#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 更改為“集群 1”,將 2 更改為“集群 2”,依此類推
uj5u.com熱心網友回復:
一種選擇是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))
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/462256.html
