use*_*059 1 r ggplot2 facet-grid
有没有一种方法可以使用ggplot的facet_grid在图表顶部的标签和图表的边距之间添加空间。以下是可重现的示例。
library(dplyr)
library(ggplot2)
Titanic %>% as.data.frame() %>%
filter(Survived == "Yes") %>%
mutate(FreqSurvived = ifelse(Freq > 100, Freq*1e+04,Freq)) %>%
ggplot( aes(x = Age, y = FreqSurvived, fill = Sex)) +
geom_bar(stat = "identity", position = "dodge") +
facet_grid(Class ~ ., scales = "free") +
theme_bw() +
geom_text(aes(label = prettyNum(FreqSurvived,big.mark = ",")), vjust = 0, position = position_dodge(0.9), size = 2)
Run Code Online (Sandbox Code Playgroud)
生成的图表在图的边框旁边有数字标签。
我想添加到@dww的答案中,但没有足够的声誉。
该expand选项实际上将允许您仅在图形顶部添加空间。从?expand_scale帮助文件:
# No space below the bars but 10% above them
ggplot(mtcars) +
geom_bar(aes(x = factor(cyl))) +
scale_y_continuous(expand = expand_scale(mult = c(0, .1)))
Run Code Online (Sandbox Code Playgroud)