5.5.2 两个分类变量
要想对两个分类变量间的相关变动进行可视化表示,需要计算出每个变量组合中的观测数量。常用的两种方法有:
- 使用内置的
geom_count()
函数:
ggplot(data = diamonds) + geom_count(mapping = aes(x = cut, y = color))
【注】图中每个圆点的大小表示每个变量组合中的观测数量。相关变动就表示为特定 x 轴变量值与特定 y 轴变量值之间的强相关关系。
- 使用
dplyr
:
diamonds %>% count(color, cut) #> Source: local data frame [35 x 3] #> Groups: color [?] #> #> color cut n #> <ord> <ord> <int> #> 1 D Fair 163 #> 2 D Good 662 #> 3 D Very Good 1513 #> 4 D Premium 1603 #> 5 D Ideal 2834 #> 6 E Fair 224 #> # ... with 29 more rows
接着使用geom_tile()
函数和填充图形属性进行可视化表示:
diamonds %>% count(color, cut) %>% ggplot(mapping = aes(x = color, y = cut)) + geom_tile(mapping = aes(fill = n))
【注】如果分类变量是无序的,那么可以使用seriation
包对行和列同时进行重新排序,以便更清楚地表示出有趣的模式。对于更大的图形,你可以使用d3heatmap
或heatmaply
包,这两个包都可以生成有交互功能的图形。
5.5.2 习题解答
问题一
如何调整count
数据,使其能更清楚地表示出切割质量在颜色间的分布,或者颜色在切割质量间的分布?
解答
为了清楚地显示切割质量在颜色内的分布,可以引入一个新的变量prop
,即每个切割在颜色内的比例。
diamonds %>% count(color, cut) %>% group_by(color) %>% mutate(prop = n / sum(n)) %>% ggplot(mapping = aes(x = color, y = cut)) + geom_tile(mapping = aes(fill = prop))
同理可计算颜色在切割质量间的分布:
diamonds %>% count(color, cut) %>% group_by(cut) %>% mutate(prop = n / sum(n)) %>% ggplot(mapping = aes(x = color, y = cut)) + geom_tile(mapping = aes(fill = prop))
问题二
使用geom_tile()
函数结合 dplyr 来探索平均航班延误数量是如何随着目的地和月份的变化而变化的。为什么这张图难以阅读?如何改进?
解答
flights %>% group_by(month, dest) %>% summarise(dep_delay = mean(dep_delay, na.rm = TRUE)) %>% ggplot(aes(x = factor(month), y = dest, fill = dep_delay)) + geom_tile() + labs(x = "Month", y = "Destination", fill = "Departure Delay") #> `summarise()` regrouping output by 'month' (override with `.groups` argument)
从上图可以发现存在缺失值,因此可以通过删除缺失值来改进:
flights %>% group_by(month, dest) %>% # This gives us (month, dest) pairs summarise(dep_delay = mean(dep_delay, na.rm = TRUE)) %>% group_by(dest) %>% # group all (month, dest) pairs by dest .. filter(n() == 12) %>% # and only select those that have one entry per month ungroup() %>% mutate(dest = reorder(dest, dep_delay)) %>% ggplot(aes(x = factor(month), y = dest, fill = dep_delay)) + geom_tile() + labs(x = "Month", y = "Destination", fill = "Departure Delay") #> `summarise()` regrouping output by 'month' (override with `.groups` argument)
问题三
为什么在以上示例中使用aes(x = color, y = cut)
要比aes(x = cut, y = color)
更好?
解答
更好的做法是使用带有更多类别的分类变量,或者在y轴上较长的标签。如果可能的话,标签应该是水平的,因为这样更容易阅读。并且,切换顺序不会导致标签重叠。
diamonds %>% count(color, cut) %>% ggplot(mapping = aes(y = color, x = cut)) + geom_tile(mapping = aes(fill = n))