美文网首页
R可视化——ggblanket包介绍

R可视化——ggblanket包介绍

作者: 科研那点事儿 | 来源:发表于2022-08-21 09:08 被阅读0次

    今天,小编给大家介绍一款简单好用的作图R包——ggblanket,这是一个将ggplot2作图代码进行简化的作图R包,比如这个包将原先ggplot2包中的ggplot()和geom_* ()两部分合并为gg_*,具体功能大家可以看原网页介绍:


image.png

看一下这个包绘制的一些图片的展示:


image.png

安装ggblanket包

###安装方式——两种
#直接安装
install.packages("ggblanket")
#通过devtools包安装
install.packages("devtools")
devtools::install_github("davidhodge931/ggblanket")

具体用法

1、加载包
#加载包
library(dplyr)
library(ggplot2)
library(ggblanket)
library(palmerpenguins)
2、{ggblanket}使用gg_* 函数包装ggplot2::geom_* 函数,gg_* 函数包含了ggplot::geom_*中的参数:
iris %>%
  mutate(Species = stringr::str_to_sentence(Species)) %>% 
  gg_point(
    x = Sepal.Width, 
    y = Sepal.Length, 
    col = Species)
image.png
3、{ggblanket}将ggplot2包中的col和fill参数合并到一个col参数中,通过col参数控制图形颜色填充:
penguins %>% 
  gg_histogram(
    x = body_mass_g, 
    col = species)
image.png
4、{ggblanket}通过pal和alpha参数实现自定义颜色和其透明度:
penguins %>% 
  mutate(sex = stringr::str_to_sentence(sex)) %>% 
  group_by(species, sex) %>% 
  summarise(body_mass_g = mean(body_mass_g, na.rm = TRUE)) %>% 
  gg_col(
    x = species, 
    y = body_mass_g, 
    col = sex, 
    position = position_dodge2(preserve = "single"),
    pal = c("#1B9E77", "#9E361B"), #自定义颜色
    alpha = 0.6) #透明度
image.png
5、分面:{ggblanket}通过单个变量向facet提供一个facet参数(相当于ggplot2包的facet_wrap参数):
penguins %>% 
  tidyr::drop_na(sex) %>%
  mutate(sex = stringr::str_to_sentence(sex)) %>% 
  gg_violin(
    x = sex, 
    y = body_mass_g, 
    facet = species, #分面
    y_include = 0, 
    y_breaks = scales::breaks_width(1000),
    pal = "#1B9E77")
image.png

{ggblanket}还提供了facet2参数(相当于ggplot2::facet_grid),实现网格分面:

penguins %>% 
  tidyr::drop_na(sex) %>% 
  mutate(sex = stringr::str_to_sentence(sex)) %>% 
  gg_point(
    x = bill_length_mm, 
    y = body_mass_g,
    col = sex,
    facet = species,
    facet2 = sex, 
    y_breaks = scales::breaks_width(1500), 
    size = 1)
image.png
6、{ggblanket}为了配合Rstudio自动补全参数的功能,也提供输入参数前缀以查找其完整参数,如键入x_、y_、col_或facet_后,按tab键就可以看到以对应关键词为前缀的参数,常用参数有:
image.png
penguins %>%
  gg_jitter(
    x = species,
    y = body_mass_g,
    col = flipper_length_mm,
    position = ggplot2::position_jitter(width = 0.2, height = 0, seed = 123), 
    col_intervals = ~ santoku::chop_quantiles(.x, probs = seq(0, 1, 0.25)),
    col_legend_place = "r",
    y_include = 0,
    y_breaks = scales::breaks_width(1500), 
    y_labels = scales::label_number()
  )
