Skip to content

Instantly share code, notes, and snippets.

@woons
Created June 23, 2018 00:54
Show Gist options
  • Save woons/9aad8044089b0f382cb8969489d3bfd8 to your computer and use it in GitHub Desktop.
Save woons/9aad8044089b0f382cb8969489d3bfd8 to your computer and use it in GitHub Desktop.
multiple_waffle _chart_r
################################
# 와플차트 만들기 메뉴얼
################################
library(tidyverse)
df <- read_csv("https://docs.google.com/spreadsheets/d/e/2PACX-1vSoQVUDDcKYstVCn5l6h4JJmtoWWb7NmxaJFNCL-u3rUBaAqKfLO6cPPEAwWDOD1ZfIC8UOv31cgZAn/pub?gid=0&single=true&output=csv")
# 사각형 칼러 메뉴얼 지정
pallete <- c("#E53D36", "#FFA644", "#998A2F", "#295949", "#002D40",
"#F266C1", "#8B56BF", "#716DF2", "#91C5F2", "#A2F2EA",
"#BF3D6A", "#0569A6", "#278065", "#F4AB29", "#F13926",
"#2E112D", "#540032", "#820333", "#C9283E", "#F0433A",
"#024959", "#F2C777", "#F24638", "#FFDD5C", "#76A665",
"#227066", "#265961", "#BEDB39", "#FD7400", "#FFE11A")
# 함수 설정
squarePie <- function(pct, col.grid="#e0e0e0", col.border="black") {
# 첫번째 변수 텍스트 반영
text <- df$category[pct]
# 랜덤 컬러 추출
col <- sample(pallete, 1)
# 값 반영
pct <- df$value[pct]
# 좌표계 설정 범위
x_row <- 1:30
y_col <- 1:30
# 정확한 좌표계 지정
x <- rep(x_row, 30)
y <- rep(y_col, each=30)
# 칼러 설정
fill_col <- c(rep(col, pct), rep("#ffffff", 900-pct))
# 시각화
plot(0, 0, type="n", xlab="", ylab="", main=text, xlim=c(0,31), ylim=c(0,31), asp=1, bty="n", axes=FALSE)
symbols(x, y, asp=1, squares=rep(1, 900), inches=FALSE, add=TRUE, bg=fill_col, fg=col.grid, lwd=0.5)
rect(.5, .5, 30.5, 30.5, lwd=1, border=col.border)
}
# 34개 멀티플 차트 제작을 위한 그리드 설정
par(mfrow=c(3,5), mar=c(1, 0.5, 3, 0.5))
# 그리기 시작 plot에서 페이지 넘어가는 거 확인
for(i in seq_along(df$category)){
squarePie(i)
}
## finished ##
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment