拿R來畫畫(八):面積圖與堆積圖

面積圖

面積圖主要用來展示數量隨時間變化的情況,單一變量的面積圖在數字起伏較大時視覺效果較好。

平平無奇面積圖

library(ggplot2)
library(gcookbook)
# 太陽黑子數據集
sunspots
A Time Series: 235 × 12
JanFebMarAprMayJunJulAugSepOctNovDec
1749 58.0 62.6 70.0 55.7 85.0 83.5 94.8 66.3 75.9 75.5158.6 85.2
1750 73.3 75.9 89.2 88.3 90.0100.0 85.4103.0 91.2 65.7 63.3 75.4
1751 70.0 43.5 45.3 56.4 60.7 50.7 66.3 59.8 23.5 23.2 28.5 44.0
1752 35.0 50.0 71.0 59.3 59.7 39.6 78.4 29.3 27.1 46.6 37.6 40.0
# 將timeseries數據轉換成dataframe數據
dss = data.frame(year = as.numeric(time(sunspot.year)), ss = as.numeric(sunspot.year))
head(dss)
A data.frame: 6 × 2
yearss
<dbl><dbl>
1700 5
170111
170216
170323
170436
170558
ggplot(dss, aes(x = year, y = ss)) + 
geom_area()

在這裏插入圖片描述

精緻優雅面積圖

# color指定邊框顏色,fill指定填充顏色,alpha指定透明度
ggplot(dss, aes(x = year, y = ss)) + 
geom_area(color = "green", fill = "green", alpha = 0.3)

在這裏插入圖片描述

正負分開堆積圖

dss[100:200,]$ss = -dss[100:200,]$ss
dss$tag <- dss$ss >= 0
# rle用來捕獲連續運行長度,比如
# dat <- c(1, 2, 2, 2, 3, 1, 4, 4, 1, 1)
# r <- rle(dat)
# r
#   lengths: int [1:6] 1 3 1 1 2 2
#   values : num [1:6] 1 2 3 1 4 1
cat.rle = rle(dss$tag)
# rep.int用來生成重複序列
# rep.int(1:5,2)
# 1 2 3 4 5 1 2 3 4 5
dss$group = rep.int(1:length(cat.rle$lengths), times=cat.rle$lengths)
# group的作用是把每一正負分段分爲不同的組
ggplot(dss, aes(x = year, y = ss, fill = tag, group = group)) + 
geom_area(alpha = 0.3) + 
scale_fill_manual(values = c('green','red'), guide = FALSE)

在這裏插入圖片描述

# 如果僅使用fill自動分組會出現奇怪的圖形,中間部分上方的紅色是由於ggplot將tag中被FALSE分隔的兩個TRUE看做同一group連接起來導致的
ggplot(dss, aes(x = year, y = ss, fill = tag)) + 
geom_area(alpha = 0.3) + 
scale_fill_manual(values = c('green','red'), guide = FALSE)

在這裏插入圖片描述

# 可以看到每一段連續的tag共享同一個group
dss[107:115,]
A data.frame: 9 × 4
yearsstaggroup
<dbl><dbl><lgl><int>
1071806-28.1FALSE2
1081807-10.1FALSE2
1091808 -8.1FALSE2
1101809 -2.5FALSE2
1111810 0.0 TRUE3
1121811 -1.4FALSE4
1131812 -5.0FALSE4
1141813-12.2FALSE4
1151814-13.9FALSE4

堆積圖

堆積圖可以理解爲操作對象擴展至多個變量的面積圖。

平平無奇堆積圖

head(uspopage)
A data.frame: 6 × 3
YearAgeGroupThousands
<int><fct><int>
1900<5 9181
19005-14 16966
190015-2414951
190025-3412161
190035-44 9273
190045-54 6437
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + 
geom_area(color = 'black', size = 0.2, alpha = 0.3) + 
scale_fill_brewer(palette = "YlOrRd")

在這裏插入圖片描述

順序逆轉堆積圖

library(plyr)
ggplot(uspopage, aes(x = Year, y = Thousands, fill = factor(uspopage$AgeGroup, levels = rev(levels(uspopage$AgeGroup))))) + 
geom_area(color = 'black', size = 0.2, alpha = 0.3) + 
scale_fill_brewer(palette = "YlOrRd", breaks = rev(levels(uspopage$AgeGroup))) + 
labs(fill="AgeGroup")

在這裏插入圖片描述

去除豎線堆積圖

ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + 
geom_area(alpha = 0.3) + 
scale_fill_brewer(palette = "YlOrRd") + 
geom_line(position = 'stack', color = 'black', size = 0.2)

在這裏插入圖片描述

# 折騰半天我感覺還是沒線的最好看
ggplot(uspopage, aes(x = Year, y = Thousands, fill = AgeGroup)) + 
geom_area(alpha = 0.3) + 
scale_fill_brewer(palette = "YlOrRd")

在這裏插入圖片描述

百分比比堆積圖

# 計算百分比,首先按年份分成多個獨立的dataframe,在每個frame內部計算present
uspopage_prop <- ddply(uspopage, "Year", transform, Persent = Thousands/sum(Thousands)*100)
ggplot(uspopage_prop, aes(x = Year, y = Persent, fill = AgeGroup)) + 
geom_area(alpha = 0.3) + 
scale_fill_brewer(palette = "YlOrRd")

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章