methods("plot")
## [1] plot.acf* plot.data.frame* plot.decomposed.ts*
## [4] plot.default plot.dendrogram* plot.density
## [7] plot.ecdf plot.factor* plot.formula*
## [10] plot.function plot.hclust* plot.histogram*
## [13] plot.HoltWinters* plot.isoreg* plot.lm
## [16] plot.medpolish* plot.mlm plot.ppr*
## [19] plot.prcomp* plot.princomp* plot.profile.nls*
## [22] plot.spec plot.stepfun plot.stl*
## [25] plot.table* plot.ts plot.tskernel*
## [28] plot.TukeyHSD
##
## Non-visible functions are asterisked
예를 들어, plot.lm은 lm이라는 클래스에 정의된 plot방법으로서 plot(lm 객체)와 같은 방식으로 호출하면 자동으로 lm 클래스의 plot이 불려지게 된다.
mlbench패키지에 있는 Ozone데이터를 사용해 산점도를 그려보자.
install.packages("mlbench")
## Installing package into 'C:/Users/Byeongsoo/Documents/R/win-library/3.0'
## (as 'lib' is unspecified)
## Error: trying to use CRAN without setting a mirror
library(mlbench)
data(Ozone)
data()문은 Ozone데이터 셋을 읽어들이기 위한 명령문이다. 데이터에 대한 정보는 ?Ozone이나 help(Ozone)으로 볼 수 있다.
mlbench에 포함된 모든 데이터 셋을 보고 싶을 때는 library(help = “mlbench”)로 볼 수 있다.
이제 산점도를 그려보자.
plot(Ozone$V8, Ozone$V9)
plot은 plot(x, y)의 순서로 입력 받으며, x와 y가 숫자형 데이터일 경우에 산점도를 그려준다.
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature")
ggplot에서는 다음과 같이 표현 할 수 있다.
library(ggplot2)
ggplot(Ozone, aes(V8, V9)) + geom_point() + xlab("Sandburg Temperature") + ylab("EI Monte Temperature")
## Warning: Removed 140 rows containing missing values (geom_point).
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone")
ggplot2에서는 다음과 같다.
ggplot(Ozone, aes(V8, V9)) + geom_point() + xlab("Sandburg Temperature") + ylab("EI Monte Temperature") +
ggtitle("Ozone")
## Warning: Removed 140 rows containing missing values (geom_point).
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone", pch = 20)
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone", pch = "+")
ggplot에서는 다음과 같다.
ggplot(Ozone, aes(V8, V9)) + geom_point(shape = "X") + xlab("Sandburg Temperature") +
ylab("EI Monte Temperature") + ggtitle("Ozone")
## Warning: Removed 140 rows containing missing values (geom_point).
geom_point()에서 설정을 한다.
만약 그래프 안에 수준이 2개 이상이라면 scale_shape_manual(values=c(1,2)) 레이어를 입힌다. 이 경우 ○와 △을 점의 모양으로 사용한다.
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone", cex = 0.1)
ggplot에서는 다음과 같다.
ggplot(Ozone, aes(V8, V9)) + geom_point(size = 0.1) + xlab("Sandburg Temperature") +
ylab("EI Monte Temperature") + ggtitle("Ozone")
## Warning: Removed 140 rows containing missing values (geom_point).
geom_point에서 size=0.1로 준다.
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone", col = "#FF0000")
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone", col = "red")
ggplot에서는 다음과 같다.
ggplot(Ozone, aes(V8, V9)) + geom_point(colour = "#FF0000") + xlab("Sandburg Temperature") +
ylab("EI Monte Temperature") + ggtitle("Ozone")
## Warning: Removed 140 rows containing missing values (geom_point).
geom_point에서 colour(혹은 color)로 입력한다.
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone")
max(Ozone$V8)
## [1] NA
max(Ozone$V8, na.rm = TRUE)
## [1] 93
max(Ozone$V9, na.rm = TRUE)
## [1] 82.58
NA가 존재하므로, 최대값을 구할 때, na.rm=TRUE를 사용한다.
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone", xlim = c(0, 100), ylim = c(0, 90))
최대값을 확인한 후, 적절한 값을 선택한다.
ggolot에서는 다음과 같다.
ggplot(Ozone, aes(V8, V9)) + geom_point(colour = "#FF0000") + xlab("Sandburg Temperature") +
ylab("EI Monte Temperature") + ggtitle("Ozone") + xlim(0, 100) + ylim(0,
90)
## Warning: Removed 140 rows containing missing values (geom_point).
레이어를 입히는 식으로 xlim과 ylim을 사용한다.
data(cars)
str(cars)
## 'data.frame': 50 obs. of 2 variables:
## $ speed: num 4 4 7 7 8 9 10 10 10 11 ...
## $ dist : num 2 10 4 22 16 10 18 26 34 17 ...
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
plot(cars)
데이터를 점으로 표시하는 것보다 선으로 표시하는 것도 괜찮을 것 같다고 생각된다.
plot(cars, type = "l")
이 선에 관찰된 점들을 중첩하여 그린다.
plot(cars, type = "o", cex = 0.5)
cex는 점의 크기를 조절하는 인자임을 위에서 배웠다.
위 그래프를 보면 같은 speed에 대해 두개 이상의 dist가 있는 경우가 많아 어색해 보인다. 이 문제를 해결하기 위해 tapply를 사용해보자. tapply는 "그룹별 처리를 위한 apply 함수로서 tapply(데이터, 색인, 함수)의 형태로 호출한다. 여기서 ‘색인’은 데이터가 어느 그룹에 속하는지를 표현하기 위한 factor형 데이터이다.”
각 speed마다 평균dist를 tapply를 사용해 계산한다음, 이를 plot()하면 된다.
str(tapply(cars$dist, cars$speed, mean))
## num [1:19(1d)] 6 13 16 10 26 ...
## - attr(*, "dimnames")=List of 1
## ..$ : chr [1:19] "4" "7" "8" "9" ...
plot(tapply(cars$dist, cars$speed, mean), type = "o", cex = 0.5, xlab = "speed",
ylab = "dist", main = "주행속도별 평균 제동거리")
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ec>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <a3>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <bc>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ed>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <96>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <89>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ec>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <86>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <8d>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <eb>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <8f>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <84>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <eb>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <b3>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <84>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ed>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <8f>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <89>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ea>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <b7>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <a0>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ec>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <a0>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <9c>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <eb>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <8f>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <99>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ea>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <b1>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <b0>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <eb>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <a6>
## Warning: conversion failure on '二쇳뻾攼㹣냽攼㹢룄蹂㠼㸴 攼㹤룊洹愼㸰 攼㹣젣攼㹢룞嫄곕━' in 'mbcsToSbcs': dot substituted for <ac>
ggplot에서의 그래프 타입은 다음과 같이 설정할 수 있다.
ggplot(cars, aes(speed, dist)) + geom_point() # 점
ggplot(cars, aes(speed, dist)) + geom_line() # 선
ggplot(cars, aes(speed, dist)) + geom_line() + geom_point() # 점에 선들을 중첩.
opar <- par(mfrow = (c(1, 2)))
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone")
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone")
par(opar)
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone")
plot(Ozone$V8, Ozone$V9, xlab = "Sandburg Temperature", ylab = "EI Monte Temperature",
main = "Ozone")
opar에는 전에 지정한 설정이 저장된다.
par(opar)를 수행함으로써 mfrow지정 이전의 par설정으로 되돌렸다. 이런 설정을 하지 않는다면 계속해서 1행2열로 그려지게 된다.
head(Ozone)
## V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12 V13
## 1 1 1 4 3 5480 8 20 NA NA 5000 -15 30.56 200
## 2 1 2 5 3 5660 6 NA 38 NA NA -14 NA 300
## 3 1 3 6 3 5710 4 28 40 NA 2693 -25 47.66 250
## 4 1 4 7 5 5700 3 37 45 NA 590 -24 55.04 100
## 5 1 5 1 5 5760 3 51 54 45.32 1450 25 57.02 60
## 6 1 6 2 6 5720 4 69 35 49.64 1568 15 53.78 60
opar <- par(mfrow = c(1, 2))
plot(Ozone$V6, Ozone$V7, xlab = "Windspeed", ylab = "Humidity", main = "Ozone",
pch = 20, cex = 0.5)
plot(jitter(Ozone$V6), jitter(Ozone$V7), xlab = "Windspeed", ylab = "Humidity",
main = "Ozone", pch = 20, cex = 0.5)
par(opar)
jitter를 사용한 경우 점이 많이 몰려있는 위치를 파악할 수 있다.