代码案例功能解读
这部分代码使用了GGally
包中的ggpairs
函数来进行数据可视化和探索性分析。下面对代码进行模块说明,并解释生成的图表。
- 加载所需的包和函数:
RCopy Code
library(GGally) p_ <- GGally::print_if_interactive
- 首先加载了
GGally
包,并定义了一个print_if_interactive
函数的别名。
- 示例数据集和基本图表:
RCopy Code
data(flea) ggpairs(flea, columns = 2:4) pm <- ggpairs(flea, columns = 2:4, ggplot2::aes(colour=species)) p_(pm)
- 加载示例数据集
flea
,并使用ggpairs
函数绘制数据集中列2到4的散点图矩阵。 - 然后,使用
ggpairs
函数创建一个带有颜色映射的散点图矩阵,其中颜色按照物种(species)进行编码。 - 最后,通过调用
p_
函数来显示绘制的图表。
- 不同类型的图表:
RCopy Code
data(tips, package = "reshape") pm <- ggpairs(tips[, 1:3]) p_(pm) pm <- ggpairs(tips, 1:3, columnLabels = c("Total Bill", "Tip", "Sex")) p_(pm) pm <- ggpairs(tips, upper = "blank") p_(pm)
- 加载示例数据集
tips
,并使用ggpairs
函数分别绘制前3列的散点图矩阵。 - 接着,使用
ggpairs
函数绘制包含自定义列标签的散点图矩阵。 - 最后,使用
ggpairs
函数绘制一个只有下三角区域的空白散点图矩阵。
- 绘图类型:
RCopy Code
pm <- ggpairs( tips[, c(1, 3, 4, 2)], upper = list(continuous = "density", combo = "box_no_facet"), lower = list(continuous = "points", combo = "dot_no_facet") ) p_(pm)
- 使用
ggpairs
函数绘制一个具有不同绘图类型的散点图矩阵。上三角区域使用密度图和箱线图组合的方式绘制,下三角区域使用散点图和点图组合的方式绘制。
- 使用自定义函数:
RCopy Code
pm <- ggpairs( tips[, c(1, 3, 4, 2)], upper = list(continuous = ggally_density, combo = ggally_box_no_facet), lower = list(continuous = ggally_points, combo = ggally_dot_no_facet) ) p_(pm)
- 使用自定义的函数替代默认的绘图函数来绘制散点图矩阵。上三角区域使用自定义的密度图和箱线图函数,下三角区域使用自定义的散点图和点图函数。
- 自定义图表样式和标签:
RCopy Code
data(diamonds, package="ggplot2") diamonds.samp <- diamonds[sample(1:dim(diamonds)[1], 1000), ] pm <- ggpairs( diamonds.samp[, 1:5], mapping = ggplot2::aes(color = cut), upper = list(continuous = wrap("density", alpha = 0.5), combo = "box_no_facet"), lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot_no_facet", alpha = 0.4)), title = "Diamonds" ) p_(pm)
- 加载
ggplot2
包中的diamonds
数据集,并从中抽取1000个样本。 - 使用
ggpairs
函数绘制这些样本的散点图矩阵。上三角区域使用自定义的密度图和箱线图函数,并设置透明度(alpha)值。下三角区域使用自定义的散点图和点图函数,并设置透明度值。标题设置为"Diamonds"。
- 自定义图表布局和主题:
RCopy Code
pm <- ggpairs( tips[, c(1, 3, 4, 2)], upper = list(continuous = "density", combo = "box_no_facet"), lower = list(continuous = "points", combo = "dot_no_facet"), diag = list(continuous = "barDiag"), layout = "circle", theme = ggplot2::theme_bw() ) p_(pm)
- 使用
ggpairs
函数绘制一个散点图矩阵,指定了上三角区域绘制密度图和箱线图,下三角区域绘制散点图和点图,对角线绘制条形图。 - 使用圆形布局(layout = "circle")和黑白主题(theme_bw())来自定义图表的布局和样式。
- 使用面板数据和自定义标签:
RCopy Code
data(economics, package = "ggplot2") econ <- reshape2::melt(economics[, c("date", "unemploy", "pop")], id.vars = "date") econ <- econ[econ$date >= as.Date("2010-01-01"), ] pm <- ggpairs(econ, upper = "blank", lower = "blank", title = "Economics Data") p_(pm)
- 加载
ggplot2
包中的economics
数据集,并进行数据重塑和筛选。 - 使用
ggpairs
函数绘制面板数据的散点图矩阵,设置上三角区域和下三角区域为空白,标题为"Economics Data"。
以上就是使用GGally
包中的ggpairs
函数进行数据可视化和探索性分析的示例代码及说明。通过调整参数和自定义函数,可以灵活地绘制各种类型的散点图矩阵,并进行个性化的样式设置。
所有代码的分享及案例图
library(GGally)
p_ <- GGally::print_if_interactive
## Quick example, with and without colour
data(flea)
ggpairs(flea, columns = 2:4)
pm <- ggpairs(flea, columns = 2:4, ggplot2::aes(colour=species))
p_(pm)
# Note: colour should be categorical, else you will need to reset
# the upper triangle to use points instead of trying to compute corr
data(tips, package = "reshape")
pm <- ggpairs(tips[, 1:3])
p_(pm)
pm <- ggpairs(tips, 1:3, columnLabels = c("Total Bill", "Tip", "Sex"))
p_(pm)
pm <- ggpairs(tips, upper = "blank")
p_(pm)
## Plot Types
# Change default plot behavior
pm <- ggpairs(
tips[, c(1, 3, 4, 2)],
upper = list(continuous = "density", combo = "box_no_facet"),
lower = list(continuous = "points", combo = "dot_no_facet")
)
p_(pm)
# Supply Raw Functions (may be user defined functions!)
pm <- ggpairs(
tips[, c(1, 3, 4, 2)],
upper = list(continuous = ggally_density, combo = ggally_box_no_facet),
lower = list(continuous = ggally_points, combo = ggally_dot_no_facet)
)
p_(pm)
# Use sample of the diamonds data
data(diamonds, package="ggplot2")
diamonds.samp <- diamonds[sample(1:dim(diamonds)[1], 1000), ]
# Different aesthetics for different plot sections and plot types
pm <- ggpairs(
diamonds.samp[, 1:5],
mapping = ggplot2::aes(color = cut),
upper = list(continuous = wrap("density", alpha = 0.5), combo = "box_no_facet"),
lower = list(continuous = wrap("points", alpha = 0.3), combo = wrap("dot_no_facet", alpha = 0.4)),
title = "Diamonds"
)
p_(pm)
## Axis Label Variations
# Only Variable Labels on the diagonal (no axis labels)
pm <- ggpairs(tips[, 1:3], axisLabels="internal")
p_(pm)
# Only Variable Labels on the outside (no axis labels)
pm <- ggpairs(tips[, 1:3], axisLabels="none")
p_(pm)
## Facet Label Variations
# Default:
df_x <- rnorm(100)
df_y <- df_x + rnorm(100, 0, 0.1)
df <- data.frame(x = df_x, y = df_y, c = sqrt(df_x^2 + df_y^2))
pm <- ggpairs(
df,
columnLabels = c("alpha[foo]", "alpha[bar]", "sqrt(alpha[foo]^2 + alpha[bar]^2)")
)
p_(pm)
# Parsed labels:
pm <- ggpairs(
df,
columnLabels = c("alpha[foo]", "alpha[bar]", "sqrt(alpha[foo]^2 + alpha[bar]^2)"),
labeller = "label_parsed"
)
p_(pm)
## Plot Insertion Example
custom_car <- ggpairs(mtcars[, c("mpg", "wt", "cyl")], upper = "blank", title = "Custom Example")
# ggplot example taken from example(geom_text)
plot <- ggplot2::ggplot(mtcars, ggplot2::aes(x=wt, y=mpg, label=rownames(mtcars)))
plot <- plot +
ggplot2::geom_text(ggplot2::aes(colour=factor(cyl)), size = 3) +
ggplot2::scale_colour_discrete(l=40)
custom_car[1, 2] <- plot
personal_plot <- ggally_text(
"ggpairs allows you\nto put in your\nown plot.\nLike that one.\n <---"
)
custom_car[1, 3] <- personal_plot
p_(custom_car)
## Remove binwidth warning from ggplot2
# displays warning about picking a better binwidth
pm <- ggpairs(tips, 2:3)
p_(pm)
# no warning displayed
pm <- ggpairs(tips, 2:3, lower = list(combo = wrap("facethist", binwidth = 0.5)))
p_(pm)
# no warning displayed with user supplied function
pm <- ggpairs(tips, 2:3, lower = list(combo = wrap(ggally_facethist, binwidth = 0.5)))
p_(pm)
## Remove panel grid lines from correlation plots
pm <- ggpairs(
flea, columns = 2:4,
upper = list(continuous = wrap(ggally_cor, displayGrid = FALSE))
)
p_(pm)
## Custom with/height of subplots
pm <- ggpairs(tips, columns = c(2, 3, 5))
p_(pm)
pm <- ggpairs(tips, columns = c(2, 3, 5), proportions = "auto")
p_(pm)
pm <- ggpairs(tips, columns = c(2, 3, 5), proportions = c(1, 3, 2))
p_(pm)