多元数据直观分析

Author

221527202杨咏琪

library(openxlsx)
library(tidyverse)
── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
✔ dplyr     1.1.4     ✔ readr     2.1.5
✔ forcats   1.0.0     ✔ stringr   1.5.1
✔ ggplot2   3.5.1     ✔ tibble    3.2.1
✔ lubridate 1.9.3     ✔ tidyr     1.3.1
✔ purrr     1.0.2     
── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
✖ dplyr::filter() masks stats::filter()
✖ dplyr::lag()    masks stats::lag()
ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors
library(aplpack)
library(TeachingDemos)

Attaching package: 'TeachingDemos'

The following objects are masked from 'package:aplpack':

    faces, slider
library(fmsb)
library(ggiraphExtra)
library(andrews) 
See the package vignette with `vignette("andrews")`
data=read.xlsx('D:/chapter 3/mvstats5.xlsx','d3.1',rowNames=T)

各省消费项目均值条形图

(将横轴左边旋转90度,各省的名称均可显示)

barplot(apply(data,1,mean),las=3)#按行做均值条形图

利用ggplot2包作图较为美观

data %>%
  mutate(Average_Consumption = rowMeans(select(., -1), na.rm = TRUE)) %>% 
  ggplot(aes(x = reorder(row.names(data), -Average_Consumption), y = Average_Consumption)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "black", fill = "navy") +
  labs(title = "各省消费项目均值条形图", x = "", y = "均值") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

各消费项目均值条形图

按消费项目做均值图条形图

barplot(apply(data,2,mean))#按列做均值图条形图

barplot(apply(data[,c(1,4:6)],2,mean),col=2:5)#只留下1、4、5、6列后的数据按列做标准差条形图

利用ggplot作均值条形图

data %>% summarise(across(everything(), mean, na.rm = TRUE)) %>% 
  pivot_longer(cols = everything(), names_to = "Consumption_Type", values_to = "Average") %>% 
  mutate(
    Consumption_Type=factor(Consumption_Type,level=c('食品','衣着','设备','医疗','交通','教育','居住','杂项')),
  ) %>% 
  ggplot(aes(x = Consumption_Type, y = Average, fill = Consumption_Type)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "black") +
  theme_minimal() +
  labs(title = "各消费项目均值条形图", x = "类别", y = "均值",fill = "消费种类")
Warning: There was 1 warning in `summarise()`.
ℹ In argument: `across(everything(), mean, na.rm = TRUE)`.
Caused by warning:
! The `...` argument of `across()` is deprecated as of dplyr 1.1.0.
Supply arguments directly to `.fns` through an anonymous function instead.

  # Previously
  across(a:b, mean, na.rm = TRUE)

  # Now
  across(a:b, \(x) mean(x, na.rm = TRUE))

使各条形的颜色相同

data %>% summarise(across(everything(), mean, na.rm = TRUE)) %>% 
  pivot_longer(cols = everything(), names_to = "Consumption_Type", values_to = "Average") %>% 
  mutate(
    Consumption_Type=factor(Consumption_Type,level=c('食品','衣着','设备','医疗','交通','教育','居住','杂项')),
  ) %>% 
  ggplot(aes(x = Consumption_Type, y = Average)) +
  geom_bar(stat = "identity", position = position_dodge(), colour = "yellow", fill = "pink") +
  theme_minimal() +
  labs(title = "各消费项目均值条形图", x = "类别", y = "均值")

各消费项目箱线图

boxplot函数直接作箱线图,默认每个变量(列)作一个箱线,并将全部变量的箱线在同一个图中展示。

boxplot(data)#按列做箱线图

boxplot(data,horizontal=T,las=1)#箱线图中图形按水平放置

利用ggplot函数作箱线图,需要对数据转化为长结果数据

data %>% pivot_longer(cols = 1:8, names_to = "Consumption_Type", values_to = "Value") %>% 
  mutate(
    Consumption_Type=factor(Consumption_Type,level=c('食品','衣着','设备','医疗','交通','教育','居住','杂项')),
  ) %>% 
  ggplot(aes(x = Consumption_Type, y = Value)) +
  geom_boxplot() +
  labs(title = "各消费项目箱线图", x = "", y = "消费水平") +
  theme_minimal() #  + coord_flip() 

各消费项目星相图

stars(data)  #具有图例的360度星相图

stars(data,full=F,draw.segments=T,key.loc=c(19,9))#具有图例的180度彩色圆形星相图

各消费项目脸谱图

aplpack::faces(data)

effect of variables:
 modified item       Var   
 "height of face   " "食品"
 "width of face    " "衣着"
 "structure of face" "设备"
 "height of mouth  " "医疗"
 "width of mouth   " "交通"
 "smiling          " "教育"
 "height of eyes   " "居住"
 "width of eyes    " "杂项"
 "height of hair   " "食品"
 "width of hair   "  "衣着"
 "style of hair   "  "设备"
 "height of nose  "  "医疗"
 "width of nose   "  "交通"
 "width of ear    "  "教育"
 "height of ear   "  "居住"
aplpack::faces(data[,c(1,3:6)],ncol.plot=8)#去掉第2、7、8变量按每行6个做脸谱图

effect of variables:
 modified item       Var   
 "height of face   " "食品"
 "width of face    " "设备"
 "structure of face" "医疗"
 "height of mouth  " "交通"
 "width of mouth   " "教育"
 "smiling          " "食品"
 "height of eyes   " "设备"
 "width of eyes    " "医疗"
 "height of hair   " "交通"
 "width of hair   "  "教育"
 "style of hair   "  "食品"
 "height of nose  "  "设备"
 "width of nose   "  "医疗"
 "width of ear    "  "交通"
 "height of ear   "  "教育"
aplpack::faces(data[c(1,7,9,13,19,27,28,29),])#选择第1,7,9,13,19,27,28,29个观测的多元数据做脸谱图

effect of variables:
 modified item       Var   
 "height of face   " "食品"
 "width of face    " "衣着"
 "structure of face" "设备"
 "height of mouth  " "医疗"
 "width of mouth   " "交通"
 "smiling          " "教育"
 "height of eyes   " "居住"
 "width of eyes    " "杂项"
 "height of hair   " "食品"
 "width of hair   "  "衣着"
 "style of hair   "  "设备"
 "height of nose  "  "医疗"
 "width of nose   "  "交通"
 "width of ear    "  "教育"
 "height of ear   "  "居住"

各消费项目雷达图

ggplot2的扩展包ggiraphExtra能作雷达图

data[c(1,7,9,13,19,27,28,29),] %>% 
  mutate(省份=rownames(.)) %>% 
  ggRadar(aes(group = 省份)) 

各消费项目调和曲线图

source("D:/chapter 3/msaR.R")#加自定义函数
msa.andrews(data)#绘制调和曲线图

msa.andrews(data[c(1,7,9,13,19,27,28,29),])

andrews(data,clr=5,ymax=6)

#选择第1,9,19,28,29,30个观测的多元数据做调和曲线图
andrews(data[c(1,7,9,13,19,27,28,29),],clr=5,ymax=6)