柱狀圖又叫條形圖,是數據展示最重要的一類統計圖,數據分析結果展示中使用頻率非常高,各類統計軟件均能繪制。在R語言中r語言 條形圖上有數值,有很多包可繪制柱狀圖,比如包()函數和包()函數。 本文介紹包的()函數繪制柱狀圖。
()函數的基本用法:
barplot(height, # 柱子的高度
names.arg = NULL, # 柱子的名稱
col = NULL, # 柱子的填充顏色
border = par("fg"), # 柱子的輪廓顏色
main = NULL, # 柱狀圖主標題
xlab = NULL, # X軸標簽
ylab = NULL, # Y軸標簽
xlim = NULL, # X軸取值范圍
ylim = NULL, # Y軸取值范圍
horiz = FALSE, # 柱子是否為水平
legend.text = NULL, # 圖例文本
beside = FALSE, # 柱子是否為平行放置,用的頻率低,本文不介紹
)
柱子的高度是必須要的參數,數據類型為數值型向量或者矩陣。如果是一個數值型向量,那么向量中的每個數字即為每個柱子的高度,適用于繪制單個變量的柱狀圖;如果傳入一個矩陣,那么矩陣的每一列都對應一個柱子,柱子的高度為每一列的數字之和r語言 條形圖上有數值,每個柱子內部根據每一行數字的不同進行了劃分,適用于兩個變量交叉的柱狀圖。
#同一界面顯示多張圖
par(mfcol=c(1,2))
# 傳入數值型向量
vector = c(6, 4, 8) # 繪圖數據(數值型向量)
barplot(height = vector) # 繪制條形圖
# 傳入矩陣
matrix = matrix(1:4, ncol = 2, nrow = 2) # 繪圖數據(矩陣)
barplot(height = matrix) # 繪制條形圖
數值型向量圖(左)和矩陣圖(右)
#修改柱子的名稱
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('A', 'B'), # 柱子名稱
)
添加柱子名稱的柱狀圖
#修改柱子的顏色(填充和輪廓)
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = "white", #柱子顏色為白色
border = "black" #柱子邊框為黑色
)
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = "green", #柱子顏色為綠色
border = "black" #柱子邊框為黑
)
##修改堆積圖的顏色(填充和輪廓
#修改柱子的顏色(填充和輪廓)
barplot(height = matrix(1:4, ncol = 2, nrow = 2), # 繪圖數據(矩陣)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = c('red', 'green'), # 填充顏色
border = 'black') # 輪廓顏色
barplot(height = matrix(1:4, ncol = 2, nrow = 2), # 繪圖數據(矩陣)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = c('white', 'green'), # 填充顏色
border = 'black') # 輪廓顏色
白色和綠色填充的柱狀圖
顏色填充的堆積柱狀圖
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = 'red', # 填充顏色
border = 'black', # 輪廓顏色
xlab = '處理', # X軸標題
ylab = '物種豐富度(/m^2)') # Y軸標題
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = 'green', # 填充顏色
border = 'black', # 輪廓顏色
xlab = '處理', # X軸標題
ylab = '產量(kg/m^2)') # Y軸標題
物種豐富度和產量柱狀圖
#修改X和Y軸坐標軸取值范圍
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = 'red', # 填充顏色
border = 'black', # 輪廓顏色
xlab = '處理', # X軸標題
ylab = '物種豐富度(/m^2)', # Y軸標題
ylim = c(0, 50)) # Y軸范圍
barplot(height = c(20, 48), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = 'green', # 填充顏色
border = 'black', # 輪廓顏色
xlab = '處理', # X軸標題
ylab = '產量(kg/m^2)', # Y軸標題
ylim = c(0, 200)) # Y軸范圍)
修改X和Y軸坐標軸的柱狀圖
#設置圖例的內容和位置
barplot(height = c(50, 30), # 繪圖數據(數值型向量)
names.arg = c('Control', 'N addition'), # 柱子名稱
col = c('white','black'), # 填充顏色
border = 'black', # 輪廓顏色
xlab = '處理', # X軸標題
ylab = '物種豐富度(/m^2)', # Y軸標題
ylim = c(0, 50), # Y軸范圍
legend.text = c('Control','N addition'),#設置圖例的內容
args.legend = list(x = "topright")) #修改圖例的位置
添加圖例的柱狀圖
()函數繪制柱狀圖較為美觀,但是存在一定的缺陷,比如圖例的位置只有固定的幾個參數(如下圖),無法自定義設置位置:
圖例位置參數
附官方示例代碼:
Examples
# Formula method
barplot(GNP ~ Year, data = longley)
barplot(cbind(Employed, Unemployed) ~ Year, data = longley)
## 3rd form of formula - 2 categories :
op <- par(mfrow = 2:1, mgp = c(3,1,0)/2, mar = .1+c(3,3:1))
summary(d.Titanic <- as.data.frame(Titanic))
barplot(Freq ~ Class + Survived, data = d.Titanic,
subset = Age == "Adult" & Sex == "Male",
main = "barplot(Freq ~ Class + Survived, *)", ylab = "# {passengers}", legend = TRUE)
# Corresponding table :
(xt <- xtabs(Freq ~ Survived + Class + Sex, d.Titanic, subset = Age=="Adult"))
# Alternatively, a mosaic plot :
mosaicplot(xt[,,"Male"], main = "mosaicplot(Freq ~ Class + Survived, *)", color=TRUE)
par(op)
# Default method
require(grDevices) # for colours
tN <- table(Ni <- stats::rpois(100, lambda = 5))
r <- barplot(tN, col = rainbow(20))
#- type = "h" plotting *is* 'bar'plot
lines(r, tN, type = "h", col = "red", lwd = 2)
barplot(tN, space = 1.5, axisnames = FALSE,
sub = "barplot(..., space= 1.5, axisnames = FALSE)")
barplot(VADeaths, plot = FALSE)
barplot(VADeaths, plot = FALSE, beside = TRUE)
mp <- barplot(VADeaths) # default
tot <- colMeans(VADeaths)
text(mp, tot + 3, format(tot), xpd = TRUE, col = "blue")
barplot(VADeaths, beside = TRUE,
col = c("lightblue", "mistyrose", "lightcyan",
"lavender", "cornsilk"),
legend = rownames(VADeaths), ylim = c(0, 100))
title(main = "Death Rates in Virginia", font.main = 4)
hh <- t(VADeaths)[, 5:1]
mybarcol <- "gray20"
mp <- barplot(hh, beside = TRUE,
col = c("lightblue", "mistyrose",
"lightcyan", "lavender"),
legend = colnames(VADeaths), ylim = c(0,100),
main = "Death Rates in Virginia", font.main = 4,
sub = "Faked upper 2*sigma error bars", col.sub = mybarcol,
cex.names = 1.5)
segments(mp, hh, mp, hh + 2*sqrt(1000*hh/100), col = mybarcol, lwd = 1.5)
stopifnot(dim(mp) == dim(hh)) # corresponding matrices
mtext(side = 1, at = colMeans(mp), line = -2,
text = paste("Mean", formatC(colMeans(hh))), col = "red")
# Bar shading example
barplot(VADeaths, angle = 15+10*1:5, density = 20, col = "black",
legend = rownames(VADeaths))
title(main = list("Death Rates in Virginia", font = 4))
# Border color
barplot(VADeaths, border = "dark blue")
# Log scales (not much sense here)
barplot(tN, col = heat.colors(12), log = "y")
barplot(tN, col = gray.colors(20), log = "xy")
# Legend location
barplot(height = cbind(x = c(465, 91) / 465 * 100,
y = c(840, 200) / 840 * 100,
z = c(37, 17) / 37 * 100),
beside = FALSE,
width = c(465, 840, 37),
col = c(1, 2),
legend.text = c("A", "B"),
args.legend = list(x = "topleft"))
本文介紹包()函數繪制柱狀圖,下篇文章介紹包()函數繪制柱狀圖。
參考文獻
[1]
[2] , R. A., , J. M. and Wilks, A. R. (1988) The New S . & /Cole.
[3] , P. (2005) R . & Hall/CRC Press.