[R] 연습

R 함수 참고용

Simon Yoon 2022. 9. 9. 23:15
  • ggplot x축 라벨 각도를 90도 돌리기 (링크)
    theme(axis.text.x=element_text(angle=90, hjust=1))
    
    ggplot(Cars93, aes(x=Type, y=MPG.highway)) +
      geom_boxplot() +
      theme_bw() +
      theme(axis.text.x=element_text(angle=90, hjust=1))
  • 맥에서 ggplot 한글 깨질때 해결법 (링크)
    ## 폰트 설정 패키지 설치
    install.packages("extrafont")
    library(extrafont)
    
    ## 존재하는 모든 폰트 불러오기
    font_import()
    
    ## 폰트 설정
    theme_set(theme_gray(base_family='NanumGothic'))
    ## 혹은
    theme_set(theme_gray(base_family='AppleMyungjo'))
  • 형변환 (링크)
    as.character()
    mode(x) <- "numeric"  # mode()를 사용해서도 가능하다
  • ggplot 이중축 그래프에서 축과 축 이름 색 바꾸기 (링크)
    ggplot(~~) + 
      geom_line(~~) +
      theme(axis.title.y.left = element_text(color=cols[1]),
            axis.text.y.left = element_text(color=cols[1]),
            axis.title.y.right = element_text(color=cols[2]),
            axis.text.y.right = element_text(color=cols[2]))
  • ggplot 그래프 제목, 축 제목 설정하기 (링크)
    #그래프 제목 및 축제목 설정하기1 (ggtitle(), xlab(), ylab() 함수 이용
    > p + ggtitle("Plot of length \n by dose") +
    +   xlab("Dose (mg)") + ylab("Teeth length")
    
    #그래프 제목 및 축제목 설정하기2 (labs() 함수 이용)
    > p +labs(title="Plot of length \n by dose",
            x ="Dose (mg)", y = "Teeth length")
  • ggplot 이중축 그래프 Y축 비율 맞추기 (링크)
    max_ratio <- max(employees$total)/max(employees$employees.edu)
    max_ratio
    
    ggplot(employees, aes(x = time)) + 
      geom_line(aes(y = total, color = '전체 취업자')) + 
      geom_line(aes(y = max_ratio * employees.edu, color = '교육분야 취업자'))
  • 내용 합치기 (링크)
    paste(x, y, z, sep = "-")


Uploaded by N2T