# Load the ggplot2 package
library(ggplot2)L7_1 Visualization
Biểu đồ tĩnh
Phần 1: Các dạng cơ bản
Tổng quan các biểu đồ
ggplot(data= mpg,
aes(x = displ, y = hwy)) +
geom_point()ggplot(mpg,
aes(displ, hwy)) +
geom_point()ggplot(mpg,
aes(displ )) +
geom_bar(stat = "count" )ggplot(economics,
aes(date, unemploy)) +
geom_line()ggplot(mpg,
aes(cty)) +
geom_histogram()`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Thêm màu sắc, hình dạng
ggplot(mpg,
aes(displ, hwy,
colour = year)) +
geom_point()ggplot(mpg,
aes(displ, hwy,
colour = class,
size= log(cty))) +
geom_point()ggplot(mpg,
aes(displ, hwy,
colour = class,
shape = drv)) +
geom_point()Thêm kích cỡ khác nhau
ggplot(mpg, aes(displ, hwy,
colour = class, # biến này là biến định tính để phân theo nhóm
shape = drv, # biến này là biến định tính để phân theo nhóm
size = cyl)) +
geom_point()ggplot(mpg, aes(displ, hwy,
colour = class, # biến này là biến định tính để phân theo nhóm
shape = drv, # biến này là biến định tính để phân theo nhóm
size = log(cyl))) +
geom_point()ggplot(mpg, aes(displ, hwy,
colour = class )) +
geom_point(color="#1d9137", size=6)ggplot(mpg, aes(displ, hwy,
size = cyl)) +
geom_point(color="blue")ggplot(mpg, aes(displ, hwy,
size = cyl)) +
geom_point(aes(color="Đây chỉ là tên gán vào"))Giao diện
ggplot(mpg,
aes(displ, hwy,
colour=class,
size= cyl)) +
geom_point() +
facet_wrap(~year)Các dạng cơ bản biểu đồ
geom_smooth() fits a smoother to the data and displays the smooth and its standard error.
ggplot(mpg, aes(displ, hwy)) +
geom_smooth() +
facet_wrap(~year)`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(mpg, aes(displ, hwy)) +
geom_smooth()`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
geom_boxplot() produces a box-and-whisker plot to summarise the distribution of a set of points.
ggplot(mpg, aes(displ, hwy)) +
geom_boxplot()+
facet_wrap(~class)Warning: Continuous x aesthetic
ℹ did you forget `aes(group = ...)`?
geom_histogram() and geom_freqpoly() show the distribution of continuous variables.
ggplot(mpg, aes( hwy)) +
geom_histogram() +
facet_wrap(~class)`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
ggplot(mpg, aes( hwy)) +
geom_freqpoly() +
facet_wrap(~class)`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
geom_bar() shows the distribution of categorical variables.
ggplot(mpg, aes(displ)) +
geom_bar() +
facet_wrap(~class)geom_path() and geom_line()
Kết hợp nhiều loại biểu đồ
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth()`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(span = 0.2)`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(span = 1)`geom_smooth()` using method = 'loess' and formula = 'y ~ x'
ggplot(mpg, aes(displ, hwy)) +
geom_point() +
geom_smooth(method = "lm") +
facet_wrap(~year)`geom_smooth()` using formula = 'y ~ x'
Boxplots
ggplot(mpg, aes(drv, hwy)) +
geom_point()ggplot(mpg, aes(drv, hwy)) + geom_jitter()ggplot(mpg, aes(drv, hwy)) + geom_boxplot()ggplot(mpg, aes(drv, hwy)) + geom_violin()ggplot(mpg,
aes(drv, hwy)) +
geom_jitter()+
geom_violin()ggplot(mpg, aes(drv, hwy,
colour=year))+
geom_violin() +
geom_jitter()Warning: The following aesthetics were dropped during statistical transformation:
colour.
ℹ This can happen when ggplot fails to infer the correct grouping structure in
the data.
ℹ Did you forget to specify a `group` aesthetic or to convert a numerical
variable into a factor?
Bar
ggplot(mpg, aes(drv, hwy)) + geom_bar(stat = "identity")ggplot(mpg, aes(drv, hwy)) + geom_point()Time series
ggplot(economics, aes(date, unemploy / pop)) +
geom_line()ggplot(economics, aes(date, uempmed)) +
geom_line()ggplot(economics, aes(unemploy / pop, uempmed)) +
geom_path() +
geom_point()ggplot(economics, aes(unemploy / pop, uempmed)) +
geom_path(colour = "grey50") +
geom_point(aes(colour = date))Kết hợp biểu đồ với các thông tin khác
# Hình 1
p1 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet, group=Chick)) +
geom_line() +
ggtitle("biểu đồ đường phân theo từng nhóm")
# Second plot
p2 <- ggplot(ChickWeight, aes(x=Time, y=weight, colour=Diet)) +
geom_point(alpha=.3) +
geom_smooth(alpha=.2, size=1) +
ggtitle("biểu đồ điểm và đường trung bình trượt")Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
ℹ Please use `linewidth` instead.
# Third plot
p3 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, colour=Diet)) +
geom_density() +
ggtitle("Biểu đồ mật độ")
# Fourth plot
p4 <- ggplot(subset(ChickWeight, Time==21), aes(x=weight, fill=Diet)) +
geom_histogram(colour="black", binwidth=50) +
facet_grid(Diet ~ .) +
ggtitle("Biểu đồ phân phối") +
theme(legend.position="none") # No legend (redundant in this graph) gridExtra::grid.arrange(p1,p2,p3,p4, ncol=2)`geom_smooth()` using method = 'loess' and formula = 'y ~ x'