数据科学
用可视化方式展现变量之间的关系。R-GGally包就可以轻松绘制配对图矩阵、散点图矩阵、平行坐标图和生存图等。主要内容如下:

  • R-GGally包简介
  • R-GGally包主要函数示例

    R-GGally包简介

    作为R-ggplot2的拓展包,其可以通过添加定义好的绘图函数绘制例如散点图矩阵、平行坐标图等统计图表。

    官网

    https://ggobi.github.io/ggally/index.html

    主要绘图函数简介

  • ggmatrix():用于管理矩阵状布局中的多图的绘图函数,其可适应任何数量的行和列。

  • ggpairs():作为ggmatrix()的一种特殊形式,可实现对多元数据进行成对比较。默认情况下,ggpairs(): 提供每对列的两次不同比较,并沿对角线显示相应变量的密度或计数。通过不同的参数设置,可将对角线替换为轴值或者变量标签。
  • ggduo():在绘图矩阵中用于显示两个分组数据,比较适用于多时间序列分析和回归分析。
  • ggally_*():用于绘制多种高级图表。
  • ggbivariate():用于地绘制一个结果和几个解释变量之间的双变量关系的可视化图。
  • ggnostic():用于显示每个给定解释变量的完整模型诊断。
  • ggscatmat():用于数字矩阵图绘制。
  • ggtable():用于绘制绘制离散变量的交叉表。
  • ggcoef_model():用于绘制模型的系数。
  • ggnetworkmap():用于绘制各种精美的地图。

以上就是R-GGally包较为常用的绘图包。

R-GGally包主要函数示例

ggmatrix()绘图函数

  1. plotList <- list()
  2. for (i in 1:6) {
  3. plotList[[i]] <- ggally_text(paste("Plot #", i, sep = ""),color = I("#0057FF"))
  4. }
  5. #可视化绘制
  6. ggmatrix <- GGally::ggmatrix(
  7. plotList,
  8. nrow = 2, ncol = 3,
  9. xAxisLabels = c("A", "B", "C"),
  10. yAxisLabels = c("D", "E"),
  11. title = "Matrix Title",
  12. byrow = FALSE) +
  13. labs(
  14. title = "Example of <span style='color:#D20F26'>GGally::ggmatrix function</span>",
  15. subtitle = "processed charts with <span style='color:#1A73E8'>ggmatrix()</span>",
  16. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  17. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  18. theme(
  19. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  20. size = 20, margin = margin(t = 1, b = 12)),
  21. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  22. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-116428.png
Example of ggmatrix

ggpairs()绘图函数

  1. data(tips, package = "reshape")
  2. ggpairs <- GGally::ggpairs(tips, mapping = aes(fill = sex,color=sex),
  3. columns = c("total_bill", "time", "tip")) +
  4. ggsci::scale_fill_jco() +
  5. ggsci::scale_color_jco() +
  6. labs(
  7. title = "Example of <span style='color:#D20F26'>GGally::ggpairs function</span>",
  8. subtitle = "processed charts with <span style='color:#1A73E8'>ggpairs()</span>",
  9. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  10. #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  11. theme_bw(base_family = "Roboto Condensed")+
  12. theme(
  13. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  14. size = 20, margin = margin(t = 1, b = 12)),
  15. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  16. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-167262.png
Example of ggpairs

ggally_*系列

ggally_cross()

  1. plot01 <- ggally_cross(tips, aes(x = day, y = smoker, fill = smoker)) +
  2. ggsci::scale_fill_jco() +
  3. labs(
  4. title = "Example of <span style='color:#D20F26'>GGally::ggally_cross function</span>",
  5. subtitle = "processed charts with <span style='color:#1A73E8'>ggally_cross()</span>",
  6. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  7. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  8. theme(
  9. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  10. size = 20, margin = margin(t = 1, b = 12)),
  11. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  12. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-271981.png
Example of ggally_cross

ggally_densityDiag()

  1. plot02 <- ggally_densityDiag(tips, aes(x = day, fill = time)) +
  2. ggsci::scale_fill_jco() +
  3. labs(
  4. title = "Example of <span style='color:#D20F26'>GGally::ggally_densityDiag function</span>",
  5. subtitle = "processed charts with <span style='color:#1A73E8'>ggally_densityDiag()</span>",
  6. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  7. hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  8. theme(
  9. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  10. size = 20, margin = margin(t = 1, b = 12)),
  11. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  12. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-414598.png
Example of ggally_densityDiag

ggbivariate()绘图函数

  1. ggbivariate <- ggbivariate(tips, outcome = "smoker",
  2. explanatory = c("day", "time", "sex", "tip"),
  3. rowbar_args = list(
  4. colour = "white",
  5. size = 3,
  6. fontface = "bold",
  7. label_format = scales::label_percent(accurary = 1)
  8. )) +
  9. ggsci::scale_fill_jco() +
  10. labs(
  11. title = "Example of <span style='color:#D20F26'>GGally::ggbivariate function</span>",
  12. subtitle = "processed charts with <span style='color:#1A73E8'>ggbivariate()</span>",
  13. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  14. #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  15. theme_light(base_family = "Roboto Condensed") +
  16. theme(
  17. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  18. size = 20, margin = margin(t = 1, b = 12)),
  19. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  20. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-520770.png
Example of ggbivariate

ggnostic()绘图函数

  1. # 构建模型
  2. flea_model <- step(lm(head ~ ., data = flea), trace = FALSE)
  3. ggnostic <- ggnostic(flea_model) +
  4. labs(
  5. title = "Example of <span style='color:#D20F26'>GGally::ggnostic function</span>",
  6. subtitle = "processed charts with <span style='color:#1A73E8'>ggnostic()</span>",
  7. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  8. #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  9. theme_light(base_family = "Roboto Condensed") +
  10. theme(
  11. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  12. size = 20, margin = margin(t = 1, b = 12)),
  13. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  14. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-580637.png
Example of ggnostic

ggcoef_model()绘图函数

  1. library(labelled)
  2. # 数据处理
  3. tips_labelled <- tips %>%
  4. labelled::set_variable_labels(
  5. day = "Day of the week",
  6. time = "Lunch or Dinner",
  7. total_bill = "Bill's total"
  8. )
  9. mod_labelled <- lm(tip ~ day + time + total_bill, data = tips_labelled)
  10. ggcoef_model <- ggcoef_model(mod_labelled,colour_guide = TRUE) +
  11. ggsci::scale_color_aaas() +
  12. labs(
  13. title = "Example of <span style='color:#D20F26'>GGally::ggcoef_model function</span>",
  14. subtitle = "processed charts with <span style='color:#1A73E8'>ggcoef_model()</span>",
  15. caption = "Visualization by <span style='color:#DD6449'>DataCharm</span>") +
  16. #hrbrthemes::theme_ipsum(base_family = "Roboto Condensed") +
  17. theme_light(base_family = "Roboto Condensed") +
  18. theme(
  19. plot.title = element_markdown(hjust = 0.5,vjust = .5,color = "black",face = 'bold',
  20. size = 20, margin = margin(t = 1, b = 12)),
  21. plot.subtitle = element_markdown(hjust = 0,vjust = .5,size=15),
  22. plot.caption = element_markdown(face = 'bold',size = 12))

2021-05-24-23-28-18-688321.png
Example of ggcoef_model()