Skip to content

Commit a00cec6

Browse files
committed
add section 2.4
1 parent 58aa249 commit a00cec6

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

ex2.4.Rmd

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
---
2+
title: "Exercises for 2.4"
3+
output: html_notebook
4+
---
5+
6+
```{r}
7+
library(ggplot2)
8+
ggplot(mpg, aes(displ, cty, color = class)) + geom_point()
9+
ggplot(mpg, aes(displ, cty)) + geom_point(aes(color = "blue"))
10+
ggplot(mpg, aes(displ, cty)) + geom_point(color = "blue")
11+
```
12+
13+
第2句中的 `color = "blue"` 修饰的是 `aes` 函数,
14+
第3句中的 `color = "blue"` 修饰的是 `geom_point` 函数,
15+
后者比较好理解,指 scatter plot 图层的颜色使用蓝色,
16+
前者字面的意思是按照一个叫 "blue" 的特征对散点图做颜色分类,
17+
但并不存在 "blue" 这个特征,这时 `aes` 的行为似乎只是是给 "blue" 分配一个固定的颜色(粉色)。
18+
19+
> Instead of trying to make one very complex plot that shows everything at once,
20+
> see if you can create a series of simple plots that tells a story,
21+
> leading the reader from ignorance to knowledge.
22+
23+
# Q1
24+
25+
Experiment with the color, shape and size aesthetics.
26+
What happens when you map them to continuous values?
27+
What about categorical values?
28+
29+
```{r}
30+
ggplot(mpg, aes(displ, cty, color = hwy)) + geom_point()
31+
# ggplot(mpg, aes(displ, cty, shape = hwy)) + geom_point()
32+
```
33+
34+
将数值型特征映射到 color(上图)效果尚可,渐变色。
35+
将数值型特征映射到 shape (第2行代码)会导致错误,也是下面 Q2 的答案。
36+
37+
```{r}
38+
ggplot(mpg, aes(displ, cty, size = class)) + geom_point() # 将类别型特征映射到 size 效果不好,R 输出警告。
39+
ggplot(mpg, aes(displ, cty, shape = class)) + geom_point() # R 默认的 shape 只有6种,超过会导致区分度变差
40+
ggplot(mpg, aes(displ, cty, size = hwy)) + geom_point() # size 上应用数值型特征的效果不错
41+
ggplot(mpg, aes(cty, hwy, size = cty)) + geom_point() # x, y 值与 size 图例可以重复
42+
```
43+
44+
What happens when you use more one aesthetic in a plot?
45+
46+
```{r}
47+
ggplot(mpg, aes(displ, cty, size = hwy)) + geom_point(aes(color = class))
48+
ggplot(mpg, aes(displ, cty, color = manufacturer)) + geom_point()
49+
ggplot(mpg, aes(displ, cty, color = manufacturer)) + geom_point(aes(color = class))
50+
```
51+
52+
第1张图说明使用不同的 aesthetic 维度 (`size = hwy` vs `color = class`) 不会互相影响。
53+
54+
第3张图的图例名称虽然是 "manufacturer",内容却是 "class" 的,可见添加多个冲突的 `aes` 会导致错误。
55+
56+
可以对同一个特征使用多个 aesthetic map:
57+
```{r}
58+
ggplot(mpg, aes(displ, cty, color = drv, shape = drv)) + geom_point()
59+
```
60+
61+
# Q2
62+
63+
What happens if you map a continuous variable to shape? Why?
64+
65+
See answers above.
66+
67+
What happens if you map *trans* to shape? Why?
68+
69+
```{r}
70+
ggplot(mpg, aes(displ, cty, shape = trans)) + geom_point()
71+
```
72+
73+
*shape* 只能处理最多6种类型,超出的类型不显示新的 *shape* 图例。
74+
75+
# Q3
76+
77+
How is drive train related to fuel economy?
78+
79+
```{r}
80+
ggplot(mpg, aes(drv, cty.eco)) + geom_bar(stat = "identity")
81+
ggplot(mpg, aes(drv, cty)) + geom_bar(stat = "identity")
82+
```
83+
84+
`cty``cty.eco` 给出了矛盾的结论,貌似 `cty.eco` 算错了。
85+
86+
How is drive train related to engine size and class?
87+
88+
```{r}
89+
ggplot(mpg, aes(drv, displ)) + geom_bar(stat = "identity")
90+
#ggplot(mpg, aes(drv, class)) + geom_point()
91+
```
92+
93+
```{r}
94+
ggplot(mpg, aes(drv, ..count..)) +
95+
geom_bar(aes(fill = class), position = "dodge")
96+
```
97+
98+
`..count..` 是什么意思?
99+
100+
`fill``color` 的区别是什么?
101+
102+
`position` 的作用是什么?

0 commit comments

Comments
 (0)