Thể hiện mối quan hệ giữa GDP bình quân đầu người và lượng khí nhà kính phát thải của Việt Nam và thế giới
Mở các gói
library(RJSONIO)
library(WDI)
library(ggplot2)
library(magrittr)
library(ggthemes)
library(tidyverse)
## Loading tidyverse: tibble
## Loading tidyverse: tidyr
## Loading tidyverse: readr
## Loading tidyverse: purrr
## Loading tidyverse: dplyr
## Conflicts with tidy packages ----------------------------------------------
## filter(): dplyr, stats
## lag(): dplyr, stats
library(hrbrthemes)
Thu thập dữ liệu
dat <- WDI(indicator = c("EN.ATM.CO2E.KT", "NY.GDP.PCAP.CD","EN.ATM.GHGT.KT.CE"),
country = c("VN"),
start = 1980,
end = 2015)
Hình ảnh
dat %>% ggplot(aes(dat$NY.GDP.PCAP.CD,dat$EN.ATM.GHGT.KT.CE/1000,
label = year)) +
geom_point()+
geom_text(check_overlap = TRUE)+
stat_smooth(method = "lm", formula = y ~ x + I(x^2), size = 1 )+
labs(x = "GDP per capita (current US)",
y = NULL,
title = "Figure 1: Relationship between total greenhouse emission
and GDP per capita: 1980-2012",
subtitle = "Total greenhouse gas emissions (1000 kt of CO2 equivalent)",
caption = "Data Source: The World Bank")+
scale_x_continuous(breaks = seq(0,2000,100)) +
scale_y_continuous(breaks = seq(0,300,100))+
theme_economist(base_size = 10,
horizontal = T) +
scale_color_economist()
## Warning: Removed 8 rows containing non-finite values (stat_smooth).
## Warning: Removed 8 rows containing missing values (geom_point).
## Warning: Removed 8 rows containing missing values (geom_text).
-> Chúng ta thấy được rằng khi GDP bình quân đầu người tăng, lượng khí nhà kính Việt Nam phát thải ngày càng nhiều
Thu thập số liệu
dat_W <- WDI(indicator = c("EN.ATM.CO2E.KT", "NY.GDP.PCAP.CD","EN.ATM.GHGT.KT.CE"),
country = "all",
start = 2012,
end = 2012,
extra = TRUE,
cache = NULL)
View(dat_W)
Đổi tên lại các biến cho dễ đọc
dat_W <- rename(dat_W, TGHE = EN.ATM.GHGT.KT.CE,
GDPper = NY.GDP.PCAP.CD,
CO2 =EN.ATM.CO2E.KT)
Bỏ các quan sát tính theo khu vực, chỉ lấy các quan sát là các quốc gia
dat_W <- dat_W[-c(1:5,19,66:67,159,178:181,201:206,225:229,236:243,245:253,255:260,262:263),]
Vẽ hình
dat_W %>% ggplot(aes(log(dat_W$GDPper),(dat_W$TGHE/1000), label = dat_W$country )) +
geom_point(aes(col = income))+
geom_text(aes(colour = income), check_overlap = T)+
labs(x = "GDP per capita (current US)",
y = "Total green house emission (1000 kt of CO2 equivalent)",
title = "Figure 2:Relationship between total greenhouse
emission and GDP per capita in 2012",
caption = "Data Source: The World Bank")+
theme_ipsum() +
scale_color_ipsum()
## Warning: Removed 46 rows containing missing values (geom_point).
## Warning: Removed 46 rows containing missing values (geom_text).
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x
## $y, : font family not found in Windows font database
## Warning in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x
## $y, : font family not found in Windows font database
## Warning in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x
## $y, : font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
Chúng ta thấy được China, US,India, Rusian, Brazil là những nước có lượng phát thải lớn
Loại bỏ đi các nước China, US,India, Rusian, Brazil để quan sát các nước ở dưới
dung <- dat_W %>% arrange(desc(TGHE))
dung <- dung[-c(1:5),]
Vẽ lại hình
dung <- rename(dung, Income = income)
par(bg = "gray98")
ggplot(data = dung,
aes(log(dung$GDPper),dung$TGHE/1000, label = dung$country))+
geom_point(aes(col = Income), size = 2)+
geom_text(aes(colour = Income), check_overlap = T)+
stat_smooth(method = "lm", formula = y ~ x + I(x^2), size =1)+
labs ( x = "Log GDP per capita (Current US$)",
y = NULL,
title = "Figure 3: Relationship between Greenhouse
gas emissions and GDP per capita in 2012",
subtitle = "Greenhouse gas emissions (1000kt of CO2 equivalent)",
caption = "Source: World Bank") +
theme_ipsum() +
scale_color_ipsum()
## Warning: Removed 46 rows containing non-finite values (stat_smooth).
## Warning: Removed 46 rows containing missing values (geom_point).
## Warning: Removed 46 rows containing missing values (geom_text).
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x
## $y, : font family not found in Windows font database
## Warning in grid.Call.graphics(L_text, as.graphicsAnnot(x$label), x$x, x
## $y, : font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
## Warning in grid.Call(L_textBounds, as.graphicsAnnot(x$label), x$x, x$y, :
## font family not found in Windows font database
Từ những hình vẽ trên em có những khúc mắc mong anh dành chút thời gian giải đáp cho em ạ:
-) Ở hình 1 em muốn làm những số biểu thị đơn vị trên trục x nghiêng một góc khoảng 45 độ thì phải làm như thế nào ạ.Tại vì các số ở trên trục x nó bị sát nhau quá nên hình không được đẹp cho lắm anh ạ
-) Ở hình 2 em chỉ muốn hiện tên của 5 quốc gia phát thải lớn nhất thì phải làm như thế nào ạ ?
-) Ở hình 3 em muốn hiện rõ lên được cái điểm của Việt Nam và cho một màu khác để nổi bật lên thì phải làm như thế nào ạ ?
Ngoài ra trong những hình vẽ trên em có mắc lỗi gì mong anh nhận xét để em có thể rút kinh nghiệm ạ
Em cảm ơn anh