我使用了以下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帮助页面,但不知道如何操作。


当前回答

这个解决方案非常接近于@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))

其他回答

由于我还不被允许评论帖子,所以我单独发布了这篇文章,作为Vince和son520804的答案的补充。功劳归于他们。

Son520804: 使用虹膜数据: 我认为: 您已经安装了dplyr包,其中有方便的mutate命令 您的数据集名为survey。 调查%>%突变(Hosp1 = Hospital1, Hosp2 = Hospital2,........) 此命令帮助您重命名列,但保留所有其他列。 然后执行同样的facet_wrap,现在就没问题了。

使用Vince的虹膜示例和son520804的部分代码,我使用mutate函数做到了这一点,并在不接触原始数据集的情况下实现了一个简单的解决方案。 诀窍是创建一个替代名称向量,并在管道中使用mutate()临时更正facet名称:

i <- iris

levels(i$Species)
[1] "setosa"     "versicolor" "virginica"

new_names <- c(
  rep("Bristle-pointed iris", 50), 
  rep("Poison flag iris",50), 
  rep("Virginia iris", 50))

i %>% mutate(Species=new_names) %>% 
ggplot(aes(Petal.Length))+
    stat_bin()+
    facet_grid(Species ~ .)

在这个例子中,你可以看到i$Species的级别被临时更改为包含在new_names向量中的对应的公共名称。包含

mutate(Species=new_names) %>%

可以很容易地去掉,露出原来的命名。

警告:如果new_name向量没有正确设置,这可能很容易在名称中引入错误。使用一个单独的函数来替换变量字符串可能会更简洁。请记住,new_name向量可能需要以不同的方式重复,以匹配原始数据集的顺序。请再三检查这是否正确实现。

用变量,值作为参数的标签函数定义不适合我。另外,如果你想使用表达式,你需要使用lapply,而不能简单地使用arr[val],因为函数的参数是data.frame。

这段代码确实有效:

libary(latex2exp)
library(ggplot2)
arr <- list('virginica'=TeX("x_1"), "versicolor"=TeX("x_2"), "setosa"=TeX("x_3"))
mylabel <- function(val) { return(lapply(val, function(x) arr[x])) }
ggplot(iris, aes(x=Sepal.Length, y=Sepal.Width)) + geom_line() + facet_wrap(~Species, labeller=mylabel)

这里有一个避免编辑数据的解决方案:

假设你的图是由你的数据帧的组部分组成的,它有级别控制,test1, test2,然后创建一个由这些值命名的列表:

hospital_names <- list(
  'Hospital#1'="Some Hospital",
  'Hospital#2'="Another Hospital",
  'Hospital#3'="Hospital Number 3",
  'Hospital#4'="The Other Hospital"
)

然后创建一个'labeller'函数,并将其推到facet_grid调用中:

hospital_labeller <- function(variable,value){
  return(hospital_names[value])
}

ggplot(survey,aes(x=age)) + stat_bin(aes(n=nrow(h3),y=..count../n), binwidth=10)
 + facet_grid(hospital ~ ., labeller=hospital_labeller)
 ...

它使用数据帧的级别来索引hospital_names列表,并返回列表值(正确的名称)。


请注意,这仅适用于只有一个facet变量的情况。如果您有两个facet,那么您的labeller函数需要为每个facet返回不同的name向量。你可以这样做:

plot_labeller <- function(variable,value){
  if (variable=='facet1') {
    return(facet1_names[value])
  } else {
    return(facet2_names[value])
  }
}

其中facet1_names和facet2_names是由facet索引名('Hostpital#1'等)索引的名称的预定义列表。


编辑:如果你传递的变量/值的组合标签不知道,上述方法失败。你可以像这样为未知变量添加故障保险:

plot_labeller <- function(variable,value){
  if (variable=='facet1') {
    return(facet1_names[value])
  } else if (variable=='facet2') {
    return(facet2_names[value])
  } else {
    return(as.character(value))
  }
}

答自如何改条。ggplot中带有facet和margin=TRUE的文本标签


警告:如果您使用此方法对字符列进行facet,您可能会得到不正确的标签。请参阅此错误报告。在ggplot2的最新版本中修正。

如果你有两个面医院和房间,但只想重命名其中一个,你可以使用:

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

简单的解决方案(从这里):

p <- ggplot(mtcars, aes(disp, drat)) + geom_point()
# Example (old labels)
p + facet_wrap(~am)


to_string <- as_labeller(c(`0` = "Zero", `1` = "One"))
# Example (New labels)
p + facet_wrap(~am, labeller = to_string)