Prerequisites
install.packages("tidyverse")
library(tidyverse)
我们使用ggplot2中的mpg数据框进行分析测试。 mpg包含美国环境保护署收集的有关38辆汽车的观察结果。
mpg
#> # A tibble: 234 x 11
#> manufacturer model displ year cyl trans drv cty hwy fl class
#> <chr> <chr> <dbl> <int> <int> <chr> <chr> <int> <int> <chr> <chr>
#> 1 audi a4 1.8 1999 4 auto(l5) f 18 29 p compa…
#> 2 audi a4 1.8 1999 4 manual(m5) f 21 29 p compa…
#> 3 audi a4 2 2008 4 manual(m6) f 20 31 p compa…
#> 4 audi a4 2 2008 4 auto(av) f 21 30 p compa…
#> 5 audi a4 2.8 1999 6 auto(l5) f 16 26 p compa…
#> 6 audi a4 2.8 1999 6 manual(m5) f 18 26 p compa…
#> # … with 228 more rows
displ: 表示汽车引擎大小(以升为单位)。
hwy: 高速公路上的汽车燃油效率,以英里/加仑(mpg)为单位。 当相同距离行驶时,低燃油效率的汽车比高燃油效率的汽车消耗更多的燃油。
1. Statistical transformations
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut))
通常,可以互换使用geoms和stats。 例如,可以使用stat_count()而不是geom_bar()重新创建上一个图:
ggplot(data = diamonds) +
stat_count(mapping = aes(x = cut))
在下面的代码中,将geom_bar()的统计信息从count(默认值)更改为identity。
demo <- tribble(
~cut, ~freq,
"Fair", 1610,
"Good", 4906,
"Very Good", 12082,
"Premium", 13791,
"Ideal", 21551
)
ggplot(data = demo) +
geom_bar(mapping = aes(x = cut, y = freq), stat = "identity")
显示比例条形图,而不是计数条形图:
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, y = stat(prop), group = 1))
可以使用stat_summary()来汇总每个唯一x值的y值:
ggplot(data = diamonds) +
stat_summary(
mapping = aes(x = cut, y = depth),
fun.min = min,
fun.max = max,
fun = median
)
2. Position adjustments
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) +
geom_bar(mapping = aes(x = cut, fill = clarity))
堆叠是通过position参数指定的位置调整自动执行的。 如果不想堆积条形图,则可以使用其他三个选项之一:
identity
, dodge
或者 fill
。
position ="identity"
将把每个对象从纵坐标底部排列。要看到重叠,需要通过设置alpha来使条形稍微透明,或者通过设置fill = NA
来使条形完全透明。
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")
position ="fill"
的作用类似于堆叠,但是使每组堆叠的条具有相同的高度。 这样可以轻松比较各组之间的比例。
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "fill")
position ="dodge"
将重叠的对象彼此相邻放置。 这样可以轻松比较各个值。
ggplot(data = diamonds) +
geom_bar(mapping = aes(x = cut, fill = clarity), position = "dodge")
还有另一种调整类型,对条形图没有用,但对散点图很有用。比如数据集中有234个观测值,但图中仅能显示126个点。
可以通过将位置调整设置为
jitter
来避免。 position ="jitter"
给每个点添加少量的随机噪声。 因为没有两个点会接收到相同数量的随机噪声,所以可以将这些点分散开来。
ggplot(data = mpg) +
geom_point(mapping = aes(x = displ, y = hwy), position = "jitter")
3. Coordinate systems
coord_flip()
切换x
和y
轴。
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot()
ggplot(data = mpg, mapping = aes(x = class, y = hwy)) +
geom_boxplot() +
coord_flip()
coord_quickmap()
为地图正确设置纵横比。
nz <- map_data("nz")
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black")
ggplot(nz, aes(long, lat, group = group)) +
geom_polygon(fill = "white", colour = "black") +
coord_quickmap()
coord_polar()
使用极坐标。 极坐标揭示了条形图和Coxcomb图之间的关系。
bar <- ggplot(data = diamonds) +
geom_bar(
mapping = aes(x = cut, fill = cut),
show.legend = FALSE,
width = 1
) +
theme(aspect.ratio = 1) +
labs(x = NULL, y = NULL)
bar + coord_flip()
bar + coord_polar()
参考:https://r4ds.had.co.nz/data-visualisation.html
网友评论