多元数据直观表示

各省消费项目均值条形图

省份过多,各省的名称均不能全部显示

barplot(apply(data,1,mean))#按行做均值条形图apply对矩阵案列或行操作,barplot画条形图

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

barplot(apply(data,1,mean),las=3)#按行做均值条形图las = 0:轴标签平行于轴(默认值)。

#las = 1:轴标签水平显示。
#las = 2:轴标签垂直显示,与轴方向成90度角。
#las = 3:轴标签垂直显示,与轴方向成90度,但是相对 las=2 反转方向。

利用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 = "steelblue") +
  labs(title = "各省消费项目均值条形图", x = "", y = "均值") +
  theme_minimal() +
  theme(axis.text.x = element_text(angle = 90, hjust = 1)) 

各消费项目均值条形图

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

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

对不同项目的条形添加不同颜色

 barplot(apply(data,2,mean),col=c("blue", "red", "green", "yellow")) #按列做彩色均值图条形图,col设定颜色

去掉衣着列后的数据按列做均值条形图

barplot(apply(data[,-2],2,mean))

按消费项目做中位数条形图

barplot(apply(data,2,median))

利用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 = "black", fill = "steelblue") +
  theme_minimal() +
  labs(title = "各消费项目均值条形图", x = "类别", y = "均值")

各消费项目箱线图

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

boxplot(data)#按列做箱线图

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

利用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(d3.1)               

stars(d3.1,key.loc=c(-3,7))                #具有图例的360度星相图,key设定图例,loc图例位置

stars(d3.1,full=F,key.loc=c(17,7))                #full=F半圆

stars(d3.1,draw.segments=T,key.loc=c(17,7))#具有图例的360度彩色圆形星相图,draw.segments = T:T 代表TRUE。启用此参数表示将每个变量的数据画成不同颜色的分段,这些分段构成一个完整的圆形。

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

各消费项目脸谱图

library(aplpack) #加载aplpack包
aplpack::faces(d3.1)

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(d3.1[,-1],ncol.plot=5)#去掉第一个变量按每行5个做脸谱图

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(d3.1[c(1,9,19,28,29,30),])#选择第1,9,19,28,29,30个观测的多元数据做脸谱图

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   "  "居住"
library("TeachingDemos") #install.packages("TeachingDemos")
faces2(d3.1,ncols=7) #TeachingDemos::faces(d3.1),是另一种绘制脸谱图的方式,来自 TeachingDemos 包,每行展示7个脸谱图。

各消费项目雷达图

library("fmsb")#打开包
rddat=d3.1[c(1,9,19,28,29,30),]#赋值数据,第1、9...等省份
maxmin=rbind(apply(rddat,2,max),apply(rddat,2,min))#将最大最小值赋值给新变量
rddat=rbind(maxmin,rddat)#将最值矩阵添加到原矩阵顶部
radarchart(rddat, axistype=2, pcol=topo.colors(6), plty=1, pdensity=seq(5,40,by=5), pangle=seq(0,150,by=30), pfcol=topo.colors(6))#绘图axistype = 2:指定坐标轴的类型,2 表示坐标轴在每个顶点上,适合展示极值。

#pcol = topo.colors(6):指定绘制线条的颜色,使用 topo.colors(6) #生成6种颜色,适合数据中包含6个观测值。
#plty = 1:设置线条类型,1 表示实线。
#pdensity = seq(5, 40, by=):设置线条的填充密度,这里设置为从5到40的序列,步长为5。
#pangle = seq(0, 150, by=30):设置每个变量的角度,默认情况下可能会覆盖到每个顶点。
#pfcol = topo.colors(6):设置填充颜色,同样使用 topo.colors(6)。

ggplot2的扩展包ggiraphExtra能作雷达图

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

各消费项目调和曲线图

#install.packages("andrews")
library(andrews) 
See the package vignette with `vignette("andrews")`
andrews(d3.1,clr=5,ymax=6)

#选择第1,9,19,28,29,30个观测的多元数据做调和曲线图
andrews(d3.1[c(1,9,19,28,29,30),],clr=5,ymax=8) #clr=5颜色ymax=6坐标最值

source("msaR.R")#加自定义函数
msa.andrews(d3.1)#绘制调和曲线图

msa.andrews(d3.1[c(1,9,19,28,29,30),])#选择特定数据绘制