ggplot2强大的图形可视化能力使得R语言成为科研绘图的佼佼者,因此也衍生出了一系列辅助包,在ggplot2绘图的基础上进行补充、完善、美化。今天为大家带来的ggpattern就是一款十分实用、易上手且趣味性十足的辅助包,ggplot2的输出的每一种geom_几何对象都能在ggpattern里找到对应的geom_pattern进行填充,并且函数及参数对应度很高,用法很相似,除了内嵌的固定用法以外,用户还可以根据自己的喜好自定义,相当人性化,小伙伴们不要错过哦。

ggpattern——ggplot2的好帮手_hashmap

#安装并加载,可在cran直接获得
install.packages('ggplot2')
install.packages('ggpattern') 
library(ggplot2)
library(ggpattern)
#填充样式
df <- data.frame(level = c("a", "b", "c", 'd'), outcome = c(2.3, 1.9, 3.2, 1))
ggplot(df, aes(level, outcome)) +
  geom_col_pattern(
    aes(pattern = level, pattern_angle = level, pattern_spacing = level), 
    fill            = 'white',
    colour          = 'black', 
    pattern_density = 0.35, 
    pattern_fill    = 'black',
    pattern_colour  = 'black'
  ) +
  theme_bw() +
  labs(
    title    = "ggpattern::geom_col_pattern()",
    subtitle = 'geometry-based patterns'
  ) +
  scale_pattern_spacing_discrete(range = c(0.01, 0.05)) + 
  theme(legend.position = 'none') + 
  coord_fixed(ratio = 1)

ggpattern——ggplot2的好帮手_可视化_02

#调整颜色
ggplot(df, aes(level, outcome)) +
  geom_col_pattern(
    aes(pattern = level, fill = level, pattern_fill = level), 
    colour                   = 'black', 
    pattern_density          = 0.35, 
    pattern_key_scale_factor = 1.3) +
  theme_bw() +
  labs(
    title    = "ggpattern::geom_col_pattern()",
    subtitle = 'geometry-based patterns'
  ) +
  scale_pattern_fill_manual(values = c(a='blue', b='red', c='yellow', d='darkgreen')) + 
  theme(legend.position = 'none') + 
  coord_fixed(ratio = 1)

ggpattern——ggplot2的好帮手_hashmap_03

接下来展示一下常用图形的填充方法。

