R语言 ggplot作图

Posted 基督徒Isaac

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)))

以上是关于R语言 ggplot作图的主要内容,如果未能解决你的问题,请参考以下文章

R语言可视化及作图10--ggplot2的theme函数

R语言ggplot2绘图单元格为方块的热图简单小例子

R语言可视化及作图7--ggplot2之标签、图例和标题绘制

【R】ggplot2绘图技巧

R ggplot 作图

R语言可视化及作图6--ggplot2之点图、条形图、盒形图、直方图、线图