新聞中心
條形圖表示矩形條中的數(shù)據(jù),條的長(zhǎng)度與變量的值成比例。 r語言使用函數(shù) barplot() 創(chuàng)建條形圖。 R 語言可以在條形圖中繪制垂直和水平條。 在條形圖中,每個(gè)條可以給予不同的顏色。

讓客戶滿意是我們工作的目標(biāo),不斷超越客戶的期望值來自于我們對(duì)這個(gè)行業(yè)的熱愛。我們立志把好的技術(shù)通過有效、簡(jiǎn)單的方式提供給客戶,將通過不懈努力成為客戶在信息化領(lǐng)域值得信任、有價(jià)值的長(zhǎng)期合作伙伴,公司提供的服務(wù)項(xiàng)目有:空間域名、網(wǎng)站空間、營(yíng)銷軟件、網(wǎng)站建設(shè)、綏棱網(wǎng)站維護(hù)、網(wǎng)站推廣。
語法
在 R 語言中創(chuàng)建條形圖的基本語法是 -
barplot(H, xlab, ylab, main, names.arg, col)
以下是所使用的參數(shù)的描述 -
-
H 是包含在條形圖中使用的數(shù)值的向量或矩陣。
-
xlab 是 x 軸的標(biāo)簽。
-
ylab 是 y 軸的標(biāo)簽。
-
main 是條形圖的標(biāo)題。
-
names.arg 是在每個(gè)條下出現(xiàn)的名稱的向量。
-
col 用于向圖中的條形提供顏色。
例
使用輸入向量和每個(gè)條的名稱創(chuàng)建一個(gè)簡(jiǎn)單的條形圖。
以下腳本將創(chuàng)建并保存當(dāng)前 R 語言工作目錄中的條形圖。
# Create the data for the chart. H <- c(7,12,28,3,41) # Give the chart file a name. png(file = "barchart.png") # Plot the bar chart. barplot(H) # Save the file. dev.off()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
條形圖標(biāo)簽,標(biāo)題和顏色
可以通過添加更多參數(shù)來擴(kuò)展條形圖的功能。 主要參數(shù)用于添加標(biāo)題。 col 參數(shù)用于向條形添加顏色。 name.args 是具有與輸入向量相同數(shù)量的值的向量,以描述每個(gè)條的含義。
例
以下腳本將在當(dāng)前r語言工作目錄中創(chuàng)建并保存條形圖。
# Create the data for the chart.
H <- c(7,12,28,3,41)
M <- c("Mar","Apr","May","Jun","Jul")
# Give the chart file a name.
png(file = "barchart_months_revenue.png")
# Plot the bar chart.
barplot(H,names.arg = M,xlab = "Month",ylab = "Revenue",col = "blue",
main = "Revenue chart",border = "red")
# Save the file.
dev.off()
當(dāng)我們執(zhí)行上面的代碼,它產(chǎn)生以下結(jié)果 -
組合條形圖和堆積條形圖
我們可以使用矩陣作為輸入值,在每個(gè)條中創(chuàng)建條形圖和堆疊組的條形圖。
超過兩個(gè)變量表示為用于創(chuàng)建組合條形圖和堆疊條形圖的矩陣。
# Create the input vectors.
colors <- c("green","orange","brown")
months <- c("Mar","Apr","May","Jun","Jul")
regions <- c("East","West","North")
# Create the matrix of the values.
Values <- matrix(c(2,9,3,11,9,4,8,7,3,12,5,2,8,10,11),nrow = 3,ncol = 5,byrow = TRUE)
# Give the chart file a name.
png(file = "barchart_stacked.png")
# Create the bar chart.
barplot(Values,main = "total revenue",names.arg = months,xlab = "month",ylab = "revenue",
col = colors)
# Add the legend to the chart.
legend("topleft", regions, cex = 1.3, fill = colors)
# Save the file.
dev.off()
本文標(biāo)題:創(chuàng)新互聯(lián)R語言教程:R語言條形圖
本文路徑:http://fisionsoft.com.cn/article/djjhpoe.html


咨詢
建站咨詢
