--- layout: post title: "ggplot2: Polishing" published: yes --- ## Mẫu (`theme`) ### Mẫu có sẵn ```{r built-in} # mặc định nền xám qplot(rating, data = movies, binwidth = 1) # chuyển sang nền trắng last_plot() + theme_bw() # 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 previous_theme <- theme_set(theme_bw()) hgram # ghi đè theme hgram + previous_theme # ghi đè vĩnh viễn theme_set(previous_theme) ``` ### Cấu thành mẫu ```{r theme element} # Thay đổi tiêu đề hgramt <- hgram + labs(title = "This is a histogram") hgramt # Đổi định dạng tiêu đề hgramt + theme(plot.title = element_text(size = 20)) hgramt + theme(plot.title = element_text(size = 20, colour = "red")) hgramt + theme(plot.title = element_text(size = 20, hjust = 0)) hgramt + theme(plot.title = element_text(size = 20, face = "bold")) hgramt + theme(plot.title = element_text(size = 20, angle = 180)) # Đổi đường nền và các trục hgram + theme(panel.grid.major = element_line(colour = "red")) hgram + theme(panel.grid.major = element_line(size = 2)) hgram + theme(panel.grid.major = element_line(linetype = "dotted")) hgram + theme(axis.line = element_line()) hgram + theme(axis.line = element_line(colour = "red")) hgram + theme(axis.line = element_line(size = 0.5, linetype = "dashed")) # Đổi màu nền hgram + theme(plot.background = element_rect(fill = "grey80", colour = NA)) hgram + theme(plot.background = element_rect(size = 2)) hgram + theme(plot.background = element_rect(colour = "red")) hgram + theme(panel.background = element_rect()) hgram + theme(panel.background = element_rect(colour = NA)) hgram + theme(panel.background = element_rect(linetype = "dotted")) # Xóa lần lượt các thuộc tính phi dữ liệu hgramt last_plot() + theme(panel.grid.minor = element_blank()) last_plot() + theme(panel.grid.major = element_blank()) last_plot() + theme(panel.background = element_blank()) last_plot() + theme(axis.title.x = element_blank(), axis.title.y = element_blank()) last_plot() + theme(axis.line = element_line()) # Đổ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") qplot(cty, hwy, data = mpg) # Đặt lại theme theme_set(old_theme) # Kết quả đặt lại qplot(cut, data = diamonds, geom="bar") qplot(cty, hwy, data = mpg) ``` ## Tùy chỉnh `scales` và `geoms` ### `scale` ```{r 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") ``` ### `geom` và `stat` ```{r geom & stat} update_geom_defaults("point", aes(colour = "darkblue")) qplot(mpg, wt, data=mtcars) update_stat_defaults("bin", aes(y = ..density..)) qplot(rating, data = movies, geom = "histogram", binwidth = 1) ``` ## Lưu biểu đồ ```{r eval=FALSE} # 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 ```{r eval=FALSE} # 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()` ```{r eval=FALSE} # 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 ```{r eval=FALSE} 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 ```{r eval=FALSE} 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() ```