第一章 使用ggplot2进行数据可视化(下)
代码块
一、几何对象
同样是加载tidyverse软件包
几何对象是图中用来表示数据的几何图形对象。即是使用不同的可视化对来表示(同一个)数据。 一个数据可以用多个几何对象表示。ggplot2提供了30多种几何对象。
library(tidyverse)
# left
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy))
# right
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy))
在看两个个例子:
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, linetype = drv))
ggplot(data = mpg) +
geom_smooth(mapping = aes(x = displ, y = hwy, color = drv, linetype = drv))+
geom_point(mapping = aes(x = displ, y = hwy, color = drv))
2、统计变换
绘图时用来计算新数据的算法称为stat(statistical transfermation,统计变换)。ggplot2提供了20多种统计变换。 diamonds数据集也是ggplot2内置数据,含有大约54000颗钻石的信息,包括price, carat, color, clarity, and cut。
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
geom_bar()函数的统计变换过程:
几何对象可以和统计变换函数互换使用
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
再看一个例子
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = ..prop.., group = 1))
stat_summary强调统计变换
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.ymin = min,
fun.ymax = max,
fun.y = median
)
3、位置调整
color或者fill填充图形颜色
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, colour = cut))
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = cut))
ggplot(data = diamonds, mapping = aes(x = cut, fill = clarity)) +
geom_bar(alpha = 1/5, position = "identity")
ggplot(data = diamonds, mapping = aes(x = cut, colour = clarity)) +
geom_bar(fill = NA, position = "identity")
四、坐标系
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
五、图形分层语法
图形分层语法模板
替换尖括号内的数据
ggplot(data = <DATA>) +
<GEOM_FUNCTION>(
mapping = aes(<MAPPINGS>),
stat = <STAT>,
position = <POSITION>
) +
<COORDINATE_FUNCTION> +
<FACET_FUNCTION>