ggplot2: Layers


Tạo 1 biểu đồ

library(ggplot2)
# init one
p <- ggplot(diamonds, aes(carat, price, colour = cut))
# add a layer
p <- p + layer(geom = "point")
# Now p is this
p

plot of chunk creating a plot

Toàn bộ đối số của hàm layer():

 layer(geom, geom_params, stat, stat_params, data, mapping, 
   position)

Lối tắt

# Ver 1: Verbose
p <- ggplot(diamonds, aes(x = carat))
p <- p + layer(
geom = "bar",
geom_params = list(fill = "steelblue"),
stat = "bin",
stat_params = list(binwidth = 2)
)
p

plot of chunk another plot

# Ver 2: Shortcut
p <- ggplot(diamonds, aes(x = carat))
p <- p + geom_histogram(binwidth = 2, fill = "steelblue")
p

plot of chunk another plot

Cấu trúc lối tắt:

geom_XXX(mapping, data, ..., geom, position)
stat_XXX(mapping, data, ..., stat, position)

ggplotqplot

ggplot(msleep, aes(sleep_rem / sleep_total, awake)) + 
geom_point()
## Warning: Removed 22 rows containing missing values (geom_point).

plot of chunk other examples

# Tương đương với
qplot(sleep_rem / sleep_total, awake, data = msleep)
## Warning: Removed 22 rows containing missing values (geom_point).

plot of chunk other examples

# Thêm layer to qplot:
qplot(sleep_rem / sleep_total, awake, data = msleep) +
geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Removed 22 rows containing missing values (stat_smooth).
## Warning: Removed 22 rows containing missing values (geom_point).

plot of chunk other examples

# Tương đương với
qplot(sleep_rem / sleep_total, awake, data = msleep,
geom = c("point", "smooth"))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Removed 22 rows containing missing values (stat_smooth).
## Warning: Removed 22 rows containing missing values (geom_point).

plot of chunk other examples

# hoặc
ggplot(msleep, aes(sleep_rem / sleep_total, awake)) +
geom_point() + geom_smooth()
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.
## Warning: Removed 22 rows containing missing values (stat_smooth).
## Warning: Removed 22 rows containing missing values (geom_point).

plot of chunk other examples

Hàm summary()

p <- ggplot(msleep, aes(sleep_rem / sleep_total, awake))
summary(p)
## data: name, genus, vore, order, conservation, sleep_total,
## sleep_rem, sleep_cycle, awake, brainwt, bodywt [83x11]
## mapping: x = sleep_rem/sleep_total, y = awake
## faceting: facet_null()
p <- p + geom_point()
summary(p)
## data: name, genus, vore, order, conservation, sleep_total,
## sleep_rem, sleep_cycle, awake, brainwt, bodywt [83x11]
## mapping: x = sleep_rem/sleep_total, y = awake
## faceting: facet_null()
## -----------------------------------
## geom_point: na.rm = FALSE
## stat_identity:
## position_identity: (width = NULL, height = NULL)

Tách lớp

bestfit <- geom_smooth(method = "lm", se = F, 
colour = I("steelblue"), alpha = I(0.5), size = 2)
qplot(sleep_rem, sleep_total, data = msleep) + bestfit
## Warning: Removed 22 rows containing missing values (stat_smooth).
## Warning: Removed 22 rows containing missing values (geom_point).

plot of chunk separate layer

qplot(awake, brainwt, data = msleep, log = "y") + bestfit
## Warning: Removed 27 rows containing missing values (stat_smooth).
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk separate layer

qplot(bodywt, brainwt, data = msleep, log = "xy") + bestfit
## Warning: Removed 27 rows containing missing values (stat_smooth).
## Warning: Removed 27 rows containing missing values (geom_point).

plot of chunk separate layer

Dữ liệu

p <- ggplot(mtcars, aes(mpg, wt, colour = cyl)) + geom_point()
p

