R语言相关分析及分布分析矩阵GGally包应用分享

代码案例功能解读

这部分代码使用了GGally包中的ggpairs函数来进行数据可视化和探索性分析。下面对代码进行模块说明,并解释生成的图表。

  1. 加载所需的包和函数:
RCopy Code

library(GGally) p_ <- GGally::print_if_interactive
  • 首先加载了GGally包,并定义了一个print_if_interactive函数的别名。
  1. 示例数据集和基本图表:
 
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_函数来显示绘制的图表。
  1. 不同类型的图表:
 
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函数绘制一个只有下三角区域的空白散点图矩阵。
  1. 绘图类型:
 
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函数绘制一个具有不同绘图类型的散点图矩阵。上三角区域使用密度图和箱线图组合的方式绘制,下三角区域使用散点图和点图组合的方式绘制。
  1. 使用自定义函数:
 
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)
  • 使用自定义的函数替代默认的绘图函数来绘制散点图矩阵。上三角区域使用自定义的密度图和箱线图函数,下三角区域使用自定义的散点图和点图函数。
  1. 自定义图表样式和标签:
 
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"。
  1. 自定义图表布局和主题:
 
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())来自定义图表的布局和样式。
  1. 使用面板数据和自定义标签:
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)

  • 22
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

安宁ᨐ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值