knitr::opts_chunk$set( collapse = TRUE, comment = "#>", fig.width = 15/2.54, dpi = 96 ) # devtools::install_github("hadley/emo") library(emo) # 絵文字 library(pkgdown) # このpackageのwebsiteを手軽に作成
ggplot2
でファセットグラフを描いた際, このパネルだけズームしたい, と思うことがあります.frabento::coord_panel_ranges()
を作ってみました.library(frabento) # このパッケージ library(tidyverse) # ggplot2とtidyなデータハンドリング library(patchwork) # ggplotを簡単, キレイにレイアウト library(ragg) # マルチバイトフォントの利用を楽にするため library(ggtext) # element_markdown() を使うため library(ggh4x) # Awesome!! Hacks for ggplot2
データセットiris
(Edgar Anderson's Iris Data) を例に, ファセットグラフを
描いてみます.
theme_set(theme_linedraw(base_family = "Helvetica", base_line_size = 0.3) + theme(aspect.ratio = 1/1)) # base plot g <- iris %>% ggplot(aes(x = Sepal.Length, y = Sepal.Width)) + geom_point(aes(color = Species)) + geom_rect(data = . %>% dplyr::filter(Species == "versicolor"), xmin = 5.5, xmax = 6.5, ymin = 2.5, ymax = 3, color = "red", size = 1, fill = NA) + facet_wrap(~ Species, scale = "free", ncol = 2, dir = "h") + theme(legend.position = c(0.75, 0.25)) # zoom in versicolor panel (this is second panel) gz <- g + coord_panel_ranges(panel_ranges = list( list(NULL), list(x = c(5.4, 6.6), y = c(2.4, 3.1)), list(NULL) )) # patchwork g | gz
ggh4x
が別の解法を実装しています.g + ggh4x::facetted_pos_scales( x = list(Species == "versicolor" ~ scale_x_continuous(limits = c(5.5, 6.5), expand = expansion(add = c(0.1, 0.1)))), y = list(Species == "versicolor" ~ scale_y_continuous(limits = c(2.5, 3.0), expand = expansion(add = c(0.1, 0.1)))) )
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.