image.png
7、{ggblanket}中如果x和y轴为数字/日期时,x轴刻度默认会从0.25位置开始以使图形更美观:
storms %>%
  group_by(year) %>%
  filter(between(year, 1980, 2020)) %>%
  summarise(wind = mean(wind, na.rm = TRUE)) %>%
  gg_line(
    x = year,
    y = wind,
    x_labels = ~.x,
    y_include = 0,
    title = "Storm wind speed",
    subtitle = "USA average storm wind speed, 1980\u20132020",
    y_title = "Wind speed (knots)",
    caption = "Source: NOAA"
  ) +
  geom_point()
image.png
8、主题设置:通过theme参数控制除图例位置及方向外的所有主题设置,图例的位置与方向需要单独通过col_legend_place参数控制:
penguins %>%
  mutate(sex = stringr::str_to_sentence(sex)) %>% 
  gg_point(x = bill_depth_mm,
           y = bill_length_mm,
           col = sex,
           facet = species, 
           pal = c("#1B9E77", "#9E361B"), 
           theme = theme_bw(),#主题设置
           col_legend_place = 'r')#图例位置设置
image.png
9、自定义主题:通过gg_theme参数实现:
storms %>%
  group_by(year) %>%
  filter(between(year, 1980, 2020)) %>%
  summarise(wind = mean(wind, na.rm = TRUE)) %>%
  gg_col(
    x = year,
    y = wind,
    x_labels = ~.x,
    x_expand = c(0, 0),
    theme = gg_theme(
      bg_plot_pal = "red",#图片整体背景色
      bg_panel_pal = "green",#图形主体背景色
      grid_h = T,#横向网格线
      grid_v = F))#竖直网格线
image.png
10、当绘图为水平方向时,ggblanket包保证y标签和颜色的顺序正确:
penguins %>%
  tidyr::drop_na(sex) %>% 
  group_by(species, sex, island) %>%
  summarise(body_mass_kg = mean(body_mass_g) / 1000) %>%
  gg_col(
    x = body_mass_kg, 
    y = species, 
    col = sex, 
    facet = island,
    col_labels = snakecase::to_sentence_case, 
    position = "dodge")
image.png
11、{ggblanket}默认将未指定的标题转换为snakecase::to_sentence。对于需要手动更改的标题,可以使用x_title、y_title或col_title手动更改。也可以用titles = ~.x表示按照变量名保留未指定的标题。
penguins %>%
  group_by(species, sex) %>%
  summarise(across(body_mass_g, ~ round(mean(.x, na.rm = TRUE)), 0)) %>% 
  gg_tile(
    x = sex, 
    y = species, 
    col = body_mass_g, 
    x_labels = snakecase::to_sentence_case,
    pal = pals::brewer.blues(9), 
    width = 0.9,
    height = 0.9,
    col_legend_place = "r",
    title = "Average penguin body mass",
    subtitle = "Palmer Archipelago, Antarctica",
    theme = gg_theme(grid_h = FALSE,
                     bg_plot_pal = "white",
                     axis_pal = "white", 
                     ticks_pal = "white")) +
  geom_text(aes(label = body_mass_g), col = "#232323", size = 3.5) 
image.png
12、{ggblanket}提供了一个gg_blank函数以实现输入一些包中不支持的几何图形、主题参数或想要继续添加其他图层时:
penguins %>%
  tidyr::drop_na(sex) %>%
  mutate(sex = stringr::str_to_sentence(sex)) %>%
  group_by(species, sex) %>%
  summarise(
    mean = round(mean(bill_length_mm, na.rm = TRUE), 0),
    n = n(),
    se = mean / sqrt(n),
    upper = mean + 1.96 * se,
    lower = mean - 1.96 * se
  ) %>%
  gg_blank(
    x = sex,
    y = mean,
    col = sex,
    facet = species,
    label = mean,
    ymin = lower,
    ymax = upper,
    y_include = 0,
    y_title = "Bill length mm"
  ) +
  geom_col(width = 0.75, alpha = 0.9) +
  geom_errorbar(width = 0.1, colour = pal_na()) 
