plot(iris$Sepal.Width, iris$Sepal.Length, cex = 0.7, pch = 20, xlab = "width",
ylab = "length", main = "iris")
points(iris$Petal.Width, iris$Petal.Length, cex = 0.7, pch = "+", col = "#FF0000")
attach()는 인자로 주어진 데이터 프레임이나 리스트를 곧바로 접근할 수 있게 해준다.
attach(iris)
plot(Sepal.Width, Sepal.Length, cex = 0.5, pch = 20, xlab = "width", ylab = "length",
main = "iris")
points(Petal.Width, Petal.Length, cex = 0.5, pch = "+", col = "#FF0000")
attach()를 해제하려면 detach()를 사용한다.
attach()를 한 데이터를 detach()하지 않을 경우 Sepal.Length등이 계속 접근 가능하게 남아있게 되는 단점이 있다.
이들 데이터를 다시 사용하지 않을 계획이라면, with(), within()을 사용한다.
with(iris, {
plot(Sepal.Width, Sepal.Length, cex = 0.5, pch = 20, xlab = "width", ylab = "length",
main = "iris")
points(Petal.Width, Petal.Length, cex = 0.5, pch = "+", col = "#FF0000")
})
with()를 통해 데이터 안의(with) 필드에 바로 접근할 수 있다.
within()도 with()와 비슷하지만 데이터를 수정하는 데 사용한다.
어떤 경우에는 plot()문에 사용할 데이터가 없다거나, points()명령을 수행할 시점에 표시할 데이터가 준비될 경우가 있다. 이럴때는 type=“n"을 사용하여 plot()을 수행한다.
with(iris, {
plot(NULL, xlim = c(0, 5), ylim = c(0, 10), xlab = "width", ylab = "length",
main = "iris", type = "n")
points(Sepal.Width, Sepal.Length, cex = 0.5, pch = 20)
points(Petal.Width, Petal.Length, cex = 0.5, pch = "+", col = "#FF0000")
})
이처럼 plot()에 type="n"을 사용하여 점진적인 방법으로 그래프를 그려나갈 수 있다.
그러나 xlim과 ylim을 적절하게 설정해줘야하는 번거로움이 있다.
x <- seq(0, 2 * pi, 0.1) # [0, 2π]까지 0.1간격으로 데이터 생성
y <- sin(x)
plot(x, y, cex = 0.5, col = "red")
lines(x, y)
sin그래프를 그리는 예이다.
cars데이터에 대해 LOWESS를 수행해보자. LOWESS는 linear model이나 quadratic modle을 적합하되, 각 점에서 가까운 데이터에 많은 가중치를 주면서 regression을 수행한다. 가중회귀라고 볼 수 있다.
library(mlbench)
data(cars)
head(cars)
## speed dist
## 1 4 2
## 2 4 10
## 3 7 4
## 4 7 22
## 5 8 16
## 6 9 10
plot(cars)
lines(lowess(cars))
이외에도 loess(), ksmooth(), smooth.spline(), earth()등의 비모수적 회귀 방법을 제공한다.
다음과 같은 명령문을 통해 선형회귀직선에 대해 생각해보자.
plot(cars, xlim = c(0, 25))
abline(a = -5, b = 3.5, col = "red")
cars데이터가 dist= -5 + 3.5 X _speed_로 근사될 수 있다고 가정했을 때의 그래프이다.
plot(cars, xlim = c(0, 25))
abline(a = -5, b = 3.5, col = "red")
abline(h = mean(cars$dist), lty = 2, col = "blue")
abline(v = mean(cars$speed), lty = 2, col = "green")
그래프에 평균까지 표시해보았다. lty=2는 선의 유형을 지정하는데 사용되며, 2는 dashed line을 뜻한다.
그래프를 그려본 결과, dist= -5 + 3.5 X _speed_는 평균이 교차하는 점을 지나지 않는다. 따라서 이 직선은 올바른 선형회귀직선이 아니다.
ggplot의 경우는 다음과 같다.
library(ggplot2)
ggplot(cars, aes(speed, dist), xlim(0, 25)) + geom_point(shape = "O", size = 1) +
geom_hline(aes(yintercept = mean(cars$dist)), colour = "blue", linetype = "longdash") +
geom_vline(aes(xintercept = mean(cars$speed)), colour = "green", linetype = "longdash") +
geom_abline(slope = 3.5, intercept = -5, colour = "red") + theme(panel.background = element_blank(),
axis.line = element_line(colour = "black"), panel.grid = element_blank(),
panel.border = element_rect(colour = "black", fill = NA))
curve(sin, 0, 2 * pi)
선형회귀에 대한 예를 살펴보자.
선형회귀는 lm(종속변수 ~ 독립변수, data)의 형식을 취한다.
m <- lm(dist ~ speed, data = cars)
m
##
## Call:
## lm(formula = dist ~ speed, data = cars)
##
## Coefficients:
## (Intercept) speed
## -17.58 3.93
lm()으로 만든 모델을 abline()에 넘겨주는 것만으로 그래프를 표시할 수 있다.
plot(cars)
abline(m)
예측은 predict()로 수행한다. interval="confidence"를 지정하면 된다.
p <- predict(m, interval = "confidence")
head(p)
## fit lwr upr
## 1 -1.849 -12.330 8.631
## 2 -1.849 -12.330 8.631
## 3 9.948 1.679 18.217
## 4 9.948 1.679 18.217
## 5 13.880 6.308 21.453
## 6 17.813 10.905 24.720
p의 각행은 cars의 각 행에 대응된다.
head(merge(cars, p))
## speed dist fit lwr upr
## 1 4 2 -1.849 -12.33 8.631
## 2 4 10 -1.849 -12.33 8.631
## 3 7 4 -1.849 -12.33 8.631
## 4 7 22 -1.849 -12.33 8.631
## 5 8 16 -1.849 -12.33 8.631
## 6 9 10 -1.849 -12.33 8.631
이제 본격적으로 신뢰구간을 그래프로 표현해보자. 그래프를 그리려면 다각형의 x좌표, y좌표를 구해야 한다.
cars의 speed를 x좌표, p의 lwr와 upr을 각각 y좌표로 하여 신뢰구간을 구할 수 있다.
*다만, 닫혀있는 다각형을 그려야하므로 시작점과 끝점이 만나야 한다.
x <- c(cars$speed, tail(cars$speed, 1), rev(cars$speed), cars$speed[1])
y <- c(p[, "lwr"], tail(p[, "upr"], 1), rev(p[, "upr"]), p[, "lwr"][1])
plot(cars)
abline(m)
polygon(x, y, col = rgb(0.7, 0.7, 0.7, 0.5))
col = “grey"를 하면 polygon()호출 이전에 그래프가 모두 가려져 버리므로, rgb()함수에 alpha값을 지정해 투명한 색을 지정하면 된다.
polygon을 여러번 수행하면 계속 그래프가 덮어씌여져서 점점 가려지는 현상이 나타난다..
ggplot에서는 smooth()를 사용하면 다음과 같이 더 쉽게 그릴수 있다.
코드 출처는 http://rstudio-pubs-static.s3.amazonaws.com/1842_1b7f9146f6ba45629cc9e1cac536a5bd.html이다
ggplot(cars, aes(speed, dist)) +
geom_point(shape=1) + # ○(shape=1)을 점의 모양으로 사용
geom_smooth(method=lm) # 회귀직선을 함께 보여줌
# lm의 초기값은 회귀직선과 95% 신뢰구간이 함께 보여짐
그 밖에 smooth를 이용한 회귀분석 및 그래프이다.
ggplot(cars, aes(speed, dist)) +
geom_point(shape=1) # ○(shape=1)을 점의 모양으로 사용
ggplot(cars, aes(speed, dist)) +
geom_point(shape=1) + # ○(shape=1)을 점의 모양으로 사용
geom_smooth(method=lm, # 회귀직선을 함께 보여줌
se=FALSE) # 신뢰구간을 보여주지 않음
ggplot(cars, aes(speed, dist)) +
geom_point(shape=1) + # ○(shape=1)을 점의 모양으로 사용
geom_smooth() # 회귀곡선과 신뢰구간을 보여줌
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
plot(cars)
text(cars$speed, cars$dist, pos = 4)
각 점이 어느데이터에 해당하는지 쉽게 알 수 있다. pos는 1~4까지이며, 각각 아래,왼쪽,위,오른쪽으로 시계방향순이다.
plot(cars)
identify(cars$speed, cars$dist)
## integer(0)
마우스로 확인하고 싶은 점을 클릭한후, esc를 누르면 동작이 종료되고 클릭한 점의 번호가 그래프상에 표시된다.
plot(iris$Sepal.Width, iris$Sepal.Length, cex = 0.9, pch = 20, xlab = "width",
ylab = "length")
points(iris$Petal.Width, iris$Petal.Length, cex = 0.9, pch = "+", col = "#FF0000")
legend("topright", legend = c("Sepal", "Petal"), pch = c(20, 43), cex = 0.9,
col = c("black", "red"), bg = "gray")
위치는 topright로 했고, 범례는 Sepal, Petal을 각각 항목으로 했다. bg는 배경을 의미한다.
pch에서 ”+“가 아닌 43으로 한 이유는, 벡터가 한가지 타입의 인자를 받을 수 있기 때문이다. 굳이 모양으로 하고싶다고 한다면 pch안에 모양을 넣으면 된다.
x <- seq(-2 * pi, 2 * pi, 0.01)
x
## [1] -6.283185 -6.273185 -6.263185 -6.253185 -6.243185 -6.233185
## [7] -6.223185 -6.213185 -6.203185 -6.193185 -6.183185 -6.173185
## [13] -6.163185 -6.153185 -6.143185 -6.133185 -6.123185 -6.113185
## [19] -6.103185 -6.093185 -6.083185 -6.073185 -6.063185 -6.053185
## [25] -6.043185 -6.033185 -6.023185 -6.013185 -6.003185 -5.993185
## [31] -5.983185 -5.973185 -5.963185 -5.953185 -5.943185 -5.933185
## [37] -5.923185 -5.913185 -5.903185 -5.893185 -5.883185 -5.873185
## [43] -5.863185 -5.853185 -5.843185 -5.833185 -5.823185 -5.813185
## [49] -5.803185 -5.793185 -5.783185 -5.773185 -5.763185 -5.753185
## [55] -5.743185 -5.733185 -5.723185 -5.713185 -5.703185 -5.693185
## [61] -5.683185 -5.673185 -5.663185 -5.653185 -5.643185 -5.633185
## [67] -5.623185 -5.613185 -5.603185 -5.593185 -5.583185 -5.573185
## [73] -5.563185 -5.553185 -5.543185 -5.533185 -5.523185 -5.513185
## [79] -5.503185 -5.493185 -5.483185 -5.473185 -5.463185 -5.453185
## [85] -5.443185 -5.433185 -5.423185 -5.413185 -5.403185 -5.393185
## [91] -5.383185 -5.373185 -5.363185 -5.353185 -5.343185 -5.333185
## [97] -5.323185 -5.313185 -5.303185 -5.293185 -5.283185 -5.273185
## [103] -5.263185 -5.253185 -5.243185 -5.233185 -5.223185 -5.213185
## [109] -5.203185 -5.193185 -5.183185 -5.173185 -5.163185 -5.153185
## [115] -5.143185 -5.133185 -5.123185 -5.113185 -5.103185 -5.093185
## [121] -5.083185 -5.073185 -5.063185 -5.053185 -5.043185 -5.033185
## [127] -5.023185 -5.013185 -5.003185 -4.993185 -4.983185 -4.973185
## [133] -4.963185 -4.953185 -4.943185 -4.933185 -4.923185 -4.913185
## [139] -4.903185 -4.893185 -4.883185 -4.873185 -4.863185 -4.853185
## [145] -4.843185 -4.833185 -4.823185 -4.813185 -4.803185 -4.793185
## [151] -4.783185 -4.773185 -4.763185 -4.753185 -4.743185 -4.733185
## [157] -4.723185 -4.713185 -4.703185 -4.693185 -4.683185 -4.673185
## [163] -4.663185 -4.653185 -4.643185 -4.633185 -4.623185 -4.613185
## [169] -4.603185 -4.593185 -4.583185 -4.573185 -4.563185 -4.553185
## [175] -4.543185 -4.533185 -4.523185 -4.513185 -4.503185 -4.493185
## [181] -4.483185 -4.473185 -4.463185 -4.453185 -4.443185 -4.433185
## [187] -4.423185 -4.413185 -4.403185 -4.393185 -4.383185 -4.373185
## [193] -4.363185 -4.353185 -4.343185 -4.333185 -4.323185 -4.313185
## [199] -4.303185 -4.293185 -4.283185 -4.273185 -4.263185 -4.253185
## [205] -4.243185 -4.233185 -4.223185 -4.213185 -4.203185 -4.193185
## [211] -4.183185 -4.173185 -4.163185 -4.153185 -4.143185 -4.133185
## [217] -4.123185 -4.113185 -4.103185 -4.093185 -4.083185 -4.073185
## [223] -4.063185 -4.053185 -4.043185 -4.033185 -4.023185 -4.013185
## [229] -4.003185 -3.993185 -3.983185 -3.973185 -3.963185 -3.953185
## [235] -3.943185 -3.933185 -3.923185 -3.913185 -3.903185 -3.893185
## [241] -3.883185 -3.873185 -3.863185 -3.853185 -3.843185 -3.833185
## [247] -3.823185 -3.813185 -3.803185 -3.793185 -3.783185 -3.773185
## [253] -3.763185 -3.753185 -3.743185 -3.733185 -3.723185 -3.713185
## [259] -3.703185 -3.693185 -3.683185 -3.673185 -3.663185 -3.653185
## [265] -3.643185 -3.633185 -3.623185 -3.613185 -3.603185 -3.593185
## [271] -3.583185 -3.573185 -3.563185 -3.553185 -3.543185 -3.533185
## [277] -3.523185 -3.513185 -3.503185 -3.493185 -3.483185 -3.473185
## [283] -3.463185 -3.453185 -3.443185 -3.433185 -3.423185 -3.413185
## [289] -3.403185 -3.393185 -3.383185 -3.373185 -3.363185 -3.353185
## [295] -3.343185 -3.333185 -3.323185 -3.313185 -3.303185 -3.293185
## [301] -3.283185 -3.273185 -3.263185 -3.253185 -3.243185 -3.233185
## [307] -3.223185 -3.213185 -3.203185 -3.193185 -3.183185 -3.173185
## [313] -3.163185 -3.153185 -3.143185 -3.133185 -3.123185 -3.113185
## [319] -3.103185 -3.093185 -3.083185 -3.073185 -3.063185 -3.053185
## [325] -3.043185 -3.033185 -3.023185 -3.013185 -3.003185 -2.993185
## [331] -2.983185 -2.973185 -2.963185 -2.953185 -2.943185 -2.933185
## [337] -2.923185 -2.913185 -2.903185 -2.893185 -2.883185 -2.873185
## [343] -2.863185 -2.853185 -2.843185 -2.833185 -2.823185 -2.813185
## [349] -2.803185 -2.793185 -2.783185 -2.773185 -2.763185 -2.753185
## [355] -2.743185 -2.733185 -2.723185 -2.713185 -2.703185 -2.693185
## [361] -2.683185 -2.673185 -2.663185 -2.653185 -2.643185 -2.633185
## [367] -2.623185 -2.613185 -2.603185 -2.593185 -2.583185 -2.573185
## [373] -2.563185 -2.553185 -2.543185 -2.533185 -2.523185 -2.513185
## [379] -2.503185 -2.493185 -2.483185 -2.473185 -2.463185 -2.453185
## [385] -2.443185 -2.433185 -2.423185 -2.413185 -2.403185 -2.393185
## [391] -2.383185 -2.373185 -2.363185 -2.353185 -2.343185 -2.333185
## [397] -2.323185 -2.313185 -2.303185 -2.293185 -2.283185 -2.273185
## [403] -2.263185 -2.253185 -2.243185 -2.233185 -2.223185 -2.213185
## [409] -2.203185 -2.193185 -2.183185 -2.173185 -2.163185 -2.153185
## [415] -2.143185 -2.133185 -2.123185 -2.113185 -2.103185 -2.093185
## [421] -2.083185 -2.073185 -2.063185 -2.053185 -2.043185 -2.033185
## [427] -2.023185 -2.013185 -2.003185 -1.993185 -1.983185 -1.973185
## [433] -1.963185 -1.953185 -1.943185 -1.933185 -1.923185 -1.913185
## [439] -1.903185 -1.893185 -1.883185 -1.873185 -1.863185 -1.853185
## [445] -1.843185 -1.833185 -1.823185 -1.813185 -1.803185 -1.793185
## [451] -1.783185 -1.773185 -1.763185 -1.753185 -1.743185 -1.733185
## [457] -1.723185 -1.713185 -1.703185 -1.693185 -1.683185 -1.673185
## [463] -1.663185 -1.653185 -1.643185 -1.633185 -1.623185 -1.613185
## [469] -1.603185 -1.593185 -1.583185 -1.573185 -1.563185 -1.553185
## [475] -1.543185 -1.533185 -1.523185 -1.513185 -1.503185 -1.493185
## [481] -1.483185 -1.473185 -1.463185 -1.453185 -1.443185 -1.433185
## [487] -1.423185 -1.413185 -1.403185 -1.393185 -1.383185 -1.373185
## [493] -1.363185 -1.353185 -1.343185 -1.333185 -1.323185 -1.313185
## [499] -1.303185 -1.293185 -1.283185 -1.273185 -1.263185 -1.253185
## [505] -1.243185 -1.233185 -1.223185 -1.213185 -1.203185 -1.193185
## [511] -1.183185 -1.173185 -1.163185 -1.153185 -1.143185 -1.133185
## [517] -1.123185 -1.113185 -1.103185 -1.093185 -1.083185 -1.073185
## [523] -1.063185 -1.053185 -1.043185 -1.033185 -1.023185 -1.013185
## [529] -1.003185 -0.993185 -0.983185 -0.973185 -0.963185 -0.953185
## [535] -0.943185 -0.933185 -0.923185 -0.913185 -0.903185 -0.893185
## [541] -0.883185 -0.873185 -0.863185 -0.853185 -0.843185 -0.833185
## [547] -0.823185 -0.813185 -0.803185 -0.793185 -0.783185 -0.773185
## [553] -0.763185 -0.753185 -0.743185 -0.733185 -0.723185 -0.713185
## [559] -0.703185 -0.693185 -0.683185 -0.673185 -0.663185 -0.653185
## [565] -0.643185 -0.633185 -0.623185 -0.613185 -0.603185 -0.593185
## [571] -0.583185 -0.573185 -0.563185 -0.553185 -0.543185 -0.533185
## [577] -0.523185 -0.513185 -0.503185 -0.493185 -0.483185 -0.473185
## [583] -0.463185 -0.453185 -0.443185 -0.433185 -0.423185 -0.413185
## [589] -0.403185 -0.393185 -0.383185 -0.373185 -0.363185 -0.353185
## [595] -0.343185 -0.333185 -0.323185 -0.313185 -0.303185 -0.293185
## [601] -0.283185 -0.273185 -0.263185 -0.253185 -0.243185 -0.233185
## [607] -0.223185 -0.213185 -0.203185 -0.193185 -0.183185 -0.173185
## [613] -0.163185 -0.153185 -0.143185 -0.133185 -0.123185 -0.113185
## [619] -0.103185 -0.093185 -0.083185 -0.073185 -0.063185 -0.053185
## [625] -0.043185 -0.033185 -0.023185 -0.013185 -0.003185 0.006815
## [631] 0.016815 0.026815 0.036815 0.046815 0.056815 0.066815
## [637] 0.076815 0.086815 0.096815 0.106815 0.116815 0.126815
## [643] 0.136815 0.146815 0.156815 0.166815 0.176815 0.186815
## [649] 0.196815 0.206815 0.216815 0.226815 0.236815 0.246815
## [655] 0.256815 0.266815 0.276815 0.286815 0.296815 0.306815
## [661] 0.316815 0.326815 0.336815 0.346815 0.356815 0.366815
## [667] 0.376815 0.386815 0.396815 0.406815 0.416815 0.426815
## [673] 0.436815 0.446815 0.456815 0.466815 0.476815 0.486815
## [679] 0.496815 0.506815 0.516815 0.526815 0.536815 0.546815
## [685] 0.556815 0.566815 0.576815 0.586815 0.596815 0.606815
## [691] 0.616815 0.626815 0.636815 0.646815 0.656815 0.666815
## [697] 0.676815 0.686815 0.696815 0.706815 0.716815 0.726815
## [703] 0.736815 0.746815 0.756815 0.766815 0.776815 0.786815
## [709] 0.796815 0.806815 0.816815 0.826815 0.836815 0.846815
## [715] 0.856815 0.866815 0.876815 0.886815 0.896815 0.906815
## [721] 0.916815 0.926815 0.936815 0.946815 0.956815 0.966815
## [727] 0.976815 0.986815 0.996815 1.006815 1.016815 1.026815
## [733] 1.036815 1.046815 1.056815 1.066815 1.076815 1.086815
## [739] 1.096815 1.106815 1.116815 1.126815 1.136815 1.146815
## [745] 1.156815 1.166815 1.176815 1.186815 1.196815 1.206815
## [751] 1.216815 1.226815 1.236815 1.246815 1.256815 1.266815
## [757] 1.276815 1.286815 1.296815 1.306815 1.316815 1.326815
## [763] 1.336815 1.346815 1.356815 1.366815 1.376815 1.386815
## [769] 1.396815 1.406815 1.416815 1.426815 1.436815 1.446815
## [775] 1.456815 1.466815 1.476815 1.486815 1.496815 1.506815
## [781] 1.516815 1.526815 1.536815 1.546815 1.556815 1.566815
## [787] 1.576815 1.586815 1.596815 1.606815 1.616815 1.626815
## [793] 1.636815 1.646815 1.656815 1.666815 1.676815 1.686815
## [799] 1.696815 1.706815 1.716815 1.726815 1.736815 1.746815
## [805] 1.756815 1.766815 1.776815 1.786815 1.796815 1.806815
## [811] 1.816815 1.826815 1.836815 1.846815 1.856815 1.866815
## [817] 1.876815 1.886815 1.896815 1.906815 1.916815 1.926815
## [823] 1.936815 1.946815 1.956815 1.966815 1.976815 1.986815
## [829] 1.996815 2.006815 2.016815 2.026815 2.036815 2.046815
## [835] 2.056815 2.066815 2.076815 2.086815 2.096815 2.106815
## [841] 2.116815 2.126815 2.136815 2.146815 2.156815 2.166815
## [847] 2.176815 2.186815 2.196815 2.206815 2.216815 2.226815
## [853] 2.236815 2.246815 2.256815 2.266815 2.276815 2.286815
## [859] 2.296815 2.306815 2.316815 2.326815 2.336815 2.346815
## [865] 2.356815 2.366815 2.376815 2.386815 2.396815 2.406815
## [871] 2.416815 2.426815 2.436815 2.446815 2.456815 2.466815
## [877] 2.476815 2.486815 2.496815 2.506815 2.516815 2.526815
## [883] 2.536815 2.546815 2.556815 2.566815 2.576815 2.586815
## [889] 2.596815 2.606815 2.616815 2.626815 2.636815 2.646815
## [895] 2.656815 2.666815 2.676815 2.686815 2.696815 2.706815
## [901] 2.716815 2.726815 2.736815 2.746815 2.756815 2.766815
## [907] 2.776815 2.786815 2.796815 2.806815 2.816815 2.826815
## [913] 2.836815 2.846815 2.856815 2.866815 2.876815 2.886815
## [919] 2.896815 2.906815 2.916815 2.926815 2.936815 2.946815
## [925] 2.956815 2.966815 2.976815 2.986815 2.996815 3.006815
## [931] 3.016815 3.026815 3.036815 3.046815 3.056815 3.066815
## [937] 3.076815 3.086815 3.096815 3.106815 3.116815 3.126815
## [943] 3.136815 3.146815 3.156815 3.166815 3.176815 3.186815
## [949] 3.196815 3.206815 3.216815 3.226815 3.236815 3.246815
## [955] 3.256815 3.266815 3.276815 3.286815 3.296815 3.306815
## [961] 3.316815 3.326815 3.336815 3.346815 3.356815 3.366815
## [967] 3.376815 3.386815 3.396815 3.406815 3.416815 3.426815
## [973] 3.436815 3.446815 3.456815 3.466815 3.476815 3.486815
## [979] 3.496815 3.506815 3.516815 3.526815 3.536815 3.546815
## [985] 3.556815 3.566815 3.576815 3.586815 3.596815 3.606815
## [991] 3.616815 3.626815 3.636815 3.646815 3.656815 3.666815
## [997] 3.676815 3.686815 3.696815 3.706815 3.716815 3.726815
## [1003] 3.736815 3.746815 3.756815 3.766815 3.776815 3.786815
## [1009] 3.796815 3.806815 3.816815 3.826815 3.836815 3.846815
## [1015] 3.856815 3.866815 3.876815 3.886815 3.896815 3.906815
## [1021] 3.916815 3.926815 3.936815 3.946815 3.956815 3.966815
## [1027] 3.976815 3.986815 3.996815 4.006815 4.016815 4.026815
## [1033] 4.036815 4.046815 4.056815 4.066815 4.076815 4.086815
## [1039] 4.096815 4.106815 4.116815 4.126815 4.136815 4.146815
## [1045] 4.156815 4.166815 4.176815 4.186815 4.196815 4.206815
## [1051] 4.216815 4.226815 4.236815 4.246815 4.256815 4.266815
## [1057] 4.276815 4.286815 4.296815 4.306815 4.316815 4.326815
## [1063] 4.336815 4.346815 4.356815 4.366815 4.376815 4.386815
## [1069] 4.396815 4.406815 4.416815 4.426815 4.436815 4.446815
## [1075] 4.456815 4.466815 4.476815 4.486815 4.496815 4.506815
## [1081] 4.516815 4.526815 4.536815 4.546815 4.556815 4.566815
## [1087] 4.576815 4.586815 4.596815 4.606815 4.616815 4.626815
## [1093] 4.636815 4.646815 4.656815 4.666815 4.676815 4.686815
## [1099] 4.696815 4.706815 4.716815 4.726815 4.736815 4.746815
## [1105] 4.756815 4.766815 4.776815 4.786815 4.796815 4.806815
## [1111] 4.816815 4.826815 4.836815 4.846815 4.856815 4.866815
## [1117] 4.876815 4.886815 4.896815 4.906815 4.916815 4.926815
## [1123] 4.936815 4.946815 4.956815 4.966815 4.976815 4.986815
## [1129] 4.996815 5.006815 5.016815 5.026815 5.036815 5.046815
## [1135] 5.056815 5.066815 5.076815 5.086815 5.096815 5.106815
## [1141] 5.116815 5.126815 5.136815 5.146815 5.156815 5.166815
## [1147] 5.176815 5.186815 5.196815 5.206815 5.216815 5.226815
## [1153] 5.236815 5.246815 5.256815 5.266815 5.276815 5.286815
## [1159] 5.296815 5.306815 5.316815 5.326815 5.336815 5.346815
## [1165] 5.356815 5.366815 5.376815 5.386815 5.396815 5.406815
## [1171] 5.416815 5.426815 5.436815 5.446815 5.456815 5.466815
## [1177] 5.476815 5.486815 5.496815 5.506815 5.516815 5.526815
## [1183] 5.536815 5.546815 5.556815 5.566815 5.576815 5.586815
## [1189] 5.596815 5.606815 5.616815 5.626815 5.636815 5.646815
## [1195] 5.656815 5.666815 5.676815 5.686815 5.696815 5.706815
## [1201] 5.716815 5.726815 5.736815 5.746815 5.756815 5.766815
## [1207] 5.776815 5.786815 5.796815 5.806815 5.816815 5.826815
## [1213] 5.836815 5.846815 5.856815 5.866815 5.876815 5.886815
## [1219] 5.896815 5.906815 5.916815 5.926815 5.936815 5.946815
## [1225] 5.956815 5.966815 5.976815 5.986815 5.996815 6.006815
## [1231] 6.016815 6.026815 6.036815 6.046815 6.056815 6.066815
## [1237] 6.076815 6.086815 6.096815 6.106815 6.116815 6.126815
## [1243] 6.136815 6.146815 6.156815 6.166815 6.176815 6.186815
## [1249] 6.196815 6.206815 6.216815 6.226815 6.236815 6.246815
## [1255] 6.256815 6.266815 6.276815
y <- matrix(c(cos(x), sin(x)), ncol = 2) # y축은 행렬로 만든다.
c(cos(x), sin(x))는 cos(x)와 sin(x)로 구해진 벡터를 합한 새로운 벡터를 만든다. matrix는 값을 열 순으로 채우므로 새로운 벡터는 2개 열을 가진 행렬이 된다.
matplot(x, y, col = c("red", "black"), cex = 0.2)
abline(h = 0, v = 0)
빨간선은 cos(x)그래프를, 검정선은 sin(X)그래프이다. 열마다 그래프를 하나씩 그리는 듯하다.
boxplot(iris$Sepal.Width)
boxplot(iris$Sepal.Width, horizontal = TRUE)
상자그림을 그리면서 계산된 정확한 값을 보려면 boxplot()의 반환값을 보면 된다.
boxstats <- boxplot(iris$Sepal.Width)
boxstats
## $stats
## [,1]
## [1,] 2.2
## [2,] 2.8
## [3,] 3.0
## [4,] 3.3
## [5,] 4.0
##
## $n
## [1] 150
##
## $conf
## [,1]
## [1,] 2.935
## [2,] 3.065
##
## $out
## [1] 4.4 4.1 4.2 2.0
##
## $group
## [1] 1 1 1 1
##
## $names
## [1] "1"
stats은 차례대로 lower fence, 제1분위수, 중앙값(제2분위수), 제3분위수, upper fence이다.
out은 outlier(이상치)를 의미한다.
outlier옆에 해당하는 데이터의 번호를 표시해보자.
boxstats <- boxplot(iris$Sepal.Width, horizontal = TRUE)
text(boxstats$out, rep(1, NROW(boxstats$out)), labels = boxstats$out, pos = 1,
cex = 0.8)
y좌표는 rep(1, NROW(boxstats$out))을 사용했는데 이 값은 boxstats$out의 길이가 4이므로 1이 4회 반복된 벡터인 c(1,1,1,1)이 된다. 즉 텍스트의 위치를(outlier값, 1)로 잡은 것이다.
1로 잡은 이유는 상자그림이 y축의 1에 해당하는 곳에 일렬로 있기 때문이다.
notch라는 인자는 median값에 대한 신뢰구간이 오목하게 그려준다.
2개의 상자그림을 나란히 그렸을 때 만약 두 상자 그림의 notch가 겹친다면 두 상자 그림의 중앙값이 다르지 않다고 볼 수 있다.(이유를 설명하기 위해서는 통계적인 지식이 필요하다)
sv <- subset(iris, Species == "setosa" | Species == "versicolor")
sv$Species <- factor(sv$Species)
boxplot(Sepal.Width ~ Species, data = sv, notch = TRUE)
먼저 Species가 setosa 또는 versicolor인 행을 선택했다.
sv$Species를 factor로 변환했다. subset이 비록 두개의 Species만 선택하는 역할을 하더라도, level은 여전히 setosa, versicolor, 그리고 virginica이기 때문이다. 따라서 subset에 있는 level만 남기기 위한 것이다. 만약 factor지정을 하지 않았다면 텅빈 virginica컬럼이 추가로 보이게 될 것이다.
sv2 <- subset(iris, Species == "setosa" | Species == "versicolor")
boxplot(Sepal.Width ~ Species, data = sv2, notch = TRUE)
마지막으로 세번째 줄에서는 Sepal.Width를 Species마다 그렸다. 이는 'Sepal.Width ~ Species'로 표현되었다.
두 상자 그림의 notch를 살펴보면, 가운데 부분이 오목하게 들어가 있다. 이 영역이 중앙값에 대한 신뢰구간이다.