Open JK-junkin opened 3 years ago
上記の参考サイト (https://andburch.github.io/ggplot_facets/) にあるコードのままではエラーが出たので、私はエラーを起こした箇所をコメントアウトして使っています。下記に載せておきます。
UniquePanelCoords <- ggplot2::ggproto(
"UniquePanelCoords", ggplot2::CoordCartesian,
num_of_panels = 1,
panel_counter = 1,
panel_ranges = NULL,
setup_layout = function(self, layout, params) {
self$num_of_panels <- length(unique(layout$PANEL))
self$panel_counter <- 1
layout
},
setup_panel_params = function(self, scale_x, scale_y, params = list()) {
if (!is.null(self$panel_ranges) & length(self$panel_ranges) != self$num_of_panels)
stop("Number of panel ranges does not equal the number supplied")
train_cartesian <- function(scale, limits, name, given_range = NULL) {
if (is.null(given_range)) {
# range <- ggplot2:::scale_range(scale, limits, self$expand)
expansion <- ggplot2:::default_expansion(scale, expand = self$expand)
range <- ggplot2:::expand_limits_scale(scale, expansion,
coord_limits = self$limits[[name]])
} else {
range <- given_range
}
# out <- scale$break_info(range)
# out$arrange <- scale$axis_order()
# names(out) <- paste(name, names(out), sep = ".")
# out
out <- list(
ggplot2:::view_scale_primary(scale, limits, range),
sec = ggplot2:::view_scale_secondary(scale, limits, range),
arrange = scale$axis_order(),
range = range
)
names(out) <- c(name, paste0(name, ".", names(out)[-1]))
out
}
cur_panel_ranges <- self$panel_ranges[[self$panel_counter]]
if (self$panel_counter < self$num_of_panels)
self$panel_counter <- self$panel_counter + 1
else
self$panel_counter <- 1
c(train_cartesian(scale_x, self$limits$x, "x", cur_panel_ranges$x),
train_cartesian(scale_y, self$limits$y, "y", cur_panel_ranges$y))
}
)
coord_panel_ranges <- function(panel_ranges, expand = TRUE,
default = FALSE, clip = "on") {
ggplot2::ggproto(NULL, UniquePanelCoords, panel_ranges = panel_ranges,
expand = expand, default = default, clip = clip)
}
この関数を適当なスクリプトファイル (たとえばcoord_facet.r
) にして、
source("coord_facet.r", encoding = "UTF-8")
と読み込んで使っています。
宣伝になって恐縮ですが、上記関数を拙パッケージにて実装しています。
https://jk-junkin.github.io/frabento/articles/coord_panel_ranges.html
devtools::install_github("JK-junkin/frabento")
でインストールできます。
また、ggh4x
パッケージ(CRANにもアップされている)の関数でもできます。私はこちらも使います。
https://teunbrand.github.io/ggh4x/reference/facetted_pos_scales.html
https://teunbrand.github.io/ggh4x/reference/scale_facet.html
ggh4x
パッケージは他にも作図に便利な関数が多いので、拙パッケージよりもオススメです。
この方法(facet形式の図ごとに任意の縦軸の上限・下限を与える)をずっと探してました!ありがとうございます.
問題 frasyrに限らず、
ggplot2
:package:のfacet_wrap()
等を使って図を描いた後で、任意のパネル (= facet) の縦軸表示範囲を調整したいことがあります。例えばwikiのここで、fishing_mortalityパネルの低い値にズームインしたい場合などです。対応案 このサイトを参考にして、
coord_panel_ranges()
関数を自作して、frasyrの出力結果に下記のように追記すると調整できました。