image.png
13、{ggblanket}支持用户结合文本参数和ggplotly中的tooltip = "text "参数创建漂亮的文字提示标签(需要鼠标移动到需要提示位置才会显示):
theme_custom <- gg_theme(
  "helvetica",
  bg_plot_pal = "white",
  bg_panel_pal = "white",
  grid_h = TRUE
)

iris %>% 
  mutate(Species = stringr::str_to_sentence(Species)) %>% 
  add_tooltip_text(titles = snakecase::to_sentence_case) %>% 
  gg_point(
    x = Sepal.Width, 
    y = Sepal.Length, 
    col = Species, 
    text = text, 
    col_legend_place = "r",
    theme = theme_custom) %>% 
  plotly::ggplotly(tooltip = "text")
image.png
14、{ggblanket}通过以下方式提供对ggplot2包中其他geom_参数的访问(大家可以自行探索,表面意思应该是可以直接在gg_函数中使用goem_*中的参数):
penguins %>%
  tidyr::drop_na(sex) %>%
  gg_smooth(
    x = flipper_length_mm,
    y = body_mass_g,
    col = sex,
    level = 0.99, #来自geom_smooth的参数
    col_legend_place = "t",
    col_title = "", 
    col_labels = snakecase::to_sentence_case) 
image.png
15、个性化创建绘图函数:

gg_point_custom <- function(data, x, y, col, 
                            size = 3, 
                            pal = pals::brewer.dark2(9), 
                            col_title = "", 
                            col_legend_place = "t",
                            ...) {
  data %>% 
    gg_point(x = {{ x }}, y = {{ y }}, col = {{col}}, 
             size = size, 
             pal = pal, 
             col_title = col_title, 
             col_legend_place = col_legend_place, 
             ...)
}

iris %>%
  mutate(Species = stringr::str_to_sentence(Species)) %>% 
  gg_point_custom(
    x = Sepal.Width,
    y = Sepal.Length,
    col = Species, 
    title = "Edgar Anderson's iris data",
    subtitle = "Iris sepal length by width and species",
    caption = "Edgar Anderson, 1935"
  )
image.png
参考:https://davidhodge931.github.io/ggblanket/
更多精彩欢迎大家关注微信公众号【科研后花园】!!!

相关文章

  • R可视化——ggblanket包介绍

        今天,小编给大家介绍一款简单好用的作图R包——ggblanket,这是一个将ggplot2作图代码进行简化...

  • python--seaborn散点图

    seaborn是专门用于统计数据可视化的包,可媲美R语言中的ggplot2包。本文介绍用seaborn绘制散点图。...

  • python--seaborn直方图

    seaborn是专门用于统计数据可视化的包,可媲美R语言中的ggplot2包。本文介绍用seaborn绘制直方图。...

  • 经典信号通路作图工具包pathview

    介绍个数据整合和可视化的R包pathview。 首先安装包并载入数据: 基因表达变化数据框如下所示,行是基因ID,...

  • python--seaborn盒形图

    seaborn是专门用于统计数据可视化的包,可媲美R语言中的ggplot2包。本文介绍用seaborn绘制盒形图。...

  • python--seaborn折线图

    seaborn是专门用于统计数据可视化的包,可媲美R语言中的ggplot2包。本文介绍用seaborn绘制折线图。...

  • python--seaborn热力图

    seaborn是专门用于统计数据可视化的包,可媲美R语言中的ggplot2包。本文介绍用seaborn绘制热力图。...

  • R语言rattle可视化包介绍

    一期介绍数据挖掘可视化工具Rattle包,是一个用于数据挖掘的R语言图形交互界面,可以快速处理常见的数据挖掘问题。...

  • 三、差异分析

    三大R包差异分析 可视化 三大R包差异基因对比 #########生信技能树课程笔记

  • R语言入门——ggplot2

    常用可视化R包 ggplot2 统计变换 总结

网友评论

      本文标题:R可视化——ggblanket包介绍

      本文链接:https://www.haomeiwen.com/subject/pvkvgrtx.html