USPersonalExpenditure
USPersonalExpenditure in R Markdown
KY
2020-04-23
##畫圖和指令相關語法
library(lattice)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(tidyr)
library(ggplot2)
library(grid)
library(gridExtra)
##
## Attaching package: 'gridExtra'
## The following object is masked from 'package:dplyr':
##
## combine
##找出資料庫,使用資料
require(stats)
USPersonalExpenditure
## 1940 1945 1950 1955 1960
## Food and Tobacco 22.200 44.500 59.60 73.2 86.80
## Household Operation 10.500 15.500 29.00 36.5 46.20
## Medical and Health 3.530 5.760 9.71 14.0 21.10
## Personal Care 1.040 1.980 2.45 3.4 5.40
## Private Education 0.341 0.974 1.80 2.6 3.64
dta <- USPersonalExpenditure
##網頁推薦USPersonalExpenditure使用medpolish,但不知道功用
medpolish(log10(dta))
## 1: 1.126317
## 2: 1.032421
## Final: 1.032421
##
## Median Polish Results (Dataset: "log10(dta)")
##
## Overall: 0.9872192
##
## Row Effects:
## Food and Tobacco Household Operation Medical and Health Personal Care
## 0.7880270 0.4327608 0.0000000 -0.5606543
## Private Education
## -0.7319467
##
## Column Effects:
## 1940 1945 1950 1955 1960
## -0.4288933 -0.2267967 0.0000000 0.1423128 0.3058289
##
## Residuals:
## 1940 1945 1950 1955 1960
## Food and Tobacco 0.000000 0.0999105 0.000000 -0.053048 -0.142555
## Household Operation 0.030103 -0.0028516 0.042418 0.000000 -0.061167
## Medical and Health -0.010551 0.0000000 0.000000 0.016596 0.031234
## Personal Care 0.019362 0.0968971 -0.037399 -0.037399 0.000000
## Private Education -0.293625 -0.0399168 0.000000 0.017388 0.000000
##重新命名欄列名稱
colnames(dta)<-c("1940y","1945y","1950y","1955y","1960y")
rownames(dta)<-c("FT","HO","MH","PC","PE")
##看看命名後狀況
head(dta)
## 1940y 1945y 1950y 1955y 1960y
## FT 22.200 44.500 59.60 73.2 86.80
## HO 10.500 15.500 29.00 36.5 46.20
## MH 3.530 5.760 9.71 14.0 21.10
## PC 1.040 1.980 2.45 3.4 5.40
## PE 0.341 0.974 1.80 2.6 3.64
##將資料從寬資料轉長資料,才方便製圖
library(reshape2)
##
## Attaching package: 'reshape2'
## The following object is masked from 'package:tidyr':
##
## smiths
dta1<- melt(dta)
##一樣觀察轉變後狀況
head(dta1)
## Var1 Var2 value
## 1 FT 1940y 22.200
## 2 HO 1940y 10.500
## 3 MH 1940y 3.530
## 4 PC 1940y 1.040
## 5 PE 1940y 0.341
## 6 FT 1945y 44.500
names(dta1)
## [1] "Var1" "Var2" "value"
##命名新的欄列名稱
colnames(dta1)<- c("category","year", "expenditure")
head(dta1)
## category year expenditure
## 1 FT 1940y 22.200
## 2 HO 1940y 10.500
## 3 MH 1940y 3.530
## 4 PC 1940y 1.040
## 5 PE 1940y 0.341
## 6 FT 1945y 44.500
##大略看一下圖形關係
library(ggfortify)
autoplot(with(dta1,
lm(expenditure ~ category))) +
theme_bw()

##雖然簡單分出category,year,expenditure,但覺得ggplot語法不熟悉,許多圖形常跑error
ggplot(dta1, aes(category, expenditure)) +
geom_boxplot(col='gray') +
geom_point() +
facet_wrap(. ~ year) +
theme_bw()