plot of chunk data

# Làm mới dữ liệu
mtcars <- transform(mtcars, mpg = mpg ^ 2)
p %+% mtcars

plot of chunk data

Các ánh xạ thẩm mỹ

Hàm aes()

aes(x = weight, y = height, colour = age)
aes(weight, height, colour = sqrt(age))

Biểu đồ và phân lớp

p <- ggplot(mtcars)
summary(p)
## data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb
## [32x11]
## faceting: facet_null()
p <- p + aes(wt, hp)
summary(p)
## data: mpg, cyl, disp, hp, drat, wt, qsec, vs, am, gear, carb
## [32x11]
## mapping: x = wt, y = hp
## faceting: facet_null()
p <- ggplot(mtcars, aes(x = mpg, y = wt))
p + geom_point()

plot of chunk plot & layer

# Ghi đè phân lớp
p + geom_point(aes(colour = factor(cyl)))

plot of chunk plot & layer

p + geom_point(aes(y = disp))

plot of chunk plot & layer

Thiết đặt hay ánh xạ?

p <- ggplot(mtcars, aes(mpg, wt))
# Thiết đặt
p + geom_point(colour = "darkblue")

plot of chunk setting vs mapping

# Ánh xạ
p + geom_point(aes(colour = "darkblue"))

plot of chunk setting vs mapping

# Tương tự với `qplot()`
qplot(mpg, wt, data=mtcars, colour = I("darkblue"))

plot of chunk setting vs mapping

qplot(mpg, wt, data=mtcars, colour = "darkblue")

plot of chunk setting vs mapping

Nhóm

Nhiều nhóm, một thuộc tính thẩm mỹ

data(Oxboys, package="nlme")
p <- ggplot(Oxboys, aes(age, height, group = Subject)) +
geom_line()
# Tương tự với `qplot()`
qplot(age, height, data=Oxboys, group = Subject, geom="line")

plot of chunk grouping 1

qplot(age, height, data=Oxboys, geom="line")

plot of chunk grouping 1

Các nhóm khác nhau với các thuộc tính thẩm mỹ (lớp) khác nhau

p + geom_smooth(aes(group = Subject), method="lm", se = F)

plot of chunk grouping 2

p + geom_smooth(aes(group = 1), method="lm", size = 2, se = F)

plot of chunk grouping 2

# Tương tự với `qplot()`
qplot(age, height, data=Oxboys, group = Subject, geom="line") +
geom_smooth(method="lm", se = F)

plot of chunk grouping 2

qplot(age, height, data=Oxboys, group = Subject, geom="line") +
geom_smooth(aes(group = 1), method="lm", size = 2, se = F)

plot of chunk grouping 2

Ghi đè nhóm mặc định

(boysbox <- ggplot(Oxboys, aes(Occasion, height)) + geom_boxplot())

plot of chunk grouping 3

boysbox + geom_line(aes(group = Subject), colour = "#3366FF")

plot of chunk grouping 3

# Tương tự với `qplot()`
qplot(Occasion, height, data=Oxboys, geom="boxplot")

plot of chunk grouping 3

qplot(Occasion, height, data=Oxboys, geom="boxplot") +
geom_line(aes(group = Subject), colour="#3366FF")

plot of chunk grouping 3

Ghép thuộc tính thẩm mỹ với đối tượng đồ họa

Đường thẳng

df <- data.frame(x = 1:3, y = 1:3, colour = c(1,3,5))
# Ver 1
qplot(x, y, data=df, colour=factor(colour), size = I(5)) +
geom_line(aes(group = 1), size = 2)

plot of chunk matching lines

# Ver 2
qplot(x, y, data=df, colour=colour, size = I(5)) +
geom_line(size = 2)

plot of chunk matching lines

