Skip to content
Navigation Menu
Sign in
Products
Platform
GitHub
Repository hosting, code review, and collaboration
Copilot
AI coding agent for tasks, projects, and automated work
Build & Ship
Actions
CI/CD and automation
Code Quality
Quality analysis, fixes, and enforcement
Codespaces
Cloud development environments
Secure
Code Security
Vulnerability detection and remediation
Secret Protection
Secret scanning and leak prevention
All Features
Developers
Learn
Documentation
Training
Certifications
Build
Build with Copilot
Customize Copilot
Integrations
Explore
Trending repos
GitHub Sponsors
Developer articles
Changelog
Changelog
Your source for the latest features, improvements, and fixes across GitHub products, all in one place.
Businesses
By Stage
Enterprises
Team
Startups
Nonprofits
By Industry
Healthcare
Financial services
Manufacturing
Government
See all industries
Evaluate GitHub
Why GitHub
Customer Stories
Business insights
Trust Center
Add-ons
GitHub Advanced Security
Enterprise-grade code security
Copilot for business
Enterprise-grade AI features
Premium Support
Enterprise-grade 24/7 support
Resources
Learn & Connect
Documentation
Resource center
Blog
Events & webinars
Community Forum
Customer Stories
Integrations
Support
Customer support
Training
GitHub Status
Company
About
Partners
Changelog
GitHub Universe
GitHub Universe
The event for developers, by developers — talks, workshops, and what’s next for GitHub.
Pricing
Search
/
Sign in
Sign up
Appearance settings
You signed in with another tab or window.
Reload
to refresh your session.
You signed out in another tab or window.
Reload
to refresh your session.
You switched accounts on another tab or window.
Reload
to refresh your session.
Dismiss alert
{{ message }}
afkbcgz
R_for_Data_Science
Repository navigation
Code
Pull requests
Actions
Projects
Security and quality
Insights
More
items
Files
Expand file tree
master
Breadcrumbs
R_for_Data_Science
/
tidyverse_ggplot2_aes_eval.Rmd
Copy path
Blame
More file actions
Blame
More file actions
Latest commit
History
History
History
71 lines (46 loc) · 2.41 KB
master
Breadcrumbs
R_for_Data_Science
/
tidyverse_ggplot2_aes_eval.Rmd
Copy path
Top
File metadata and controls
Code
Blame
71 lines (46 loc) · 2.41 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# ggplot2之延迟映射 {#tidyverse-ggplot2-aes-eval}
本章是翻译的ggplot的官方文档,后续我会增加一些案例
```{r, message=FALSE, warning=FALSE}
library(tidyverse)
library(palmerpenguins)
penguins <- penguins %>%
drop_na()
```
绝大部分时候,数据框的变量直接映射到图形元素,然后生成图片。但也有一些时候,变量需要先做统计变换,然后再映射给图形元素,这个过程称之为**延迟映射**。
ggplot2 把进行数据映射分成了三个阶段。
- 第一个阶段,**拿到数据之后**。最初阶段,拿到用户提供的数据,映射给图形元素。
- 第二个阶段,**统计变换之后**。数据完成转化或者统计计算之后,再映射给图形元素。
- 第三个阶段,**图形标度之后**。数据完成标度配置之后,映射给图形元素,在最后渲染出图之前。
延迟到**统计变换之后**的例子是,在`geom_histogram()`画柱状图的时候,
柱子的高度并不直接来源用户提供的数据,而是来源于`stat_bin()`函数统计计算后的结果
```{r}
penguins %>%
ggplot(aes(x = bill_length_mm)) +
geom_histogram(aes(y = after_stat(count)))
```
```{r}
# Scale tallest bin to 1
penguins %>%
ggplot(aes(x = bill_length_mm)) +
geom_histogram(aes(y = after_stat(count) / max(count)))
```
延迟到**图形标度之后**的例子是,`geom_bar()`画柱子图,定义了柱子的颜色后,想在颜色的基础上增加点透明度,作为柱子的填充色。
```{r}
penguins %>%
ggplot(aes(x = species, color = species)) +
geom_bar(
aes(fill = after_scale(alpha(color, 0.6)))
)
```
如果不是直接使用原始数据,而是用统计变换后的数据来映射,就需要使用`after_stat()`函数告诉ggplot2 等到统计变换完成后再做美学映射。类似地,如果想在完成标度配置之后,再映射给图形元素,就需要使用`after_scale()`函数。如果多次映射图形元素,比如变量 `x` 先传递给统计函数,然后把统计结果映射给图形元素,就需要使用
`stage(start = NULL, after_stat = NULL, after_scale = NULL)` 控制每一个过程。
```{r}
penguins %>%
ggplot(aes(x = species)) +
geom_bar(
aes(fill = stage(start = species, after_scale = alpha(fill, 0.4)))
)
```
```{r, echo = F, message = F, warning = F, results = "hide"}
pacman::p_unload(pacman::p_loaded(), character.only = TRUE)
```
You can’t perform that action at this time.