ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • R을 이용한 데이터시각화
    Statistics,Visualization/R 2021. 10. 16. 18:37
    데이터를 시각화하는 도구는 상당히 많이 존재합니다.
    그 중에서 다양한 패키지와 기능을 포함한 R을 통한 데이터 시각화 과정을 살펴보겠습니다.
    ggplot2를 활용

    1. 막대그래프 

    #ggplot2
    #데이터 불러오기 
    library(ggplot2)
    DIR="/Users/minyoung_jo/Desktop/R_data/archive/audi.csv"
    Audi=read.csv("/Users/minyoung_jo/Desktop/R_data/archive/audi.csv")
    
    #빈 배경의 도화지 생성
    ggplot()
    
    #x축설정,geom_bar로 막대그래프생성
    #theme_classic으로 배경을 깔끔하게 설정
    ggplot(Audi,aes(x=year)) +
      geom_bar() + 
    	theme_classic()
    
    #글꼴수정
    theme(axis.text.x=element_text(size=7,face="bold"),
            axis.text.y=element_text(size=8,face="bold"),
            axis.title.x=element_text(size=8,face="bold"),
            axis.title.y=element_text(size=7,face="bold")) 
    
    #글꼴수정
    	theme(text=element_text(size = 7,face="bold"))
    
    #그래프 축 조정
    #breaks는 축의 간격을 설정
    #expand는 x축,y축 그래프사이의 공백을 조절 
      scale_x_continuous(breaks=seq(1990,2020,by=2),
                         expand = c(0,0))+
      scale_y_continuous(breaks=seq(0,4000,by=500),
                         expand = c(0,0))
    #그래프 색상 조정
    ggplot(Audi,aes(x= year, fill=transmission)) +
    
    #그래프 범례 조정
    ggplot(Audi,aes(x=year,fill=transmission)) +
      geom_bar() + 
    	theme_classic() +
    	theme(legend.position="bottom")

    2.히스토그램

    #geom_histogramg활용하기
    #geom_histogram은 데이터를 30등분하여 표현(bins=n으로 설정)
    ggplot(Audi,aes(x=price)) +
      geom_histogram(bins=100)+
      theme_classic()
    
    #geom_histogram 색, 축조정 
    ggplot(Audi,aes(x=price,fill=transmission)) +
      geom_histogram(bins=100)+
      theme_classic()+
    	scale_x_continuous(expand=c(0,0))+
    	scale_y_continuous(expand=c(0,0))

    3.산점도

    #geom_point 산점도
    ggplot(Audi,aes(x=mpg,y=price))+
     geom_point()+
     theme_classic()
    
    #geom_point 산점도 색 수정 
    ggplot(Audi,aes(x=mpg,y=price,col=fuelType))+
     geom_point()+
     theme_classic()

    4.회귀선

    #회귀선추가 _곡선
    ggplot(Audi,aes(x=mpg,y=price))+
      geom_point() +
      geom_smooth() +
      theme_classic()
    
    #회귀선추가 _직선
    ggplot(Audi,aes(x=mpg,y=price))+
      geom_point() +
      geom_smooth(method='lm') +
      theme_classic()

    5. 박스플롯

    #박스플롯
    ggplot(Audi,aes(x=fuelType,y=price))+
      geom_boxplot(outlier.color="red") +
      theme_classic()
    
    #박스플롯 색 지정
    ggplot(Audi,aes(x=fuelType,y=price,fill=transmission))+
      geom_boxplot(outlier.color="red") +
      theme_classic()

     

    가장 대표적으로 시각화에 자주 사용하기는 도표들을 넣어보았습니다. 

    댓글

my_log