library(knitr)

Data

자료 출처는 통계청 보도자료.

load("./hh_econ.RData")
rownames(hh.econ.11.2) <- c("Market Income", "Disposable Income", "Consumer Spending")
colnames(hh.econ.11.2) <- paste(c("Bottom", "Second", "Third", "Fourth", "Top"), "Fifth")
kable(format(hh.econ.11.2, digits = 1, nsmall = 2, big.mark = ","), align = "r")
Bottom Fifth Second Fifth Third Fifth Fourth Fifth Top Fifth
Market Income 1,196.70 2,545.40 3,512.50 4,621.30 7,537.00
Disposable Income 961.30 2,113.90 2,914.90 3,762.90 6,144.20
Consumer Spending 1,260.50 1,864.60 2,325.10 2,791.70 3,654.60

ggplots

Data Reshaping

library(reshape2)
dimnames(hh.econ.11.2) <- list("Income.Expense" = rownames(hh.econ.11.2), "Income.Fifth" = colnames(hh.econ.11.2))
kable(format(hh.econ.melt <- melt(hh.econ.11.2, value.name = "Amount"), digits = 1, nsmall = 2, big.mark = ","), align = c("l", "l", "r"))
Income.Expense Income.Fifth Amount
Market Income Bottom Fifth 1,196.70
Disposable Income Bottom Fifth 961.30
Consumer Spending Bottom Fifth 1,260.50
Market Income Second Fifth 2,545.40
Disposable Income Second Fifth 2,113.90
Consumer Spending Second Fifth 1,864.60
Market Income Third Fifth 3,512.50
Disposable Income Third Fifth 2,914.90
Consumer Spending Third Fifth 2,325.10
Market Income Fourth Fifth 4,621.30
Disposable Income Fourth Fifth 3,762.90
Consumer Spending Fourth Fifth 2,791.70
Market Income Top Fifth 7,537.00
Disposable Income Top Fifth 6,144.20
Consumer Spending Top Fifth 3,654.60

ggplots step by step

library(ggplot2)
# source("./theme_kr_HCR.R")
gg.title <- "Income and Consumer Expenditure for each Fifth (2011 4/4)"
x.lab <- "Income Fifth"
y.lab <- "Amount in Thousand Won"
Income.Amount <- hh.econ.melt[hh.econ.melt$Income.Expense == "Market Income", "Amount"]
# (g1 <- ggplot(hh.econ.melt, aes(x = Income.Fifth, y = Amount, fill = Income.Expense, colour = Income.Expense, group = Income.Expense)) + 
#  geom_bar(stat = "identity"))
# (g1 <- ggplot(hh.econ.melt, aes(x = Income.Fifth, y = Amount, fill = Income.Expense, colour = Income.Expense, group = Income.Expense)) + 
#  geom_bar(stat = "identity", position = "dodge"))
(g1 <- ggplot(hh.econ.melt, aes(x = Income.Fifth, y = Amount, fill = Income.Expense)) + 
  geom_bar(stat = "identity"))

(g1 <- ggplot(hh.econ.melt, aes(x = Income.Fifth, y = Amount, fill = Income.Expense)) + 
  geom_bar(stat = "identity", position = "dodge"))

(g2 <- g1 + 
  theme_bw())

(g3.1 <- g2 + 
  geom_line(aes(group = Income.Expense, linetype = Income.Expense), position = position_dodge(width = 1.0), size = 1, show.legend = FALSE))

#  geom_line(aes(group = Income.Expense, linetype = Income.Expense), position = position_dodge(width = 1.0), size = 1))
(g3.2 <- g3.1 + 
  geom_point(aes(group = Income.Expense, shape = Income.Expense), position = position_dodge(width = 1.0), size = 3, stroke = 1.2, show.legend = FALSE))

#  geom_point(aes(group = Income.Expense, shape = Income.Expense), position = position_dodge(width = 1.0), size = 3, stroke = 1.2))
(g4 <- g3.2 + 
  scale_y_continuous(breaks = Income.Amount, labels = format(Income.Amount, digits = 1, nsmall = 1, big.mark = ",")) + 
  scale_linetype_manual(values = c("blank", "solid", "solid")) +
  scale_shape_manual(values = c(NA, 4, 4)) +
  labs(title = gg.title, x = x.lab, y = y.lab, fill = "Income or Expenses", linetype = "Income or Expenses", shape = "Income or Expenses"))

(g5 <- g4 + theme(legend.position = c(0.2, 0.8)))

(g6 <- g5 + scale_fill_grey(start = 1/6, end = 5/6) + 
  scale_colour_grey(start = 1/6, end = 5/6))

(g7 <- g5 + 
  scale_fill_manual(values = c("blue", "cyan", "red")))  

(g8 <- g7 + 
#  guides(linetype = "none", shape = "none") +
  theme(legend.key = element_blank()))

save.image("./hh_econ_ggplot.RData")