今天,小编给大家介绍一款简单好用的作图R包——ggblanket,这是一个将ggplot2作图代码进行简化的作图R包,比如这个包将原先ggplot2包中的ggplot()和geom_* ()两部分合并为gg_*,具体功能大家可以看原网页介绍:
![](https://img.haomeiwen.com/i28270211/6d9ba9f578f5d9d3.png)
看一下这个包绘制的一些图片的展示:
![](https://img.haomeiwen.com/i28270211/4d9b2a8c222d211e.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)
![](https://img.haomeiwen.com/i28270211/ca918d80f12a56ac.png)
3、{ggblanket}将ggplot2包中的col和fill参数合并到一个col参数中,通过col参数控制图形颜色填充:
penguins %>%
gg_histogram(
x = body_mass_g,
col = species)
![](https://img.haomeiwen.com/i28270211/f33c2f9298fd8d7d.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) #透明度
![](https://img.haomeiwen.com/i28270211/1816563f82327075.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")
![](https://img.haomeiwen.com/i28270211/7a72028acc69e273.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)
![](https://img.haomeiwen.com/i28270211/0f932fdede024e86.png)
6、{ggblanket}为了配合Rstudio自动补全参数的功能,也提供输入参数前缀以查找其完整参数,如键入x_、y_、col_或facet_后,按tab键就可以看到以对应关键词为前缀的参数,常用参数有:
![](https://img.haomeiwen.com/i28270211/e7005c4a3ee7234d.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()
)
![](https://img.haomeiwen.com/i28270211/326291e879db2574.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()
![](https://img.haomeiwen.com/i28270211/04cfbc04b2639a34.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')#图例位置设置
![](https://img.haomeiwen.com/i28270211/b2738f8cd1b2ebfc.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))#竖直网格线
![](https://img.haomeiwen.com/i28270211/a24d8683eee2e271.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")
![](https://img.haomeiwen.com/i28270211/db0f21b01e7e65b2.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)
![](https://img.haomeiwen.com/i28270211/c6f475e28c7c5af2.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())
![](https://img.haomeiwen.com/i28270211/b1c22e0ea5103353.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")
![](https://img.haomeiwen.com/i28270211/2c1d785cb6ff2deb.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)
![](https://img.haomeiwen.com/i28270211/fe14cb323c39f7e1.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"
)
![](https://img.haomeiwen.com/i28270211/8c2f2d6546bd9cc6.png)
网友评论