R语言 ggplot作图

参考用书:
https://www.jianshu.com/p/07f7931a00db
https://socviz.co/makeplot.html
https://www.math.pku.edu.cn/teachers/lidf/docs/Rbook/html/_Rbook/ggplot2.html

# 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 = 图片若有命名 #

# nest数据嵌套
# https://zhuanlan.zhihu.com/p/390532042 推荐
# https://zhuanlan.zhihu.com/p/346700620
# https://zhuanlan.zhihu.com/p/390533249
iris %>%
  group_by(Species) %>%
  nest()
iris %>%
  nest(petal = c(Petal.Length, Petal.Width), 
       sepal = c(Sepal.Length, Sepal.Width))
model <- mtcars%>%
  group_by(cyl) %>%
  nest() %>%
  mutate(model = map(data, ~lm(mpg ~ wt + qsec, 
                               data = .x)))

猜你喜欢

转载自blog.csdn.net/weixin_42683052/article/details/121266421