#geom_bar_pattern()
ggplot(mpg, aes(class)) +
  geom_bar_pattern(
    aes(
      pattern = class, 
      pattern_angle = class
    ), 
    fill            = 'white', 
    colour          = 'black',
    pattern_spacing = 0.025
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_bar_pattern()") + 
  theme(legend.position = 'none') +
  coord_fixed(ratio = 1/15) + 
  scale_pattern_discrete(guide = guide_legend(nrow = 1))

ggpattern——ggplot2的好帮手_可视化_04

#pie graph
df <- data.frame(
  group = factor(c("Cool", "But", "Use", "Less"), levels = c("Cool", "But", "Use", "Less")),
  value = c(10, 20, 30, 40)
)
ggplot(df, aes(x="", y = value, pattern = group, pattern_angle = group))+
  geom_bar_pattern(
    width                = 1, 
    stat                 = "identity", 
    fill                 = 'white', 
    colour               = 'black',
    pattern_aspect_ratio = 1, 
    pattern_density      = 0.3
  ) +
  coord_polar("y", start=0) + 
  theme_void(20) + 
  theme(
    legend.key.size = unit(2, 'cm')
  ) + 
  labs(title = "ggpattern::geom_bar_pattern() + coord_polar()")

ggpattern——ggplot2的好帮手_ggplot2_05

#geom_bin2d_pattern()
ggplot(diamonds, aes(x, y)) + 
  xlim(4, 10) + ylim(4, 10) +
  geom_bin2d_pattern(aes(pattern_spacing = ..density..), fill = 'white', bins = 6, colour = 'black', size = 1) +
  theme_bw(18) +
  theme(legend.position = 'none') + 
  labs(title = "ggpattern::geom_bin2d_pattern()")

ggpattern——ggplot2的好帮手_hashmap_06

#geom_boxplot_pattern()
ggplot(mpg, aes(class, hwy)) +
  geom_boxplot_pattern(
    aes(
      pattern      = class, 
      pattern_fill = class
    ), 
    pattern_spacing = 0.03
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_boxplot_pattern()") + 
  theme(legend.position = 'none') + 
  coord_fixed(1/8)

ggpattern——ggplot2的好帮手_可视化_07

#geom_col_pattern()
df <- data.frame(trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2))
ggplot(df, aes(trt, outcome)) +
  geom_col_pattern(
    aes(
      pattern = trt, 
      fill    = trt
    ), 
    colour                   = 'black', 
    pattern_density          = 0.5, 
    pattern_key_scale_factor = 1.11
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_col_pattern()") + 
  theme(legend.position = 'none') +  
  coord_fixed(ratio = 1/2)

ggpattern——ggplot2的好帮手_可视化_08

#geom_crossbar_pattern()
df <- data.frame(
  trt = factor(c(1, 1, 2, 2)),
  resp = c(1, 5, 3, 4),
  group = factor(c(1, 2, 1, 2)),
  upper = c(1.1, 5.3, 3.3, 4.2),
  lower = c(0.8, 4.6, 2.4, 3.6)
)
ggplot(df, aes(trt, resp, colour = group)) +
  geom_crossbar_pattern(
    aes(
      ymin          = lower, 
      ymax          = upper, 
      pattern_angle = trt, 
      pattern       = group
    ), width        = 0.2, 
    pattern_spacing = 0.02
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_crossbar_pattern()") + 
  theme(legend.position = 'none') + 
  coord_fixed(ratio = 1/3)

ggpattern——ggplot2的好帮手_hashmap_09

#geom_density_pattern()
ggplot(mtcars) +
  geom_density_pattern(
    aes(
      x            = mpg, 
      pattern_fill = as.factor(cyl), 
      pattern      = as.factor(cyl)
    ), 
    fill                     = 'white', 
    pattern_key_scale_factor = 1.2,
    pattern_density          = 0.4
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_density_pattern()") + 
  theme(legend.key.size = unit(2, 'cm')) +
  coord_fixed(ratio = 100)

ggpattern——ggplot2的好帮手_可视化_10

.

#geom_map_pattern()
crimes <- data.frame(state = tolower(rownames(USArrests)), USArrests)
states_map <- map_data("state")
ggplot(crimes, aes(map_id = state)) +
  geom_map_pattern(
    aes(
      # fill            = Murder,
      pattern_fill    = Murder,
      pattern_spacing = state,
      pattern_density = state,
      pattern_angle   = state,
      pattern         = state
    ),
    fill   = 'white',
    colour = 'black',
    pattern_aspect_ratio = 1.8,
    map    = states_map
  ) +
  expand_limits(x = states_map$long, y = states_map$lat) +
  coord_map() +
  theme_bw(18) +
  labs(title = "ggpattern::geom_map_pattern()") + 
  scale_pattern_density_discrete(range = c(0.01, 0.3)) + 
  scale_pattern_spacing_discrete(range = c(0.01, 0.03)) + 
  theme(legend.position = 'none')

ggpattern——ggplot2的好帮手_ggplot2_11

#geom_polygon_pattern()
angle <- seq(0, 2*pi, length.out = 7) + pi/6
polygon_df <- data.frame(
  angle = angle,
  x     = cos(angle),
  y     = sin(angle)
)
ggplot(polygon_df) +
  geom_polygon_pattern(
    aes(x = x, y = y), 
    fill            = 'white', 
    colour          = 'black', 
    pattern_spacing = 0.15, 
    pattern_density = 0.4, 
    pattern_fill    = 'lightblue', 
    pattern_colour  = '#002366',
    pattern_angle   = 45
  ) + 
  labs(title = "ggpattern") + 
  coord_equal() + 
  theme_bw(25) + 
  theme(axis.title = element_blank())

ggpattern——ggplot2的好帮手_bi_12

#geom_rect_pattern()
plot_df <- data.frame(
  xmin    = c(0, 10),
  xmax    = c(8, 18),
  ymin    = c(0, 10),
  ymax    = c(5, 19),
  type    = c('a', 'b'),
  angle   = c(45, 0),
  pname   = c('circle', 'circle'),
  pcolour = c('red', 'blue'),
  pspace  = c(0.03, 0.05),
  size    = c(0.5, 1),
  stringsAsFactors = FALSE
)
ggplot(plot_df) +
  geom_rect_pattern(
    aes(
      xmin=xmin, ymin=ymin, xmax=xmax, ymax=ymax,
      pattern_angle   = I(angle),
      pattern_colour  = I(pcolour),
      pattern_spacing = I(pspace),
      pattern_size    = I(size)
    ),
    pattern         = 'circle',
    fill            = 'white',
    colour          = 'black',
    pattern_density = 0.3
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_rect_pattern()") + 
  theme(legend.key.size = unit(1.5, 'cm'))

ggpattern——ggplot2的好帮手_可视化_13

#geom_ribbon_pattern()
huron <- data.frame(year = 1875:1972, level = as.vector(LakeHuron))
ggplot(huron, aes(year)) +
  geom_ribbon_pattern(
    aes(
      ymin = level - 1, 
      ymax = level + 1
    ), 
    fill            = NA, 
    colour          = 'black',
    pattern         = 'circle',
    pattern_spacing = 0.03, 
    pattern_density = 0.5,
    pattern_angle   = 30,
    outline.type    = 'legacy'
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_ribbon_pattern()")

ggpattern——ggplot2的好帮手_wxpython_14

#geom_sf_pattern()
nc <- sf::st_read(system.file("shape/nc.shp", package = "sf"), quiet = TRUE)
nc <- nc  %>% filter(between(CNTY_ID, 1820, 1830))
ggplot(nc) +
  geom_sf_pattern(
    aes(
      pattern = NAME, 
      fill    = NAME
    ),
    pattern_aspect_ratio = 2.8
  ) +
  theme_bw(15) + 
  theme(legend.key.size = unit(1.5, 'cm')) +
  labs(title = "ggpattern::geom_sf_pattern()")

ggpattern——ggplot2的好帮手_hashmap_15

#geom_tile_pattern()
df <- data.frame(
  x = rep(c(2, 5, 7, 9, 12), 2),
  y = rep(c(1, 2), each = 5),
  z = factor(rep(1:5, each = 2)),
  w = rep(diff(c(0, 4, 6, 8, 10, 14)), 2)
)
ggplot(df, aes(x, y)) +
  geom_tile_pattern(
    aes(
      fill    = z, 
      pattern = z
    ), 
    colour = "grey50"
  ) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_tile_pattern()") + 
  theme(
    legend.position = 'bottom',
    legend.key.size = unit(1.5, 'cm')
  ) + 
  coord_fixed(ratio = 4)

ggpattern——ggplot2的好帮手_hashmap_16

#geom_violin_pattern()
ggplot(mtcars, aes(as.factor(cyl), mpg)) +
  geom_violin_pattern(aes(pattern = as.factor(cyl))) +
  theme_bw(18) +
  labs(title = "ggpattern::geom_violin_pattern()") + 
  theme(
    legend.key.size  = unit(2, 'cm')
  ) + 
  coord_fixed(1/15)

ggpattern——ggplot2的好帮手_ggplot2_17

----------------------------分界线----------------------------------

前几期我们的Small Dragon小伙伴分享了一个绘制动态图的包gganimate,本篇推文也接近尾声了,再给大家展示一下ggpattern和gganimate的结合,让你的个性化填充动起来!

library(ggpattern)
library(gganimate)
df1 <- data.frame(time = 1, offset = 0    , trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2), stringsAsFactors = FALSE)
df2 <- data.frame(time = 2, offset = 0.045, trt = c("a", "b", "c"), outcome = c(2.3, 1.9, 3.2), stringsAsFactors = FALSE)
df  <- rbind(df1, df2)
p <- ggplot(df, aes(trt, outcome)) +
    geom_col_pattern(
      aes(
        pattern_fill    = trt, 
        pattern_xoffset = I(offset), 
        pattern_yoffset = I(-offset)
      ), 
      colour          = 'black', 
      fill            = 'white',
      pattern_density = 0.5,
      pattern_angle   = 45
    ) +
    theme_bw() +
    labs(title = "ggpattern + gganimate") + 
    theme(legend.position = 'none') + 
    coord_fixed(ratio = 1/2) 
p <- p + transition_states(time, transition_length = 2,
                             state_length = 0, wrap = FALSE)

ggpattern——ggplot2的好帮手_bi_18

除此之外,还有很多实用和有趣的函数与功能没有展示出来,小伙伴们感兴趣的话可以阅读原文献并且自己研究一下,会很受裨益。