我使用了以下ggplot命令:
ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ..count.. / n), binwidth = 10)
+ scale_y_continuous(formatter = "percent", breaks = c(0, 0.1, 0.2))
+ facet_grid(hospital ~ .)
+ theme(panel.background = theme_blank())
生产
然而,我想将facet标签更改为更短的内容(如Hosp 1, Hosp 2…),因为它们现在太长了,看起来很局促(增加图形的高度不是一个选项,它将占用文档中的太多空间)。我查看了facet_grid帮助页面,但不知道如何操作。
注意,这个解决方案不会很好地工作,如果ggplot显示的因子比你的变量实际包含的少(如果你已经进行了子集设置,这可能会发生):
library(ggplot2)
labeli <- function(variable, value){
names_li <- list("versicolor"="versi", "virginica"="virg")
return(names_li[value])
}
dat <- subset(iris,Species!="setosa")
ggplot(dat, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ ., labeller=labeli)
一个简单的解决方案(除了在names_li中添加所有未使用的因子,这可能很乏味)是使用droplevels()删除未使用的因子,要么在原始数据集中,要么在labbeler函数中,参见:
labeli2 <- function(variable, value){
value <- droplevels(value)
names_li <- list("versicolor"="versi", "virginica"="virg")
return(names_li[value])
}
dat <- subset(iris,Species!="setosa")
ggplot(dat, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ ., labeller=labeli2)
我现在解决这个问题的方法是使用dplyr::case_when在facet_grid或facet_wrap函数中生成一个标签器。这是@lillemets提出的解决方案的扩展
ggplot(survey, aes(x = age)) + stat_bin(aes(n = nrow(h3), y = ..count.. / n), binwidth = 10)
+ scale_y_continuous(formatter = "percent", breaks = c(0, 0.1, 0.2))
+ facet_grid(case_when(hospital == "Hospital #1" ~ "Hosp1",
hospital == "Hospital #2" ~ "Hosp2") ~ .)
+ theme(panel.background = theme_blank())
如果您有第二个facet标签要更改,那么只需在facet_grid中的~的另一侧使用相同的方法即可
如果你有两个面医院和房间,但只想重命名其中一个,你可以使用:
facet_grid( hospital ~ room, labeller = labeller(hospital = as_labeller(hospital_names)))
使用基于矢量的方法重命名两个面(如naught101的答案),你可以这样做:
facet_grid( hospital ~ room, labeller = labeller(hospital = as_labeller(hospital_names),
room = as_labeller(room_names)))
这个解决方案非常接近于@domi,但是它通过获取前4个字母和最后一个数字来缩短名称。
library(ggplot2)
# simulate some data
xy <- data.frame(hospital = rep(paste("Hospital #", 1:3, sep = ""), each = 30),
value = rnorm(90))
shortener <- function(string) {
abb <- substr(string, start = 1, stop = 4) # fetch only first 4 strings
num <- gsub("^.*(\\d{1})$", "\\1", string) # using regular expression, fetch last number
out <- paste(abb, num) # put everything together
out
}
ggplot(xy, aes(x = value)) +
theme_bw() +
geom_histogram() +
facet_grid(hospital ~ ., labeller = labeller(hospital = shortener))
在不修改底层数据的情况下进行更改的最简单方法是:
使用as_labeller()创建一个对象。如果列名以数字开头,或者包含空格或特殊字符,不要忘记使用反标记:
# Necessary to put RH% into the facet labels
hum_names <- as_labeller(
c(`50` = "RH% 50", `60` = "RH% 60",`70` = "RH% 70",
`80` = "RH% 80",`90` = "RH% 90", `100` = "RH% 100"))
在ggplot中添加:
ggplot(dataframe, aes(x = Temperature.C, y = fit)) +
geom_line() +
facet_wrap(~Humidity.RH., nrow = 2, labeller = hum_names)
下面是另一个解决方案,它遵循@naught101给出的解决方案的精神,但更简单,也没有在ggplot2的最新版本上抛出警告。
基本上,首先创建一个命名字符向量
hospital_names <- c(
`Hospital#1` = "Some Hospital",
`Hospital#2` = "Another Hospital",
`Hospital#3` = "Hospital Number 3",
`Hospital#4` = "The Other Hospital"
)
然后将它用作标签器,只需修改@naught101给出的最后一行代码
... + facet_grid(hospital ~ ., labeller = as_labeller(hospital_names))