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