ggplot(faithful, aes(x = waiting)) + geom_histogram()
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
기본설정은 30개의 빈(bin)으로 묶임. 빈의 크기는 binwidth 으로 조정할 수 있음.
ggplot(faithful, aes(x = waiting)) +
geom_histogram(binwidth = 5, fill = "white", colour = "black")
binsize <- diff(range(faithful$waiting))/15
ggplot(faithful, aes(x = waiting)) +
geom_histogram(binwidth = binsize, fill = "white", colour = "black")
origin(boundary 로 바뀜)을 사용, 경계 이동.
h <- ggplot(faithful, aes(x = waiting))
h + geom_histogram(binwidth = 8, fill = "white", colour = "black", boundary = 31)
h + geom_histogram(binwidth = 8, fill = "white", colour = "black", boundary = 35)
데이터가 이산 값일 때 히스토그램 빈의 아래쪽 경계는 닫혀있고 위쪽 경계는 열려있음.
[1,2), [2,3) : 첫번째 빈에는 1은 들어있지만 2는 들어있지 않음. 동일한 효과를 내는 함수 : geom_bar(stat = “bin”)
면을 분할하는 변수로 smoke 사용
library(MASS)
#str(birthwt)
ggplot(birthwt, aes(x = bwt)) +
geom_histogram(fill = "white", colour = "black") +
facet_grid(smoke ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
birthwt1 <- birthwt
birthwt1$smoke <- factor(birthwt1$smoke)
levels(birthwt1$smoke)
## [1] "0" "1"
library(plyr)
birthwt1$smoke <- revalue(birthwt1$smoke, c("0" = "No smoke", "1" = "Smoke"))
ggplot(birthwt1, aes(x = bwt)) +
geom_histogram(fill = "white", colour = "black") +
facet_grid(smoke ~ .)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
y축 눈금크기를 독립적으로 변경되게 : scale = “free”
ggplot(birthwt1, aes(x = bwt)) +
geom_histogram(fill = "white", colour = "black") +
facet_grid(race ~ ., scale = "free")
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
fill 에 집단분류 변수를 대입
ggplot(birthwt1, aes(x = bwt, fill = smoke)) +
geom_histogram(position = "identity", alpha = 0.4)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
커널밀도곡선(kernel density curve) : geom_density() 이용, x에 연속변수 대입
ggplot(faithful, aes(x = waiting)) +
geom_density()
양 옆과 아래쪽에 선이 나오지 않게 하려면 geom_line(stat = “density”)
ggplot(faithful, aes(x = waiting)) +
geom_line(stat = "density") +
expand_limits(y = 0)
밀도곡선의 y값을 맞추기 위해 y = ..density.. 를 대입해서 히스토그램의 크기를 줄여 밀도곡선에 맞춤.
ggplot(faithful, aes(x = waiting, y = ..density..)) +
geom_histogram(fill = "cornsilk", colour = "grey60", size = .2) +
geom_density() +
xlim(35, 105)
## `stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(birthwt1, aes(x = bwt, colour = smoke)) +
geom_density()
ggplot(birthwt1, aes(x = bwt, fill = smoke)) +
geom_density(alpha = .3)
ggplot(birthwt1, aes(x = bwt)) +
geom_density() +
facet_grid(smoke ~ .)