一、常用的绘图函数
ggplot2包是R语言中数据可视化的世界级库,ggpubr包是一款基于ggplot2进行封装的包,它解决了ggplot2绘图流程繁琐、调试困难等问题。在实际应用中,我们常常会用到以下几种函数:
1、ggscatter()
ggscatter() 用于绘制散点图,可以通过设置颜色、大小、形状等参数来展示不同的分类变量或数值差异。
{
library(ggpubr)
ggscatter(iris, x = "Sepal.Length", y = "Sepal.Width",
color = "Species", palette = "jco", size = 3,
shape = 21, title = "Iris dataset",
xlab = "Sepal length (cm)", ylab = "Sepal width (cm)")
}
2、ggline()
ggline() 主要用于绘制线性图,通过比较不同组别之间的数值差异,并分别用不同图例颜色进行展示。
{
ggline(mpg, x = "year", y = "cty", color = "manufacturer",
palette = "jco", add = "mean_se",
title = "City mileage by manufacturer and year",
xlab = "Year of production", ylab = "Miles per gallon")
}
3、ggdensity()
ggdensity() 用于绘制密度图,可以通过比较不同组别之间的数据分布情况进行统计分析。
{
# plot density of diamond prices by color
ggdensity(diamonds, x = "price", fill = "color", palette = "jco",
title = "Density plot of diamond prices by color",
xlab = "Price (US dollars)", ylab = "Density")
}
二、图表排版相关函数介绍
通过ggpubr包中的函数,可以进行图表及文本元素的排版/组合,便于生成美观、易读的数据报告。
1、ggarrange()
ggarrange() 用于组合多个ggplot2图表,可以根据需要调整排版方式(比如一行多列、多行多列、居中对齐等)。
{
# first create ggplot objects
p1 <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) + geom_point() + ggtitle("Iris dataset")
p2 <- ggplot(mpg, aes(x = cty, color = drv)) + geom_density() + ggtitle("Distribution of city miles per gallon by driver type")
# arrange plots in two rows and one column
ggarrange(p1, p2, nrow = 2, common.legend = TRUE, legend = "right")
}
2、ggtext()
ggarrange() 用于组合多个ggplot2图表,可以根据需要调整排版方式(比如一行多列、多行多列、居中对齐等)。
{
# define text elements
text1 <- "This is an example of a text line in a ggplot2 plot."
text2 <- "This is another line of text separated by a line break.\nNote that we can use 'atop' to stack multiple lines of text on top of each other."
# create plot object
p <- ggplot(mpg, aes(x = class, y = hwy, fill = class)) +
geom_boxplot() +
ggtitle("Highway mileage by vehicle class") +
theme_pubclean() +
# add text elements using ggtext()
ggtext(autoplottext(
text_col = "black",
face = "plain",
atop(
text1,
text2
)
),
x = 0.2,
y = 1.1,
size = 12
)
}
三、自定义配色和主题
ggpubr包中还提供了覆盖ggplot2默认配色和主题的函数,以便实现自定义的视觉效果。
1、get_palette()
get_palette() 可以获取常见的颜色配色方案r,也可以按照自己的需求进行设置。下面是一个展示rColorBrewer库中的配色方案的例子:
{
# define color palettes
library(rColorBrewer)
palettes <- c("YlOrRd", "PuBu", "RdPu", "Greens", "Blues", "PuRd")
# create plot object
p <- ggplot(iris, aes(x = Sepal.Length, y = Sepal.Width, color = Species)) +
geom_point() +
ggtitle("Iris dataset")
# create plots using different palettes
ggarrange(
map(palettes, ~p + scale_color_brewer(palette = .x, name = .x)),
ncol = 2, nrow = 3,
common.legend = TRUE,
legend = "right"
)
}
2、theme_pubr()
theme_pubr() 提供了基于ggplot2的自定义主题,可以快速的修改图表的外观和风格。
{
# create plot object
p <- ggplot(mpg, aes(x = class, y = hwy, fill = drv)) +
geom_boxplot() +
ggtitle("Highway mileage by vehicle class and driver type") +
theme_pubr()
}
四、图例相关函数介绍
图例(legend)是一种展示图表中各种颜色和形状代表的意义的元素。
1、get_legend()
get_legend() 用于提取图表中的图例,可以进行位置移动、大小修改、标题修改等相应的修改操作。
{
# create plot object
p <- ggplot(mpg, aes(x = class, y = hwy, fill = drv)) +
geom_boxplot() +
ggtitle("Highway mileage by vehicle class and driver type")
# extract the legend and adjust its position
legend <- get_legend(p)
p + theme(legend.position = "none") +
annotation_custom(legend$grobs[[1]], xmin = 0.8, xmax = 1, ymin = 0.5, ymax = 0.7)
}
2、add_legend()
add_legend() 可以为图表添加新的图例,将每种变量的含义清晰展示出来,便于观察和分析。
{
# create plot object
p <- ggplot(mpg, aes(x = class, y = hwy, color = drv)) +
geom_point() +
ggtitle("Highway mileage by vehicle class and driver type")
# add a title to the legend
legend_title <- "Driver type"
p + scale_color_manual(values = c("#F8766D", "#00BA38", "#619CFF"), name = legend_title) +
add_legend(title = legend_title)
}