#Principals of data visualization
- Tell the truth
- Maximize data-ink ration
- Maximize data density
- No lie-factor # GGPLOT2 Tải dữ liệu: gapminder
cú pháp: p = ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp)) Data=gapminder có thể thay bằng gapminder cũng được nhưng khi đảo vị trí sẽ không được
library(ggplot2)
library(gapminder)
p = ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp))
p = p + geom_point()
p
thêm kiểu khác
library(ggplot2)
library(gapminder)
library(ggthemes)
p = ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp), color=continent)
p = p + geom_point()
p = p + geom_line()
p
kiểu khác 2:
p2 = p + geom_point() + geom_smooth()
p2
## `geom_smooth()` using method = 'gam' and formula 'y ~ s(x, bs = "cs")'
thêm màu
p = ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))
p = p + geom_point(aes(color=continent))
p = p + geom_smooth(method="loess")
p = ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp), color=continent)
p = p + geom_point() + geom_smooth(method="loess")
p
## `geom_smooth()` using formula 'y ~ x'
Hoán chuyển trục hoành:
p4 = ggplot(data=gapminder, aes(x=gdpPercap, y=lifeExp), color=continent)
p4 = p4 + geom_point() + geom_smooth(method="loess")
p4
## `geom_smooth()` using formula 'y ~ x'
scale up
p = p + scale_x_log10()
p
## `geom_smooth()` using formula 'y ~ x'
Lớp 4: nhãn trục tung & hoành
p = ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))
p = p + geom_point(aes(color=continent))
p = p + geom_smooth(method="loess") + scale_x_log10()
p = p + labs(x="Log GDP per Capita", y="Life Expectancy")
p
## `geom_smooth()` using formula 'y ~ x'
Lớp 4: tựa đề (title)
p = p + ggtitle("Association between GDP Per Capita and Life
Expectancy") + theme(plot.title=element_text(lineheight=0.8,
face="bold", hjust = 0.5))
p
## `geom_smooth()` using formula 'y ~ x'
Lớp 5: theme
library(ggthemes)
p + theme_economist()
## `geom_smooth()` using formula 'y ~ x'
Văn phạm trang trí ggplot2 :
p = p + labs(title="Association between GDP Per Capita and Life
Expectancy", x="Log GDP per Capita", y="Life Expectancy")
p
## `geom_smooth()` using formula 'y ~ x'
Thêm Theme
p = ggplot(data = gapminder, aes(x = gdpPercap, y = lifeExp))
p = p + geom_point(aes(color=continent))
p = p + geom_smooth(method="loess")
p = p + labs(x="Log GDP per Capita", y="Life Expectancy")
p = p + labs(title="Association between GDP Per Capita and Life
Expectancy", x="Log GDP per Capita", y="Life Expectancy") + theme(axis.title.x=element_text(color="orange", size=10, face="bold"),
axis.text.x=element_text(angle=45, vjust=0.5, size=10, face="bold"),
axis.title.y = element_text(color="pink", size=14, face="bold"),
axis.text.y=element_text(angle=90, vjust=0.5, size=10, face="bold"))
p
## `geom_smooth()` using formula 'y ~ x'