ggplot2: Polishing


Mẫu (theme)

Mẫu có sẵn

# mặc định nền xám
qplot(rating, data = movies, binwidth = 1)

plot of chunk built-in

# chuyển sang nền trắng
last_plot() + theme_bw()

plot of chunk built-in

# theme tác động tới đồ thị lúc hiển thị, 
# không phải lúc khởi tạo
hgram <- qplot(rating, data = movies, binwidth = 1)
hgram

plot of chunk built-in

previous_theme <- theme_set(theme_bw())
hgram

plot of chunk built-in

# ghi đè theme
hgram + previous_theme

plot of chunk built-in

# ghi đè vĩnh viễn
theme_set(previous_theme)

Cấu thành mẫu

# Thay đổi tiêu đề
hgramt <- hgram +
labs(title = "This is a histogram")
hgramt

plot of chunk theme element

# Đổi định dạng tiêu đề
hgramt + theme(plot.title = element_text(size = 20))

plot of chunk theme element

hgramt + theme(plot.title = element_text(size = 20, 
colour = "red"))

plot of chunk theme element

hgramt + theme(plot.title = element_text(size = 20, 
hjust = 0))

plot of chunk theme element

hgramt + theme(plot.title = element_text(size = 20, 
face = "bold"))

plot of chunk theme element

hgramt + theme(plot.title = element_text(size = 20, 
angle = 180))

plot of chunk theme element

# Đổi đường nền và các trục
hgram + theme(panel.grid.major = element_line(colour = "red"))

plot of chunk theme element

hgram + theme(panel.grid.major = element_line(size = 2))

plot of chunk theme element

hgram + theme(panel.grid.major = element_line(linetype = "dotted"))

plot of chunk theme element

hgram + theme(axis.line = element_line())

plot of chunk theme element

hgram + theme(axis.line = element_line(colour = "red"))

plot of chunk theme element

hgram + theme(axis.line = element_line(size = 0.5, 
linetype = "dashed"))

plot of chunk theme element

# Đổi màu nền
hgram + theme(plot.background = element_rect(fill = "grey80",
colour = NA))

plot of chunk theme element

hgram + theme(plot.background = element_rect(size = 2))

plot of chunk theme element

hgram + theme(plot.background = element_rect(colour = "red"))

plot of chunk theme element

hgram + theme(panel.background = element_rect())

plot of chunk theme element

hgram + theme(panel.background = element_rect(colour = NA))

plot of chunk theme element

hgram + theme(panel.background = 
element_rect(linetype = "dotted"))

plot of chunk theme element

# Xóa lần lượt các thuộc tính phi dữ liệu
hgramt

plot of chunk theme element

last_plot() + theme(panel.grid.minor = element_blank())

plot of chunk theme element

last_plot() + theme(panel.grid.major = element_blank())

plot of chunk theme element

last_plot() + theme(panel.background = element_blank())

plot of chunk theme element

last_plot() + 
theme(axis.title.x = element_blank(),
axis.title.y = element_blank())

plot of chunk theme element

last_plot() + theme(axis.line = element_line())

plot of chunk theme element

# Đổi theme mặc định
old_theme <- theme_update(
plot.background = element_rect(fill = "#3366FF"),
panel.background = element_rect(fill = "#003DF5"),
axis.text.x = element_text(colour = "#CCFF33"),
axis.text.y = element_text(colour = "#CCFF33", hjust = 1),
axis.title.x = element_text(colour = "#CCFF33", face = "bold"),
axis.title.y = element_text(colour = "#CCFF33", face = "bold",
angle = 90)
)
# Kết quả đổi theme
qplot(cut, data = diamonds, geom="bar")

plot of chunk theme element

qplot(cty, hwy, data = mpg)
## Error: ggplot2 doesn't know how to deal with data of class numeric
# Đặt lại theme
theme_set(old_theme)
# Kết quả đặt lại
qplot(cut, data = diamonds, geom="bar")

plot of chunk theme element

qplot(cty, hwy, data = mpg)
## Error: ggplot2 doesn't know how to deal with data of class numeric

Tùy chỉnh scalesgeoms

scale

# Thay đổi scale mặc định bằng cách gán hàm
scale_colour_discrete <- scale_colour_grey
scale_fill_discrete <- scale_fill_grey
scale_colour_continuous <- function(...)
scale_colour_gradient(low = "white", high = "black")
scale_fill_continuos <- function(...)
scale_fill_gradient(low = "white", high = "black")

geomstat

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

plot of chunk geom & stat

update_stat_defaults("bin", aes(y = ..density..))
qplot(rating, data = movies, geom = "histogram", binwidth = 1)

plot of chunk geom & stat

Lưu biểu đồ

# Lưu 1 biểu đồ
qplot(mpg, wt, data = mtcars)
ggsave(file = "output.pdf")

#Trường hợp lưu nhiều đồ thị vào 1 tệp
pdf(file = "output.pdf", width = 6, height = 6)
qplot(mpg, wt, data = mtcars)
qplot(wt, mpg, data = mtcars)
dev.off()

Nhiều biểu đồ trên cùng 1 trang

# 3 biểu đồ mẫu
(a <- qplot(date, unemploy, data = economics, geom = "line"))
(b <- qplot(uempmed, unemploy, data = economics) +
geom_smooth(se = F))
(c <- qplot(uempmed, unemploy, data = economics, geom="path"))

viewport()

# A viewport that takes up the entire plot device
library(grid)
vp1 <- viewport(width = 1, height = 1, x = 0.5, y = 0.5)
vp1 <- viewport()

# A viewport that takes up half the width and half the height,
# located in the middle of the plot.
vp2 <- viewport(width = 0.5, height = 0.5, x = 0.5, y = 0.5)
vp2 <- viewport(width = 0.5, height = 0.5)

# A viewport that is 2cm x 3cm located in the center
vp3 <- viewport(width = unit(2, "cm"), height = unit(3, "cm"))

# A viewport in the top right
vp4 <- viewport(x = 1, y = 1, just = c("right", "top" ))
# Bottom left
vp5 <- viewport(x = 0, y = 0, just = c("right", "bottom"))

Biểu đồ con

pdf("polishing-subplot-1.pdf", width = 4, height = 4)
subvp <- viewport(width = 0.4, height = 0.4, x = 0.75, y = 0.35)
b
print(c, vp = subvp)
dev.off()

# Sửa cho đẹp
csmall <- c +
theme_gray(9) +
labs(x = NULL, y = NULL) +
theme(plot.margin = unit(rep(0, 4), "lines"))

pdf("polishing-subplot-2.pdf", width = 4, height = 4)
b
print(csmall, vp = subvp)
dev.off()

Lưới chữ nhật

pdf("polishing-layout.pdf", width = 8, height = 6)
grid.newpage()
pushViewport(viewport(layout = grid.layout(2, 2)))

vplayout <- function(x, y)
viewport(layout.pos.row = x, layout.pos.col = y)
print(a, vp = vplayout(1, 1:2))
print(b, vp = vplayout(2, 1))
print(c, vp = vplayout(2, 2))
dev.off()