R for Data Science总结之——ggplot2

R for Data Science总结之——ggplot2

ggplot2作为R语言中经典的画图包,Grammar of Graphics理论的最佳实现,用图层的方式让数据研究人员可以最大程度地自定义化编程作图,其基本结构为:

ggplot(data = ) +
<GEOM_FUNCTION>(
mapping = aes(),
stat = ,
position =
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>

安装和加载:

install.packages("ggplot2")
library(ggplot2)

本文主要介绍Hadley Wickham著作R for Data Science 中的ggplot2包用法。

快速作图qplot()

qplot()函数是graphics包中的plot函数的简单包装,以图层理念的快速作图函数,简单实现为:

# Use data from data.frame
qplot(mpg, wt, data = mtcars)
qplot(mpg, wt, data = mtcars, colour = cyl)
qplot(mpg, wt, data = mtcars, size = cyl)
qplot(mpg, wt, data = mtcars, facets = vs ~ am)

# qplot will attempt to guess what geom you want depending on the input
# both x and y supplied = scatterplot
qplot(mpg, wt, data = mtcars)
# just x supplied = histogram
qplot(mpg, data = mtcars)
# just y supplied = scatterplot, with x = seq_along(y)
qplot(y = mpg, data = mtcars)

注意qplot()会根据给定自变量的数量猜测使用的geom自动作图
手动选择geom进行操作如下:

qplot(mpg, wt, data = mtcars, geom = "path")
qplot(factor(cyl), wt, data = mtcars, geom = c("boxplot", "jitter"))
qplot(mpg, data = mtcars, geom = "dotplot")

ggplot()函数

aes()

与qplot()在一个函数中控制所有的x,y轴变量,data, color, size, facet, geom不同的是,ggplot()中一般在ggplot()函数中控制data并通过aes()函数控制x,y轴变量,group, color, size的分组再根据图层思想,用 “+” 符号添加geom函数,不同的geom_point(), geom_line(), geom_boxplot()控制不同的制图,可以再Rstudio中打出geom再按"Tab"键进行选择,其后仍然用 “+” 符号添加C

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
What exactly is data science? With this book, you’ll gain a clear understanding of this discipline for discovering natural laws in the structure of data. Along the way, you’ll learn how to use the versatile R programming language for data analysis. Whenever you measure the same thing twice, you get two results—as long as you measure precisely enough. This phenomenon creates uncertainty and opportunity. Author Garrett Grolemund, Master Instructor at RStudio, shows you how data science can help you work with the uncertainty and capture the opportunities. You’ll learn about: Data Wrangling—how to manipulate datasets to reveal new information Data Visualization—how to create graphs and other visualizations Exploratory Data Analysis—how to find evidence of relationships in your measurements Modelling—how to derive insights and predictions from your data Inference—how to avoid being fooled by data analyses that cannot provide foolproof results Through the course of the book, you’ll also learn about the statistical worldview, a way of seeing the world that permits understanding in the face of uncertainty, and simplicity in the face of complexity. Table of Contents Part I. Explore Chapter 1. Data Visualization with ggplot2 Chapter 2. Workflow: Basics Chapter 3. Data Transformation with dplyr Chapter 4. Workflow: Scripts Chapter 5. Exploratory Data Analysis Chapter 6. Workflow: Projects Part II. Wrangle Chapter 7. Tibbles with tibble Chapter 8. Data Import with readr Chapter 9. Tidy Data with tidyr Chapter 10. Relational Data with dplyr Chapter 11. Strings with stringr Chapter 12. Factors with forcats Chapter 13. Dates and Times with lubridate Part III. Program Chapter 14. Pipes with magrittr Chapter 15. Functions Chapter 16. Vectors Chapter 17. Iteration with purrr Part IV. Model Chapter 18. Model Basics with modelr Chapter 19. Model Building Chapter 20. Many Models with purrr and broom Part V. Communicate Chapter 21. R Markdown Chapter 22. Graphics for Communication with ggplot2 Chapter 23. R Markdown Formats Chapter 24. R Markdown Workflow

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值