我在多個組上制作 ggplot2 折線圖,使用facet_grid.
dat <- data.frame(date = c(1:5, 1:5),
type = rep(c("A", "B"), each = 5),
value = c(4500, 4800, 4600, 4900, 4700,
1500, 1510, 1500, 1400, 1390)
)
library(ggplot2)
dat |>
ggplot(aes(date, value, group = type))
geom_line()
facet_wrap(~type)

我現在想重新調整 y 軸,使其在兩種情況下都從 0 開始,但它達到了該特定組的最大值。
我嘗試將scales =引數設定為free_y- 這修復了 y 比例的上部,但不幸的是它具有不從 0 開始的不良影響:
dat |>
ggplot(aes(date, value, group = type))
geom_line()
facet_wrap(~type, scales = "free_y")

任何想法如何解決這一問題?
使用reprex v2.0.2創建于 2022-10-25
uj5u.com熱心網友回復:
這可以通過使用以下方式將比例限制固定在下端來實作scale_y_continuous(limits = c(0, NA)):
dat <- data.frame(date = c(1:5, 1:5),
type = rep(c("A", "B"), each = 5),
value = c(4500, 4800, 4600, 4900, 4700,
1500, 1510, 1500, 1400, 1390)
)
library(ggplot2)
dat |>
ggplot(aes(date, value, group = type))
geom_line()
scale_y_continuous(limits = c(0, NA))
facet_wrap(~type, scales = "free_y")

轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523470.html
標籤:rggplot2刻面
上一篇:按每個子組計算比例(dplyr)
下一篇:如何簡單提取回歸曲線的具體值?
