── 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(andrews)
See the package vignette with `vignette("andrews")`
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))
对8个不同项目的条形添加不同颜色
barplot(apply(data,2,mean),col=1:8)
去掉杂项列后的数据按列做均值条形图
barplot(apply(data[,1:7],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()
各消费项目星相图
此图缺少图例,不清楚那一份对应那个项目。
stars(data)
通过key.loc参数加入图例
stars(data,1,key.loc =c(17,7))
full=F指定该星象图为半圆
stars(data,full=F,key.loc =c(17,7))
各消费项目脸谱图
faces(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 " "居住"
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 " "衣着"