# Chuyển đổi biến rời rạc thành biến liên tục
xgrid <- with(df, seq(min(x), max(x), length = 50))
interp <- data.frame(
x = xgrid,
y = approx(df$x, df$y, xout = xgrid)$y,
colour = approx(df$x, df$colour, xout = xgrid)$y
)

# Ver 3
qplot(x, y, data = df, colour = colour, size = I(5)) +
geom_line(data = interp, size = 2)

plot of chunk matching lines

Đa giác

qplot(color, data = diamonds)

plot of chunk matching polygon

qplot(color, data = diamonds, fill = cut)

plot of chunk matching polygon

Phép biến đổi thống kê

ggplot(diamonds, aes(carat)) + 
geom_histogram(aes(y = ..density..), binwidth = 0.1)

plot of chunk stat

# Tương tự với `qplot()`  
qplot(carat, ..density.., data = diamonds, geom="histogram",
binwidth = 0.1)

plot of chunk stat

Điều chỉnh vị trí

# Stack, fill & dodge
dplot <- ggplot(diamonds, aes(clarity, fill = cut))
dplot + geom_bar(position = "stack")

plot of chunk position adjustments

dplot + geom_bar(position = "fill")

plot of chunk position adjustments

dplot + geom_bar(position = "dodge")

plot of chunk position adjustments

# Identity
dplot + geom_bar(position = "identity")

plot of chunk position adjustments

qplot(clarity, data = diamonds, geom="line", colour = cut, 
stat="bin", group=cut)

plot of chunk position adjustments

Kết hợp tất cả!

Kết hợp geoms và stats

d <- ggplot(diamonds, aes(carat)) + xlim(0, 3)
# Ver 1: area geom
d + stat_bin(aes(ymax = ..count..), binwidth = 0.1, geom = "area")

plot of chunk geoms and stats

# Ver 2: point geom
d + stat_bin(
aes(size = ..density..), binwidth = 0.1,
geom = "point", position="identity"
)
## Warning: Removed 2 rows containing missing values (geom_point).

plot of chunk geoms and stats

# # Ver 3: tile geom
d + stat_bin2d(aes(y = 1), binwidth = c(0.1,1))

plot of chunk geoms and stats

Vẽ đồ thị cho các thống kê tính trước

stat_identity()

Thay đổi thuộc tính thẩm mỹ và dữ liệu

require(nlme, quiet = TRUE, warn.conflicts = FALSE)
# Chạy mô hình 1
model <- lme(height ~ age, data = Oxboys,
random = ~ 1 + age | Subject)
# Khởi tạo plot
oplot <- ggplot(Oxboys, aes(age, height, group = Subject)) +
geom_line()

# So sánh giữa thực tế và mô hình:
# 1. Tính toán theo mô hình
# từ dữ liệu tự khởi tạo.
age_grid <- seq(-1, 1, length = 10)
subjects <- unique(Oxboys$Subject)

preds <- expand.grid(age = age_grid, Subject = subjects)
preds$height <- predict(model, preds)

oplot + geom_line(data = preds, colour = "#3366FF", size= 0.4)

plot of chunk vary aes & data

# 2. Tính toán theo mô hình
# từ dữ liệu gốc.
Oxboys$fitted <- predict(model)
Oxboys$resid <- with(Oxboys, fitted - height)

# Cập nhật dữ liệu gốc, đổi trục tung thành residual,
# và thêm một đường smooth.
oplot %+% Oxboys + aes(y = resid) + geom_smooth(aes(group=1))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

plot of chunk vary aes & data

# Chạy mô hình 2
model2 <- update(model, height ~ age + I(age ^ 2))
Oxboys$fitted2 <- predict(model2)
Oxboys$resid2 <- with(Oxboys, fitted2 - height)

oplot %+% Oxboys + aes(y = resid2) + geom_smooth(aes(group=1))
## geom_smooth: method="auto" and size of largest group is <1000, so using loess. Use 'method = x' to change the smoothing method.

plot of chunk vary aes & data