Tao Yan
簡介
條形圖可以說是我們最常用的數(shù)據(jù)可視化方法了雷滚,通常用于展示不同分類條件下(在x軸上)某個數(shù)值型變量的取值(y軸上)册踩。繪制條形圖時需要特別注意的一個細節(jié)是條形圖的條形高度有時表示的是數(shù)據(jù)集中變量的頻數(shù)尽狠,有時表示的則是變量本身。本文將會介紹這兩類條形圖的繪圖技巧。
繪制條形圖
使用ggplot()
函數(shù)與geom_bar(stat="identity")
,繪制條形圖戴差,我們將利用gcookbook包中的數(shù)據(jù)進行繪制。
#沒安裝包要先安裝包gcookbook铛嘱、ggplot2以及dplyr
library(gcookbook)#加載gcookbook以使用其包含的數(shù)據(jù)
library(ggplot2)#用于可視化
library(dplyr)#用于數(shù)據(jù)處理
這里我們調(diào)用**gcookbook**里的數(shù)據(jù)集繪制條形圖
head(pg_mean)#查看數(shù)據(jù)集
## group weigh
1 ctrl 5.032
2 trt1 4.661
3 trt2 5.526
ggplot(data=pg_mean, aes(x=group, y=weight))+#將group暖释、weight分別賦值給x、y軸
geom_bar(stat = "identity")#必須將geom_bar()中的stat(統(tǒng)計變換)參數(shù)設(shè)置
為”identity“墨吓,即對原始數(shù)據(jù)集不作任何統(tǒng)計變換球匕,而該參數(shù)的默認值為'count',即觀測數(shù)量帖烘。
當x是連續(xù)型(數(shù)值型)變量時亮曹,條形圖略有不同,需要略作調(diào)整秘症,具體如下:
str(BOD)#查看BOD數(shù)據(jù)集可以發(fā)現(xiàn)Time變量是數(shù)值型
## 'data.frame': 6 obs. of 2 variables:
## $ Time : num 1 2 3 4 5 7
## $ demand: num 8.3 10.3 19 16 15.6 19.8
## - attr(*, "reference")= chr "A1.4, p. 270"
ggplot(data=BOD, aes(x=Time, y=demand))+ geom_bar(stat = "identity")#此時Time是數(shù)值型
ggplot(data=BOD, aes(x=factor(Time), y=demand))+
geom_bar(stat = "identity")#將Time轉(zhuǎn)換為因子型(分類/離散變量)照卦,仔細比較兩圖
條形圖顏色有兩部分:填充顏色(fill)以及邊框顏色(color),因此調(diào)整條形圖顏色要調(diào)兩部分乡摹,具體如下:
ggplot(data=BOD, aes(x=factor(Time), y=demand))+
geom_bar(stat = "identity", fill="blue", color="black")#可以自己設(shè)定喜好的顏色
繪制簇狀條形圖
方法:將分類變量映射到fill參數(shù)役耕,運用geom_bar(position="dodge")
繪制,具體如下:
head(cabbage_exp)#查看數(shù)據(jù)趟卸,發(fā)現(xiàn)含有兩個分類變量:`Cultivar`和`Date`以及一個連續(xù)型變量Weight
## Cultivar Date Weight sd n se
## 1 c39 d16 3.18 0.9566144 10 0.30250803
## 2 c39 d20 2.80 0.2788867 10 0.08819171
## 3 c39 d21 2.74 0.9834181 10 0.31098410
## 4 c52 d16 2.26 0.4452215 10 0.14079141
## 5 c52 d20 3.11 0.7908505 10 0.25008887
## 6 c52 d21 1.47 0.2110819 10 0.06674995
ggplot(data=cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+#分別將Date與Cultivar映射給x和fill
geom_bar(stat = "identity", position = "dodge")#position = "dodge"表示條形圖分開不重疊(簇形圖)蹄葱,默認的為stack(堆疊式),還有百分比堆疊式(fill)
ggplot(data=cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+
geom_bar(stat = "identity", position = "stack")#堆疊式
ggplot(data=cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+
geom_bar(stat = "identity", position = "fill")#百分比堆疊式
設(shè)置顏色或者調(diào)用調(diào)色板
ggplot(data=cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+
geom_bar(stat = "identity", position = "dodge", color="black")+
scale_fill_brewer(palette = "Set1")#Set1為調(diào)色板,后期將會專門講解Color設(shè)置
ggplot(data=cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+
geom_bar(stat = "identity", position = "dodge", color="black")+
scale_fill_manual(values = c("darkred", "purple"))#自設(shè)置顏色
繪制頻數(shù)條形圖
head(diamonds,n=10)#查看前10行數(shù)據(jù)
## # A tibble: 10 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
## 7 0.24 Very Good I VVS1 62.3 57 336 3.95 3.98 2.47
## 8 0.26 Very Good H SI1 61.9 55 337 4.07 4.11 2.53
## 9 0.22 Fair E VS2 65.1 61 337 3.87 3.78 2.49
## 10 0.23 Very Good H VS1 59.4 61 338 4.00 4.05 2.39
ggplot(diamonds, aes(x=cut))+#此時不要映射任何變量到y(tǒng)
geom_bar()#等價于geom_bar(stat="bin")
繪制y軸正負軸都有數(shù)據(jù)的條形圖
#首先先創(chuàng)建一下數(shù)據(jù)集
set.seed(1111)#此命令保證數(shù)據(jù)結(jié)果可以重現(xiàn)在任何電腦上
x <- 1980+1:36#賦值x
y <- round(100*rnorm(36))#賦值y
mydata <- data.frame(x=x, y=y)#創(chuàng)建數(shù)據(jù)集mydata
head(mydata)#查看數(shù)據(jù)集
## x y
## 1 1981 -9
## 2 1982 132
## 3 1983 64
## 4 1984 117
## 5 1985 12
## 6 1986 -293
mydata <- mydata%>%#%>%管道操作锄列,結(jié)合dplyr為數(shù)據(jù)處理神器
mutate(judge=ifelse(y>=0,"Yes", "No"))#創(chuàng)建judge變量图云,將y正負分類
head(mydata)#查看數(shù)據(jù)
## x y judge
## 1 1981 -9 No
## 2 1982 132 Yes
## 3 1983 64 Yes
## 4 1984 117 Yes
## 5 1985 12 Yes
## 6 1986 -293 No
#接下來繪制條形圖
ggplot(data=mydata, aes(x=x, y=y, fill=judge))+
geom_bar(stat = "identity",position = "identity")+#這里position="identity"可以避免系統(tǒng)對負值繪制條形圖發(fā)出警告信息
scale_fill_manual(values = c("purple", "blue"), guide=FALSE)+xlab("Year")#guide=FALSE表示不要圖例,x軸標題為Year
通過width來調(diào)整條形寬度以及條形距離
head(diamonds)
## # A tibble: 6 × 10
## carat cut color clarity depth table price x y z
## <dbl> <ord> <ord> <ord> <dbl> <dbl> <int> <dbl> <dbl> <dbl>
## 1 0.23 Ideal E SI2 61.5 55 326 3.95 3.98 2.43
## 2 0.21 Premium E SI1 59.8 61 326 3.89 3.84 2.31
## 3 0.23 Good E VS1 56.9 65 327 4.05 4.07 2.31
## 4 0.29 Premium I VS2 62.4 58 334 4.20 4.23 2.63
## 5 0.31 Good J SI2 63.3 58 335 4.34 4.35 2.75
## 6 0.24 Very Good J VVS2 62.8 57 336 3.94 3.96 2.48
ggplot(data=diamonds, aes(x=color, y=price, fill=cut))+
geom_bar(stat = "identity", width = 0.6, position = position_dodge(0.8))+#調(diào)整條形寬度以及條形距離
scale_fill_brewer(palette = "Set1")
geom_text()
添加數(shù)據(jù)標簽
使用geom_text()
為條形圖添加標簽,需要分別指定一個變量映射給x邻邮、y以及標簽(label)竣况,vjust
和hjust
分別調(diào)整標簽的豎直和水平位置。
#標簽在條形圖頂端下方
ggplot(data=cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight))+
geom_bar(stat = "identity")+
geom_text(aes(label=Weight), vjust=1.5, color="white")
#標簽在條形圖頂端上方
ggplot(data=cabbage_exp, aes(x=interaction(Date, Cultivar), y=Weight))+
geom_bar(stat = "identity")+
geom_text(aes(label=Weight), vjust=-0.3, color="red")#可以通過color筒严、size等自行調(diào)整標簽屬性
#堆疊圖也一樣
ggplot(data=cabbage_exp, aes(x=Date, y=Weight, fill=Cultivar))+
geom_bar(stat="identity", position = "stack")+
geom_text(aes(label=Weight), size=5, color="black", vjust=3.5, hjust=0.5,
position = position_stack())#這里的position要與geom_bar()里面的保持一致丹泉,各種參數(shù)多調(diào)整才能效果最佳
下次將重點講解如何添加誤差棒、顯著性標記鸭蛙、坐標軸標題摹恨、圖標題以及部分小技巧等等