Skip to content

Instantly share code, notes, and snippets.

@mrchypark
Created September 1, 2017 01:20
Show Gist options
  • Save mrchypark/0289df018ba02cf6bc3337b62a1f60f5 to your computer and use it in GitHub Desktop.
Save mrchypark/0289df018ba02cf6bc3337b62a1f60f5 to your computer and use it in GitHub Desktop.
library(dplyr)
library(ggplot2)
# showtext는 윈도우에서 출력시 폰트 문제가 발생하는걸 고칠 수 있게 도와줍니다.
# https://github.com/yixuan/showtext
library(showtext)
font.add.google("Noto Sans")
# q1에서 차용했습니다. 유용한 코드 공유 감사합니다!
format.percent <- function(x) paste(floor(x),'%',se1p='')
# 원 데이터 생성
set.seed(1986)
raw.data <-
data.frame(
product = sample(rep(LETTERS[c(1:4)],c(30,40,30,20))))%>%
mutate(
making.year = sample(rep(2017:2014,c(30,40,30,20))),
repair.year = making.year+sample(1:4)) %>%
arrange(product, making.year, repair.year)
# 결과 코드
raw.data %>%
# 생산 연도별 생산량 계산
group_by(product, making.year) %>%
mutate(make.num = n()) %>%
# 비율과 출력용 컬럼 작성
group_by(
product
, making.year
, repair.year
) %>%
mutate(
rep.rate = round(n()/make.num*100)
, rate.lib = paste0(rep.rate,"%")
, prod.lib = paste0(product,"제품")
) %>%
# plot 시작
ggplot(aes(x= making.year, y=repair.year)) +
geom_tile(aes(fill=rep.rate)) +
geom_text(aes(label=rate.lib)) +
facet_wrap(~prod.lib, ncol=4) +
scale_fill_gradient2(high="red"
, limits = c(0,100)
, labels = format.percent
) +
scale_y_continuous(breaks = 2015:2021) +
ggtitle("제품별 A/S 비율") +
labs(x="생산년도"
, y="A/S연도"
, fill="A/S비율\n") +
theme_linedraw() +
theme(panel.grid.major = element_blank()
, panel.grid.minor = element_blank()
, panel.background = element_rect(fill = "white")
, strip.background = element_rect(fill = "gray")
, strip.text = element_text(colour = "black")
, title = element_text(family="Noto Sans")
, axis.title.y = element_text(angle=0)
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment