今天我們學(xué)習(xí)畫柱狀圖的另一個技巧只恨,如何繪制雙坐標(biāo)系。一般情況下包含多組數(shù)據(jù)拼弃,還這多組數(shù)據(jù)的scale或者單位又不太一樣,所以有時(shí)候就需要多坐標(biāo)系來實(shí)現(xiàn)摇展。
比如上面這張圖吻氧,為了在一張圖中展示更豐富的信息,既有柱狀圖又有折線圖咏连。
如果柱狀圖和折線圖的值域不一致盯孙,比如柱狀圖表示的是數(shù)量,折線圖表示累計(jì)百分比祟滴,當(dāng)二者出現(xiàn)在一張圖中的時(shí)候镀梭,值域范圍 [0, 1] 折線圖就會幾乎貼近 x 軸而失去意義。
這時(shí)候我們就建立兩個坐標(biāo)軸踱启,柱狀圖和折線圖各自使用各自的scale报账。
比如研底,我們生成下面的測試數(shù)據(jù):
data <- data.frame(group = c("<10", "10-15", "15-20", "20-25", "25-30", ">30"),
? ? ? ? ? ? ? ? ? count = c(70, 15, 8, 4, 2, 1),
? ? ? ? ? ? ? ? ? percent = c(0.70, 0.85, 0.93, 0.97, 0.99, 1.00))
# 把group變成factor,從而按照我們想顯示的順序顯示
data$group <- factor(data$group,levels = as.character(data$group))
ggplot(data) +
geom_bar(aes(x = group, y = count), stat = "identity", fill = '#168aad')
上面就是生成了一個非常簡單的柱狀圖透罢。
但是榜晦,下面如果我們添加線圖的話:
ggplot(data) +
geom_bar(aes(x = group, y = count), stat = "identity", fill = '#168aad')+
geom_line(aes(x = group, y = percent), size = 1, color = '#800080') +
geom_point(aes(x = group, y = percent), size = 3, shape = 19, color='#800080')
就會報(bào)錯:
geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?
查資料,說要把group設(shè)置成1羽圃,尤其是有多組變量的時(shí)候乾胶。
ggplot(data) +
geom_bar(aes(x = group, y = count), stat = "identity", fill = '#168aad')+
geom_line(aes(x = group, y = percent), size = 1, color = '#800080',group=1) +
geom_point(aes(x = group, y = percent), size = 3, shape = 19, color='#800080',group=1)
但是效果如下圖,因?yàn)閏ount和percentage scale是不一樣的朽寞,所以percentage的規(guī)律其實(shí)是被完全覆蓋掉的识窿。
所以,如果我們要想讓count和percent分別按照自己的值域范圍顯示脑融,并且呈現(xiàn)在同一個圖中喻频,就需要把其中之一的值域范圍向另一個做投影,以統(tǒng)一值域范圍肘迎,相當(dāng)于 scaling甥温。
這里我們選擇將percent向count做投影,投影之后新增一列percent2妓布,然后通過改變坐標(biāo)軸 label 的方式達(dá)到保持原指標(biāo)值域范圍的目的姻蚓。
data$percent2 = data$percent / max(data$percent) * max(data$count)
ggplot(data) +
geom_bar(aes(x = group, y = count), stat = "identity", fill = '#168aad')+
geom_line(aes(x = group, y = percent2), size = 1, color = '#800080',group=1) +
geom_point(aes(x = group, y = percent2), size = 3, shape = 19, color='#800080',group=1)
這樣把count和percentage放在一個scale區(qū)間內(nèi),規(guī)律就比較能展現(xiàn)出來了匣沼。
下面狰挡,我們嘗試來添加percentage的坐標(biāo)系。
主要通過scale_y_continuous函數(shù)里面的sec.axis參數(shù)來實(shí)現(xiàn)創(chuàng)制2個坐標(biāo)系释涛。
data$percent2 = data$percent / max(data$percent) * max(data$count)
count_max=max(data$count)
label=paste0(seq(0, 100, 10))
ggplot(data) +
geom_bar(aes(x = group, y = count), stat = "identity", fill = '#168aad')+
geom_line(aes(x = group, y = percent2), size = 1, color = '#800080',group=1) +
geom_point(aes(x = group, y = percent2), size = 3, shape = 19, color='#800080',group=1)+
#geom_text(aes(x=group,y=count,label=count),size=5,color="red")+
scale_y_continuous(
limits=c(0,count_max),
breaks=seq(0,count_max,5),
sec.axis = sec_axis(~./0.99, name = "percent(%)",
breaks = seq(0,count_max,count_max/10),
labels = label)
)
通過sec.axis添加次級y軸加叁,次級y軸的刻度需要通過一級y軸的刻度調(diào)整而來,~./0.99就表示次級y軸的范圍是用一級y軸除以0.99
然后枢贿,我們再來調(diào)整一下外觀殉农。
ggplot(data) +
geom_bar(aes(x = group, y = count), stat = "identity", fill = '#168aad')+
geom_line(aes(x = group, y = percent2), size = 1, color = '#800080',group=1) +
geom_point(aes(x = group, y = percent2), size = 3, shape = 19, color='#800080',group=1)+
#geom_text(aes(x=group,y=count,label=count),size=5,color="red")+
scale_y_continuous(
limits=c(0,count_max),
breaks=seq(0,count_max,5),
sec.axis = sec_axis(~./0.99, name = "percent(%)",
breaks = seq(0,count_max,count_max/10),
labels = label)
)+
theme_classic()+
#theme_minimal() +
theme(panel.grid.major.x = element_blank(),
? ? ? ? ? panel.grid.minor.x = element_blank(),
? ? ? ? ? panel.grid.major.y = element_blank(),
? ? ? ? ? panel.grid.minor.y = element_blank()) +
theme(plot.title = element_text(hjust = 0.5)) +
labs(title = paste0("CV% distribution"), x = "group", y = "count")+
theme(text = element_text(size = 15))+
theme(axis.line.x = element_line(linetype = 1, color = "darkblue", size = 1),
? ? ? axis.line.y = element_line(linetype = 1, color = "darkblue", size = 1),
? ? ? axis.ticks.x = element_line(color = "darkblue", size = 1),
? ? ? axis.ticks.y = element_line(color = "darkblue", size = 1),
? ? ? ? ? )