多元数据直观表示

各省消费项目均值条形图

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

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

将横轴左边旋转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 = "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=1:8) #按列做彩色均值图条形图

去掉食品列后的数据按列做均值条形图

barplot(apply(data[,2:8],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)#箱线图中图形按水平放置

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

各消费项目星相图

绘制一个360度的星相图,用数据data的变量表示图中每个辐条的长度,每个变量形成一条辐射线。

stars(data)

在绘制360度星相图的同时,在位置(17, 7)显示图例。

stars(data,key.loc=c(17,7)) 

绘制一个180度的半圆形星相图,并在(17, 7)显示图例。

stars(data,draw.segments=T,key.loc=c(17,7))

绘制一个带彩色分段的360度圆形星相图,并在(17, 7)显示图例。

stars(data,full=F,draw.segments=T,key.loc=c(17,7))

各消费项目脸谱图

绘制数据的“脸谱图”,用不同的面部特征(如眼睛、嘴巴、鼻子等)表示data中不同变量的数值。

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   "  "居住"

绘制除去第一个变量后(第2到第8列)的脸谱图,每行显示7个脸谱。

aplpack::faces(data[,2:8],ncol.plot=7)

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   "  "衣着"

仅绘制d3.1数据中第1、9、19、28、29和30行的脸谱图。

aplpack::faces(data[c(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   "  "居住"

使用TeachingDemos库中的faces2函数绘制d3.1的数据脸谱图,每行显示7个脸谱。

library("TeachingDemos") 
faces2(data,ncols=7) 

各消费项目雷达图

ggplot2的扩展包ggiraphExtra能作雷达图

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

各消费项目调和曲线图

绘制数据d3.1中第1、9、19、28、29和30行的调和曲线图。

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

msa.andrews(data[c(1,9,19,28,29,30),])

加载fmsb库,该库用于绘制雷达图(或蜘蛛图)。

将数据d3.1中第1、9、19、28、29和30行提取为新变量rddat

计算rddat中每个列的最大值和最小值,并将它们绑定在一起形成一个新矩阵maxmin

将最大值和最小值行绑定到rddat的顶部,准备绘制雷达图。

绘制雷达图,参数说明:axistype=2:定义轴的类型。 pcol=topo.colors(6):使用topo.colors函数生成6种颜色为线条颜色。 plty=1:定义线型样式。 pdensity=seq(5,40,by=5):设置多边形的填充密度。 pangle=seq(0,150,by=30):定义填充角度。 pfcol=topo.colors(6):为多边形填充相应颜色。

library("fmsb")
rddat=data[c(1,9,19,28,29,30),]
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))