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


当前回答

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

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

其他回答

你试过改变医院载体的具体水平吗?

levels(survey$hospital)[levels(survey$hospital) == "Hospital #1"] <- "Hosp 1"
levels(survey$hospital)[levels(survey$hospital) == "Hospital #2"] <- "Hosp 2"
levels(survey$hospital)[levels(survey$hospital) == "Hospital #3"] <- "Hosp 3"

更改底层因子级别的名称,如下所示:

# Using the Iris data
> i <- iris
> levels(i$Species)
[1] "setosa"     "versicolor" "virginica" 
> levels(i$Species) <- c("S", "Ve", "Vi")
> ggplot(i, aes(Petal.Length)) + stat_bin() + facet_grid(Species ~ .)

添加另一个类似于@domi的解决方案,解析数学符号、上标、下标、圆括号/方括号等。

library(tidyverse)
theme_set(theme_bw(base_size = 18))

### create separate name vectors
# run `demo(plotmath)` for more examples of mathematical annotation in R
am_names <- c(
  `0` = "delta^{15}*N-NO[3]^-{}",
  `1` = "sqrt(x,y)"
)

# use `scriptstyle` to reduce the size of the parentheses &
# `bgroup` to make adding `)` possible 
cyl_names <- c(
  `4` = 'scriptstyle(bgroup("", a, ")"))~T~-~5*"%"',
  `6` = 'scriptstyle(bgroup("", b, ")"))~T~+~10~degree*C',
  `8` = 'scriptstyle(bgroup("", c, ")"))~T~+~30*"%"'
)

ggplot(mtcars, aes(wt, mpg)) + 
  geom_jitter() +
  facet_grid(am ~ cyl,
             labeller = labeller(am  = as_labeller(am_names,  label_parsed),
                                 cyl = as_labeller(cyl_names, label_parsed))
             ) +
  geom_text(x = 4, y = 25, size = 4, nudge_y = 1,
            parse = TRUE, check_overlap = TRUE,
            label = as.character(expression(paste("Log"["10"], bgroup("(", frac("x", "y"), ")")))))

### OR create new variables then assign labels directly
# reverse facet orders just for fun
mtcars <- mtcars %>% 
  mutate(am2  = factor(am,  labels = am_names),
         cyl2 = factor(cyl, labels = rev(cyl_names), levels = rev(attr(cyl_names, "names")))
  )

ggplot(mtcars, aes(wt, mpg)) + 
  geom_jitter() +
  facet_grid(am2 ~ cyl2,
             labeller = label_parsed) +
  annotate("text", x = 4, y = 30, size = 5,
           parse = TRUE, 
           label = as.character(expression(paste("speed [", m * s^{-1}, "]"))))

由reprex包于2019-03-30创建(v0.2.1.9000)

由于我还不被允许评论帖子,所以我单独发布了这篇文章,作为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向量可能需要以不同的方式重复,以匹配原始数据集的顺序。请再三检查这是否正确实现。

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

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)