我使用了以下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帮助页面,但不知道如何操作。
发布于 2010-08-13 09:40:04
将基础因子级别名称更改为类似以下内容:
# 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 ~ .)
发布于 2016-01-15 20:26:10
这是另一个解决方案,它遵循@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))
希望这能有所帮助。
发布于 2017-03-21 04:13:43
下面是我如何使用2.2.1版的ggplot2在facet_grid(yfacet~xfacet)
上做到这一点的:
facet_grid(
yfacet~xfacet,
labeller = labeller(
yfacet = c(`0` = "an y label", `1` = "another y label"),
xfacet = c(`10` = "an x label", `20` = "another x label")
)
)
请注意,这并不包含对as_labeller()
的调用--这是我为之挣扎了一段时间的东西。
这种方法的灵感来自于帮助页面Coerce to labeller function上的最后一个示例。
https://stackoverflow.com/questions/3472980
复制相似问题