Skip to Content
R在数据科学中的应用,第2版
book

R在数据科学中的应用,第2版

by Hadley Wickham, Mine Cetinkaya-Rundel, Garrett Grolemund
May 2025
Intermediate to advanced
578 pages
8h 9m
Chinese
O'Reilly Media, Inc.
Content preview from R在数据科学中的应用,第2版

第 9 章 图层 分层

本作品已使用人工智能进行翻译。欢迎您提供反馈和意见:translation-feedback@oreilly.com

导言

第 1 章中,你学到的远不止如何制作散点图、条形图和方框图。你学到的基础知识可以用来用 ggplot2 绘制任何类型的图。

本章将在此基础上进一步学习图形的分层语法。首先,我们将深入了解美学映射、几何对象和切面。然后,您将了解 ggplot2 在创建图形时在引擎盖下进行的统计变换。这些变换用于计算要绘制的新值,例如条形图中条形的高度或方框图中的中位数。您还将了解位置调整,它可以修改绘图中的地物显示方式。最后,我们将简要介绍坐标系。

我们不会涵盖每一层的所有函数和选项,但我们会引导您了解 ggplot2 提供的最重要和最常用的功能,并向您介绍扩展 ggplot2 的软件包。

先决条件

本章重点介绍 ggplot2。要访问本章使用的数据集、帮助页面和函数,请运行以下代码加载 tidyverse:

library(tidyverse)

审美映射

"一幅画的最大价值在于,它迫使我们注意到我们从未想过要看到的东西"。约翰-图基

请记住,与 ggplot2 软件包捆绑在一起的mpg 数据框包含了 38 种车型的 234 个观测值。

mpg
#> # A tibble: 234 × 11
#>   manufacturer model displ  year   cyl trans      drv     cty   hwy fl   
#>   <chr>        <chr> <dbl> <int> <int> <chr>      <chr> <int> <int> <chr>
#> 1 audi         a4      1.8  1999     4 auto(l5)   f        18    29 p    
#> 2 audi         a4      1.8  1999     4 manual(m5) f        21    29 p    
#> 3 audi         a4      2    2008     4 manual(m6) f        20    31 p    
#> 4 audi         a4      2    2008     4 auto(av)   f        21    30 p    
#> 5 audi         a4      2.8  1999     6 auto(l5)   f        16    26 p    
#> 6 audi         a4      2.8  1999     6 manual(m5) f        18    26 p    
#> # … with 228 more rows, and 1 more variable: class <chr>

mpg 中的变量包括

displ
汽车的发动机排量,单位为升。一个数字变量。
hwy
汽车在高速公路上的燃油效率,单位为英里/加仑(mpg)。行驶相同距离时,燃油效率低的汽车比燃油效率高的汽车耗油更多。一个数值变量。
class
汽车类型。一个分类变量。

首先,让我们来直观地看一下displhwy 之间的关系,对于不同的汽车classes。我们可以通过散点图来实现这一点,其中数字变量映射到xy 的审美观,分类变量映射到colorshape 这样的审美观。

# Left
ggplot(mpg, aes(x = displ, y = hwy, color = class)) +
  geom_point()

# Right
ggplot(mpg, aes(x = displ, y = hwy, shape = class)) +
  geom_point()
#> Warning: The shape palette can deal with a maximum of 6 discrete values
#> because ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.

Read now

Unlock full access

More than 5,000 organizations count on O’Reilly

AirBnbBlueOriginElectronic ArtsHomeDepotNasdaqRakutenTata Consultancy Services

QuotationMarkO’Reilly covers everything we've got, with content to help us build a world-class technology community, upgrade the capabilities and competencies of our teams, and improve overall team performance as well as their engagement.
Julian F.
Head of Cybersecurity
QuotationMarkI wanted to learn C and C++, but it didn't click for me until I picked up an O'Reilly book. When I went on the O’Reilly platform, I was astonished to find all the books there, plus live events and sandboxes so you could play around with the technology.
Addison B.
Field Engineer
QuotationMarkI’ve been on the O’Reilly platform for more than eight years. I use a couple of learning platforms, but I'm on O'Reilly more than anybody else. When you're there, you start learning. I'm never disappointed.
Amir M.
Data Platform Tech Lead
QuotationMarkI'm always learning. So when I got on to O'Reilly, I was like a kid in a candy store. There are playlists. There are answers. There's on-demand training. It's worth its weight in gold, in terms of what it allows me to do.
Mark W.
Embedded Software Engineer

You might also like

R深度学习权威指南

R深度学习权威指南

Posts & Telecom Press, Joshua F. Wiley
AI工程

AI工程

Chip Huyen
Raku学习手册

Raku学习手册

brian d foy

Publisher Resources

ISBN: 9798341657304