Как настроить асимметрию расширения пределов в ggplot? Например,
library(ggplot2)
ggplot(mtcars) +
geom_bar(aes(x = cyl), width = 1)
Я хотел бы, чтобы дно баров заподлицо с нижней частью фона панели, но все равно хотелось бы место наверху. Я могу добиться этого с помощью пустой аннотации:
ggplot(mtcars) +
geom_bar(aes(x = cyl), width = 1) +
annotate("blank", x = 4, y = 16) +
scale_y_continuous(expand = c(0.0,0))
Однако в предыдущих версиях ggplot
я мог использовать решение, предоставленное Розеном Матевым:
library("scales")
scale_dimension.custom_expand <- function(scale, expand = ggplot2:::scale_expand(scale)) {
expand_range(ggplot2:::scale_limits(scale), expand[[1]], expand[[2]])
}
scale_y_continuous <- function(...) {
s <- ggplot2::scale_y_continuous(...)
class(s) <- c('custom_expand', class(s))
s
}
а затем используйте scale_y_continuous(expand = list(c(0,0.1), c(0,0)))
которые добавили бы последовательно добавление в верхнюю часть диаграммы. В текущей версии, однако, я получаю сообщение об ошибке
ggplot(mtcars) +
geom_bar(aes(x = cyl), width = 1) +
scale_y_continuous(expand = list(c(0,0.1), c(0,0)))
# Error in diff(range) * mul : non-numeric argument to binary operator
Есть ли эффективное решение для ggplot2 2.0?
Решение должно включать в себя возможность гибкой работы с free_xy
и free_xy
. Например,
ggplot(mtcars) +
geom_bar(aes(x = cyl, fill = factor(vs)), width = 1) +
facet_grid(vs ~ ., scales = "free_y")
Решение должно предоставить что-то вроде:
ggplot(mtcars) +
geom_bar(aes(x = cyl, fill = factor(vs)), width = 1) +
facet_grid(vs ~ ., scales = "free_y") +
scale_y_continuous(expand = c(0,0)) +
geom_blank(data = data.frame(cyl = c(5,5), y = c(12, 16), vs = c(1,0)), aes(x = cyl, y = y))