参考用书:
# apricoter ggplot2超详细讲解 https://www.jianshu.com/p/07f7931a00db
# Data Visualization https://socviz.co/makeplot.html
# 李东风 https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/ggplot2.html
# devtools::install_github("kjhealy/socviz")
代码:
# apricoter ggplot2超详细讲解 https://www.jianshu.com/p/07f7931a00db
# Data Visualization https://socviz.co/makeplot.html
# 李东风 https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/ggplot2.html
# devtools::install_github("kjhealy/socviz")
library(gapminder)
library(tidyverse)
p <- ggplot(data = gapminder,
mapping = aes(x = gdpPercap,
y = lifeExp,
color = continent)) # color & fill by variables #
p + geom_point(alpha = 0.3) + # color to all points | by variables in aes #
geom_smooth(method = "glm") +
scale_x_log10(labels = scales::dollar) +
labs(x = "GDP Per Capita", y = "Life Expectancy in Years",
title = "Economic Growth and Life Expectancy",
subtitle = "Data points are country-years",
caption = "Source: Gapminder.")
knitr::opts_chunk$set(fig.width=8, fig.height=5)
ggsave(filename = "my_figure.png") # plot = 图片若有命名 #