[英]facet_wrap: How to add y axis to every individual graph when scales=“free_x”?
[英]How to increase the default y-axis limit in ggplot when scales in facet_wrap() are set to "free"
我試圖弄清楚當使用facet_wrap()
並且刻度是“免費的”時如何更改 ggplot 中 y 軸限制的全局默認設置。 我已經搜索了該網站,但找不到任何解決我的問題的方法。 我知道我可以單獨構建每個圖,然后使用cowplot::plot_grid()
或類似的東西組裝它們,但這對於 10 個子圖會很麻煩。
示例數據和繪圖:
library(tidyverse)
d <- tibble(
var = c(rep(LETTERS[1:2], each = 4)),
id = c(rep(1:4, 2)),
mean = c(23, 24, 21, 22, 154, 153, 152, 151),
sd = c(rep(c(2, 10), each = 4)),
diff = c(rep(letters[1:4], 2))
)
ggplot(d, aes(id, mean)) +
geom_point() +
geom_errorbar(aes(ymin = mean - sd,
ymax = mean + sd)) +
geom_text(aes(
x = id,
y = mean + sd,
vjust = -2,
label = diff
)) +
facet_wrap( ~ var, scales = "free_y")
我遇到的問題是使用vjust = -2
參數時字母超出范圍。 我知道在這個例子中我可以簡單地改變vjust
參數,但在我的實際數據集中,不幸的是這不會解決問題。 所以我的問題是,假設vjust = -2
是固定的,有沒有辦法在facet_wrap()
中的scales = "free"
時增加默認的 y 軸限制。
一個選項是擴展軸限制。
ggplot(d, aes(id, mean)) +
geom_point() +
geom_errorbar(aes(ymin = mean - sd,
ymax = mean + sd)) +
geom_text(aes(
x = id,
y = mean + sd,
vjust = -2,
label = diff
)) +
facet_wrap( ~ var, scales = "free_y") +
scale_y_continuous(expand = expansion(mult = c(0.1, 0.2)))
這將頂部范圍增加 20%,底部增加 10%。 AFAIK, 默認mult = c(0.05, 0)
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.