1 R 그래프를 위한 기본함수

  1. 고수준 함수: plot(), barplot(), boxplot(), hist(), pie(), persp()
  2. 저수준함수
    1. 점그리기: points()
    2. 선그리기: lines(), abline(), arrows()
    3. 문자출력: text()
    4. 도형: rect(), ploygon()
    5. 좌표축: axis()
    6. 격자표현: grid()

1.1 plot() 함수

  • plot

    x <- rnorm(100, sd=2); y <- 0.3 + 2*x + rnorm(100, sd=1)
    plot(x)

  • 심볼 모양 : plot(, pch=)

    심볼 모양

    심볼 모양

  • 선의 종류 : * plot(..., lty=)

  • 산점도 + 텍스트 추가
    • Animals data: Average brain and body weights for 28 species of land animals.

      library(MASS)
      head(Animals)
    plot(x=Animals$body , y=Animals$brain, pch=16, col="blue",
     xlab="Body weight (kg)", ylab="Brain weight (g)", xlim=c(0,250), ylim=c(0, 1400))
    text(x=Animals$body, y=Animals$brain, labels=row.names(Animals), pos=4)
  • 그림 범례(legend) 표시

    plot(~Sepal.Length+Sepal.Width, data=iris, pch=rep(15:17, each=50), 
         col=c("red", "blue", "green")[iris$Species], cex=1.5)
    legend("topright", legend = levels(iris$Species), pch=15:17, 
           col=c("red", "blue", "green"), cex=1.2, bty="n")

1.2 막대그래프: barplot 함수

pie.sales <- c(0.12, 0.3, 0.26, 0.16, 0.04, 0.12)
names(pie.sales) <- c("Blueberry", "Cherry", "Apple", "Boston Cream",
"Other", "Vanilla Cream")
barplot(pie.sales, las=2) #las=2: x-tick 

counts <- table(mtcars$vs, mtcars$gear)
barplot(counts, main="Car Distribution by Gears and VS",
  xlab="Number of Gears", col=c("darkblue","red"),
    legend = rownames(counts), beside=TRUE)