Tom tat cac buoc chinh trong ggplot 2

- Buoc 1: Goi data va quy dinh cac truc x, y

VD: ggplot(data=dat2,aes(x=gdpPercap,y=lifeExp))

- Buoc 2: Chon kieu bieu do, chon kieu scatterplot

- Buoc 3: Ve them duong xu huong cua data

- Buoc 4: Hoan chuyen truc hoanh “gdpPercap”

- Buoc 5: Gan nhan cho tieu de, truc x, truc y

- Bước 6: Chinh Theme cho bieu do

- Buoc 7: Trang tri theme chi tiet

Thuc hanh ggplot2 voi data “gapminder”

library(gapminder)
## Warning: package 'gapminder' was built under R version 4.3.1
dat2=gapminder
library(ggplot2)
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ dplyr     1.1.2     ✔ readr     2.1.4
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.2.1
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the conflicted package (<http://conflicted.r-lib.org/>) to force all conflicts to become errors

Giai thich: data gapminder co san trong package “gapminder”

Xem data “gapminder”

head(dat2)
## # A tibble: 6 × 6
##   country     continent  year lifeExp      pop gdpPercap
##   <fct>       <fct>     <int>   <dbl>    <int>     <dbl>
## 1 Afghanistan Asia       1952    28.8  8425333      779.
## 2 Afghanistan Asia       1957    30.3  9240934      821.
## 3 Afghanistan Asia       1962    32.0 10267083      853.
## 4 Afghanistan Asia       1967    34.0 11537966      836.
## 5 Afghanistan Asia       1972    36.1 13079460      740.
## 6 Afghanistan Asia       1977    38.4 14880372      786.
names(dat2)
## [1] "country"   "continent" "year"      "lifeExp"   "pop"       "gdpPercap"

Buoc 1: Goi data vao ggplot

p=ggplot(data=dat2,aes(x=gdpPercap,y=lifeExp))

Buoc 2: Chon kieu bieu do, chon kieu scatterplot

p=p+geom_point(col="blue")
p

# Buoc 3: Ve them duong xu huong cua data

p=p+geom_smooth(col="coral")
p
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

# Buoc 3B: Ve them duong xu huong cua data, meyhod=“loess”

p=ggplot(data=dat2,aes(x=gdpPercap,y=lifeExp))+geom_point()
p=p+geom_smooth(col="red",method="loess")
p
## `geom_smooth()` using formula = 'y ~ x'

# Buoc 4: Hoan chuyen truc hoanh “gdpPercap”

p=ggplot(data=dat2,aes(x=gdpPercap,y=lifeExp))+geom_point(col="darkgreen")+geom_smooth(col="red",method="loess")
p=p+scale_x_log10()
p
## `geom_smooth()` using formula = 'y ~ x'

# Buoc 5: Gan nhan cho tieu de, truc x, truc y

p=p+labs(title="Mối liên quan giữa GDP và Life Expectancy", x="Log GDP per Capita", y="Life Expectancy")
p
## `geom_smooth()` using formula = 'y ~ x'

# Bước 6: Chinh Theme cho bieu do ### 6A: chon theme trang

p+theme_bw()
## `geom_smooth()` using formula = 'y ~ x'

### chon theme Classic

p+theme_classic()
## `geom_smooth()` using formula = 'y ~ x'

### chon theme test

p+theme_test()
## `geom_smooth()` using formula = 'y ~ x'

### chon theme replace

p+theme_replace()
## `geom_smooth()` using formula = 'y ~ x'

ggplot(data=dat2,aes(x=gdpPercap,y=lifeExp))+geom_point(aes(col=continent))+geom_smooth(col="coral")+scale_x_continuous(breaks=seq(0,120000,30000))+scale_y_continuous(breaks=seq(0,120,20))+labs(title="Mối liên quan giữa GDP và Life Expectancy", x="Log GDP per Capita", y="Life Expectancy")
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'

# Buoc 7: Trang tri theme ### axis.title.x va axis.title.y: dieu chinh nhan cac truc (mau xanh) ### axis.text.x va axis.text.y: dieu chinh chu so gia tri cac truc (mau nau) ### legend.position=“none”: khong hien chu giai

ggplot(data=dat2,aes(x=gdpPercap,y=lifeExp))+geom_point(aes(col=continent))+geom_smooth(col="coral")+scale_x_continuous(breaks=seq(0,120000,30000))+scale_y_continuous(breaks=seq(0,120,20))+labs(title="Mối liên quan giữa GDP và Life Expectancy", x="Log GDP per Capita", y="Life Expectancy")+theme(axis.title.x = element_text(color="blue",size = 14,face="bold"),axis.title.y = element_text(color="blue",size = 14,face="bold"),axis.text.x = element_text(colour="brown",angle=45,vjust=0.5,size=10,face="bold"),axis.text.y = element_text(colour="brown",angle=90,vjust=0.5,size=10,face="bold"),legend.position = "none")
## `geom_smooth()` using method = 'gam' and formula = 'y ~ s(x, bs = "cs")'