티스토리 뷰

R

[R 정리] 시각화

Aaron 2019. 1. 29. 19:54
반응형

시각화 





#. plot 차트 주 옵션

xlim = c(0, 10) : x축 범주(눈금) (limit)


ylim = c(0, 10) : y축 범주(눈금)


type = ' ' : 그래프 타입

plot(x1, type = 'o')

plot(x1, type = 'l')

plot(x1, type = 'b')

...


lty = ' ' : 그래프 선 모양 (line type)

 plot(x1, type = 'o', lty=0) # lty="blank" 

 plot(x1, type = 'o', lty=1) # lty="solid" 

 plot(x1, type = 'o', lty=2) # lty="dashed" 

 plot(x1, type = 'o', lty=3) # lty="dotted" 

 plot(x1, type = 'o', lty=4) # lty="dotdash" 

 plot(x1, type = 'o', lty=5) # lty="longdash" 

 plot(x1, type = 'o', lty=6) # lty="twodash" 


col = ' ' : 그래프 색상

col = '숫자' or  '색상명(Eng)'

plot(x1, type = 'l', col = 1) # col = 'black'

plot(x1, type = 'l', col = 2) # col = 'red'

plot(x1, type = 'l', col = 3) # col = 'green'

plot(x1, type = 'l', col = 4) # col = 'blue'

....


xlab = '  ' : x축 범주 제목 (label)


ylab = '  '  : y축 범주 제목


main = '  ' : 차트 제목


ann = T or F : 초기 그래프 출력 시, 축 제목 지정 여부

# ann = F 일 경우, title() 함수로 축 제목 설정 가능 

plot(x1, type='o', ann = F)

title(main="Plot", col.main="red", font.main=4)     # font.main : 폰트

title(xlab="x축", col.lab="black") 

title(ylab="y축", col.lab="blue")


axes = T or F : 초기 그래프 출력 시, 축 범주 지정 여부

# axes = F 일 경우, axis() 함수로 축 범주 설정 가능

plot(x1, type='o', axes = F, ylim = c(0,10))

axis(1, at = 1:5, lab=c('A','B','C','D','E'))     # x축(1) 범주 사용자 지정 설정

axis(2)                                             # y축(2) 범주 사용자 지정 설정 





#. axis와 title 함수를 사용한 축 설정 및 제목 설정 

x1 <- c(1,3,7,5,10)

plot(x1,    

     axes = F,    # 축 범주 지정 False

     ann = F,     # 축 제목 지정 False

     ylim = c(0,15), col = 'red',  type = 'b')  

 

axis(1, at = 1:5, lab=c('A','B','C','D','E'))    # axes=F 에 대한 설정

axis(2, ylim = c(0,15))


title(main="Plot", col.main="red", font.main=4, xlab="x축", ylab="y축")   # ann=F 에 대한 설정




#. 여러 Plot 차트 그리기 (lines)

 v1 <- c(1,2,3,4,5)

 v2 <- c(2,4,6,8,10)


 plot(v1, type = 'o', col = 'red', ylim = c(1,10))    # 첫 번째로 그려진 그래프의 눈금으로 틀이 고정

 lines(v2, type = 'o', col = 'blue')




#. barplot 주 옵션

# data frame을 barplot에 적용 시 matrix 형식으로 적용

m <- matrix(c(4,6,7,3),nrow = 2)


beside = F or T : 그룹별로 묶어서 출력할지 여부(default = F)

barplot(m,  # data

          names = c('a','b'),   # bar name

          beside = T,           # T = 그룹별 묶어서(여러 barplot), F = 그룹별(하나의 barplot)

          col = c(2,3))          # color


x <- c(1,3,5,6,9)


horiz = F or T : 그래프를 수평으로 출력할지 여부(horizon)

barplot(x)   # default(세로)

barplot(x, horiz = T)  가로로





#. 범례 설정

legend(x = 2, y = 240,          # x, y 좌표

         legend = result$업종,    # 범주

         cex = 0.8,      # 글자 크기

         pch = 1,        # 범례 크기  

         col = 1:6,       # 색상(plot)

         fill =  rainbow(nrow(result))   # 색 채우기(barplot)

         lty = 1)         # line type




#. pie 차트 label 설정 방법

# pie plot

v1 <- c(1,2,3,4)

pct <- round(v1/sum(v1) * 100,1)


lab1 <- c('Day 1','Day 2','Day 3','Day 4')

lab2 <- paste(lab1,"\n",pct," %")

pie(v1,                                # data

    radius=1,                        # pie 반지름 크기

    init.angle=90,                  # pie 시작 각도

    col=rainbow(length(v1)),    # pie 색상

    label=lab2)                    # pie label


# pie3D plot

label <- paste(names(vsum), '\n', rate, '%', sep = '')

library(plotrix)

pie3D(rate, 

        main = '항목별 지출비율', 

        col = rainbow(length(rate)),

        labelcex = 1,      # pie label 크기

        radius = 0.8,      # pie 크기

        labels = label,    # pie label

        explode = 0.05)  # pie간 간격

legend(0.3, 1, names(vsum), cex = 0.7, fill = rainbow(length(rate)))




#. 파레트 옵션

# 파레트에 여러 그래프 출력 c(행의 개수, 열의 개수)

par(mfrow=c(3,3))    


# Rstudio figure(파레트) reset

plot.new()  


# 분리된 figure(파레트) 창 생성

dev.new()  


# 범주, 축 제목 위치 설정

par(mgp=c(3,2,1))         # par(mgp=c(제목위치,지표값위치,지표선위치)) 


# 그래프 외부 여백 설정

par(oma=c(0,2,0,0))       # par(oma=c(하,좌,상,우))  

 


 

#. 데이터 적용 예

# barplot

barplot(as.matrix(result[,c(5,8,4,3,2,7,6)]/10000), beside = T, col = rainbow(nrow(result)), ylim = c(0,10))    

legend(1, 10, result$업종, fill =  rainbow(nrow(result)), cex = 0.8)

title(main = '요일별 업종의 통화건수', xlab = '요일', ylab = '통화건수(단위 : 만)')


# plot

plot(result[,2]/10000, type = 'o', col = 1, ann = F, axes = F, ylim = c(0,80))

lines(result[,3]/10000, type = 'o', col = 2)

lines(result[,4]/10000, type = 'o', col = 3)

axis(1, at = 1:4, labels = result[,1]) 

axis(2, ylim = c(0,80))

title(main = '년도별 제품의 판매량액', xlab = '년도', ylab = '판매량(단위 : 만)')

legend(1, 80, colnames(result)[-1], col = 1:3, lty = 1, cex = 1)

#

plot(t(df1)[,1], type = 'o', col = 1, ylim = c(0,3500), ann = F, axes = F)

for(i in 2:nrow(df1)){

     lines(t(df1)[,i], type = 'o', col = i)   

   }

ax_name <- substr(colnames(df1),2,5)  

axis(1, at = 1:length(ax_name), labels = ax_name) 

axis(2, ylim = c(0,3500))  

title(main = '근로자별 월 급여액', xlab = '년도', ylab = '급여액')')

legend(1, 3500, emp$고용형태[-1], col = 1:nrow(df1), lty = 1, cex = 0.6)


histogram plot 

hist(height,

     breaks = c(170, 175, 180, 185, 190, 195),   # 구간의 분기

     include.lowest = T,        # 최소값 포함 여부

     col = rainbow(5),  

     right = F,                    # 오른쪽 닫힘 여부 

     main = "Histogram 2")




반응형
댓글
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday