【R】ggpattern
2020年10月26日
1. はじめに
ggplotで棒グラフ等の内部を塗りつぶすなど、geomのカスタマイズができるパッケージggpattern
があります。
2. インストール
remotes::install_github("coolbutuseless/ggpattern")
3. 使ってみる
つかってみました。
library(ggplot2) library(ggpattern) df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(1.3, 2.9, 2.2, 3.3)) ggplot(df) + geom_col_pattern( aes(level, outcome, pattern_fill = level), pattern = 'stripe', fill = 'blue', colour = 'black' ) + theme_bw(10) + theme(legend.position = 'none') + labs( title = "ggpattern::geom_pattern_col()", subtitle = "pattern = 'stripe'" ) + coord_fixed(ratio = 1/2)
こんなふうに国旗も書くことができます。
flags <- c( system.file("img", "flag", "au.png", package = "ggpattern"), system.file("img", "flag", "dk.png", package = "ggpattern"), system.file("img", "flag", "gb.png", package = "ggpattern"), system.file("img", "flag", "gr.png", package = "ggpattern"), system.file("img", "flag", "no.png", package = "ggpattern"), system.file("img", "flag", "se.png", package = "ggpattern"), system.file("img", "flag", "us.png", package = "ggpattern") ) p <- ggplot(mpg, aes(class)) + geom_bar_pattern( aes( pattern_filename = class ), pattern = 'image', pattern_type = 'none', fill = 'grey80', colour = 'black', pattern_scale = -2, pattern_filter = 'point', pattern_gravity = 'east' ) + theme_bw(18) + labs( title = "ggpattern::geom_bar_pattern() + coord_flip()", subtitle = "pattern = 'image'" ) + theme(legend.position = 'none') + scale_pattern_filename_discrete(choices = flags) + coord_flip() + scale_pattern_discrete(guide = guide_legend(nrow = 1)) p