一、常用的繪圖函數
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)
}
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-hant/n/309111.html