R语言之可视化(29)如何更改ggplot2中堆积条形图中的堆积顺序
R语言之可视化(30)扫地僧easystats(1)
1.see包简介
see包是一个R语言可视化工具包,它能为使用者提供漂亮的、出版级的图像展示。
本文中主要介绍see包使用的主要函数:
- plots:多图排列
- theme类:图形主题
- palette类:图形调色类
- geom_point2: 散点图
- coord_radar: 雷达图
- geom_violinhalf:小提琴图
2.see包安装
see包可以通过两种方式进行安装,一种是在gitlab进行安装,另一种是基于CRAN进行安装。
方法一:基于github进行安装install.packages("devtools")
代码语言:javascript复制devtools::install_github("easystats/see") library(see)
方法二:基于CRAN进行安装
代码语言:javascript复制install.packages("see")library(see)
3.see包的使用
用途1:十分方便的将多个图片整合到一张图上
代码语言:javascript复制p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))
geom_boxplot() theme_modern(axis.text.angle = 45) scale_fill_material_d()
p2 <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))
geom_violin() theme_modern(axis.text.angle = 45) scale_fill_material_d(palette = "ice")
p3 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Sepal.Length))
geom_point2() theme_modern() scale_color_material_c(palette = "rainbow")
plots(p1, p2, p3, n_columns = 2)
代码语言:javascript复制plots(p1, p2, p3, n_columns = 2, tags = paste("Fig. ", 1:3))
用途2提供新的图形主题
see包总共提供了modern、lucid、blackboard、abyss等四类主题。
- Modern
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species))
geom_point2() theme_modern()
- Lucid
library(ggplot2)
ggplot(iris, aes(x = Sepal.Width, y = Sepal.Length, color = Species))
geom_point2() theme_lucid()
- Blackboard
library(rstanarm)
library(modelbased)
dat <- rstanarm::stan_glm(Sepal.Width ~ poly(Petal.Length, 2),
data = iris) %>% estimate::estimate_link(keep_draws = TRUE,
length = 100, draws = 250) %>% estimate::reshape_draws()
p <- ggplot(dat, aes(x = Petal.Length, y = Draw, group = Draw_Group))
geom_line(color = "white", alpha = 0.05) scale_x_continuous(expand = c(0,
0)) scale_y_continuous(expand = c(0, 0))
p theme_blackboard()
- Abyss
用途3提供新的调色板
代码语言:javascript复制p1 <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))
geom_boxplot() theme_modern(axis.text.angle = 45) scale_fill_material_d()
p2 <- ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))
geom_violin() theme_modern(axis.text.angle = 45) scale_fill_material_d(palette = "ice")
p3 <- ggplot(iris, aes(x = Petal.Length, y = Petal.Width, color = Sepal.Length))
geom_point2() theme_modern() scale_color_material_c(palette = "rainbow")
plots(p1, p2, p3, n_columns = 2)
用途4绘制更好看的散点图
see包提供geom_point2制作散点图, geom_point2允许散点无边界轮廓。
代码语言:javascript复制normal <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length))
geom_point(size = 8, alpha = 0.3) theme_modern()
new <- ggplot(iris, aes(x = Petal.Width, y = Sepal.Length))
geom_point2(size = 8, alpha = 0.3) theme_modern()
plots(normal, new, n_columns = 2)
image
用途5绘制雷达图
代码语言:javascript复制library(dplyr)
library(tidyr)
data <- iris %>% group_by(Species) %>% summarise_all(mean) %>%
pivot_longer(-Species)
data %>% ggplot(aes(x = name, y = value, color = Species, group = Species))
geom_polygon(fill = NA, size = 2, show.legend = FALSE)
coord_radar(start = -pi/4) theme_minimal()
用途5绘制半小提琴图半点图
同时满足展示数据分布和数据多少的需求。
代码语言:javascript复制创建半小提琴半点图,可用于同时可视化分布和样本大小。
ggplot(iris, aes(x = Species, y = Sepal.Length, fill = Species))
geom_violindot(fill_dots = "black") theme_modern() scale_fill_material_d()