Part 1 :棒棒糖圖
棒棒糖圖因其形狀和棒棒糖相似而得名,具體來看實際上是一個散點和一條線段的組合。棒棒糖圖是散點圖的一種變體,又與柱狀圖非常相似,但其在清晰展示數(shù)據(jù)的同時,減少了圖形量,使得讀者能夠更加關(guān)注于數(shù)據(jù)本身而非圖形。棒棒糖圖能夠幫助將數(shù)值與類別對齊,非常適合比較多個類別的值之間的差異。 本期介紹棒棒糖圖的基本作圖方法和優(yōu)化
示例1:相同數(shù)據(jù)源下柱狀圖和棒棒糖圖展示數(shù)值差異效果對比
示例2:使用棒棒糖圖畫出的好圖
Part 2 :圖像與代碼
1、快速構(gòu)建基礎(chǔ)圖形:
只需在散點圖基礎(chǔ)上,增加geom_segment( )圖層即可畫出棒棒糖圖
示例3:基礎(chǔ)圖形
library(ggplot2)
# 隨機構(gòu)建數(shù)據(jù)
data <- data.frame(xv = LETTERS[1:26], #x軸:以26個字母作為類別
yv = abs(rnorm(26))) #y軸:取26個符合正態(tài)分布的隨機數(shù)
## fig 1 :基礎(chǔ)圖形
fig3 = ggplot(data, aes(x = xv, y = yv)) +
geom_segment( aes(x = xv, xend = xv, y = 1, yend = yv),color = "grey40")+ #控制線段的參數(shù),見下
geom_point(size = 4, pch = 21, bg = 5, col = 1) + #控制散點的參數(shù)
theme_bw()
fig3
#geom_segment中的參數(shù)用于控制線條相關(guān)參數(shù)
#x=xv,xend=xv 表示x軸的線條起始位置x和終止位置xend都是xv(沒有線條)
#類似的,y = 1, yend = yv表示y軸線段起始點為y=1,種植點為每個類別的值即yv
2、基礎(chǔ)優(yōu)化:
(1)對線段的控制體現(xiàn)在對geom_segment()函數(shù)參數(shù)的調(diào)整,該函數(shù)的詳細用法可參考 https://ggplot2.tidyverse.org/reference/geom_segment.html
在此我們介紹常用的參數(shù):
x - (必須) 起點的x坐標
y - (必須) 起點的y坐標 — 此參數(shù)用作設(shè)定基線
xend - (必須)終點的x坐標
yend - (必須)終點的y坐標
size - (默認:0.5)線段的寬度
linetype - (默認:1=solid)線段的類型,可參見下圖中 lty 變量類型
color - (默認: "black") 線段的顏色
alpha - (默認:1=opaque)線段的透明度
(2)對頂點(散點)的控制則由geom_point()控制,此處不再贅述,可參考:
https://ggplot2.tidyverse.org/reference/geom_point.html?q=geom_point#null
值得一提的是,R語言本身提供了一些自定義圖形外觀的參數(shù),這些參數(shù)幾乎可以適用在所有基于R語言的繪圖包中:
cex - 形狀大小
lwd - 線條寬度
col - 控制顏色類型
lty - 線條類型
pch - 標記(散點)形狀
type - 點之間的連接形狀
這些參數(shù)對應(yīng)的可選變量見下圖:
(3)其他:
- x、y軸坐標交換 :直接使用** coord_flip() **函數(shù)即可
- 對變量進行排序,使圖形更加美觀;此處提供3種方法供參考:forcats::fct_reorder()、dplyr::arrange()、reorder(),具體示例參見:https://www.r-graph-gallery.com/267-reorder-a-variable-in-ggplot2.html
- 要在散點上顯示對應(yīng)數(shù)值:使用geom_text()
示例4:簡單優(yōu)化后的棒棒糖圖
library(ggplot2)
library(dplyr)
# 隨機構(gòu)建數(shù)據(jù)
data <- data.frame(xv=LETTERS[1:26],
yv=abs(rnorm(26)))
data <- data %>%
mutate(mycolor = ifelse(yv>1, "Y>1", "Y<=1")) #設(shè)置分組
## fig 2 :簡單優(yōu)化后的圖形
fig4 <- ggplot(data, aes(x=reorder(xv,-yv), y=yv,fill = mycolor)) +
geom_segment( aes(x=reorder(xv,-yv), xend=reorder(xv,-yv), y=1, yend=yv,color=mycolor),
size=0.5,linetype=1)+#使用reorder()排序變量
geom_point(size = 5, pch = 21, color="black") +
#在散點上顯示數(shù)值并保留兩位小數(shù)
geom_text(aes(label =sprintf("%.2f",yv)), color = "black", size = 1.6)+
xlab("26Letters") +
scale_y_continuous("Y_Values",breaks =c(0,0.25,0.5,0.75,1,1.25,1.5,1.75,2)) +
coord_flip() +
theme_minimal()
fig4
示例5:帶分組的棒棒糖圖
library(ggplot2)
library(hrbrthemes)
yv1 <- abs(rnorm(6))*2
data6 <- data.frame(
x=LETTERS[1:24],
val=c( yv1, yv1+1+rnorm(6, 14,1) ,yv1+1+rnorm(6, sd=1) ,yv1+1+rnorm(6, 12, 1) ),
grp=rep(c("grp1", "grp2", "grp3", "grp4"), each=6)
) %>%
arrange(val) %>%
mutate(x=factor(x, x))
fig5 = ggplot(data6) +
geom_segment( aes(x=x, xend=x, y=0, yend=val), color="grey") +
geom_point( aes(x=x, y=val, color=grp), size=3 ) +
coord_flip()+
theme_ipsum() +
theme(
legend.position = "none",
panel.border = element_blank(),
panel.spacing = unit(0.1, "lines"),
strip.text.x = element_text(size = 8)
) +
xlab("") +
ylab("Value of Y") +
facet_wrap(~grp, ncol=1, scale="free_y")
fig5
3、變體:一個類型兩個值
在棒棒糖圖中,如果一個類別有兩個數(shù)值,可以很方便地使用一條線段來表示二者間的差異。即在棒棒糖圖中geom_segment(aes(x = xv, xend = xv, y = 1, yend = yv)中,y的起始點不再是固定的度量y=1,而是該類別的另一變量示例6:
library(ggplot2)
library(dplyr)
yv1 = abs(rnorm(26))*2
data2 <- data.frame(
xv1 = LETTERS[1:26],
yv1 = yv1,
yv2 = yv1+1+rnorm(26, sd=1)
)
fig6 = ggplot(data2) +
geom_segment( aes(x=xv1, xend=xv1, y=yv1, yend=yv2),
size=0.7,linetype=1,color="grey")+ # 此處y起始點為yv1,終止點為yv2
geom_point(aes(x=xv1,y=yv1),size = 4, pch = 20, color="#66CCEE") +
geom_point(aes(x=xv1,y=yv2),size = 4, pch = 20, color="#EE6677") +
xlab("26Letters") +
scale_y_continuous("Y_Values",breaks =c(0,1,2,3,4,5,6,7)) +
theme_minimal()
fig6
示例6這樣的圖形也被稱之為“啞鈴圖”,并且已有成熟的函數(shù)可以繪制:ggalt::geom_dumbbell(),該函數(shù)的相關(guān)參數(shù)含義及用法和示例參見官方文檔:https://yonicd.github.io/ggalt/reference/geom_dumbbell.html
下面給出使用該函數(shù)繪制“啞鈴圖”的示例:
示例7:geom_dumbbell()官方文檔示例
library(ggplot2)
library(ggalt)
df <- data.frame(trt=LETTERS[1:5], l=c(20, 40, 10, 30, 50), r=c(70, 50, 30, 60, 80))
bbfig3 = ggplot(df, aes(y=trt, x=l, xend=r)) +
geom_dumbbell(size=3, color="#e3e2e1",
colour_x = "#5b8124", colour_xend = "#bad744",
dot_guide=TRUE, dot_guide_size=0.25) +
labs(x=NULL, y=NULL, title="ggplot2 geom_dumbbell with dot guide") +
theme_minimal() +
theme(panel.grid.major.x=element_line(size=0.05))
bbfig3
示例8:geom_dumbbell()繪制啞鈴圖
llibrary(ggalt)
health <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/health.csv")
health$Area <- factor(health$Area, levels=as.character(health$Area))
bbfig2 = ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area)) +
geom_dumbbell()+
theme_minimal()
bbfig2
library(ggalt)
library(ggplot2)
bbfig3 = ggplot(health, aes(x=pct_2013, xend=pct_2014, y=Area)) +
geom_segment(aes(x=pct_2013,
xend=pct_2014,
y=Area,
yend=Area),
color="#b2b2b2", size=1.5)+
geom_dumbbell(color="light blue",
size_x=3.5,
size_xend = 3.5,
colour_x="#edae52",
colour_xend = "#9fb059")+
labs(x=NULL, y=NULL,
title="Dumbbell Chart",
subtitle="Pct Change: 2013 vs 2014")+
geom_text(color="black", size=2, hjust=-0.5,
aes(x=pct_2013, label=pct_2013))+
geom_text(aes(x=pct_2014, label=pct_2014),
color="black", size=2, hjust=1.5)+
theme_minimal()
bbfig3
參考: