欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

R語言條形圖創(chuàng)建方法

 更新時(shí)間:2021年04月11日 13:27:12   作者:w3cschool  
在本篇文章里小編給大家整理的是一篇關(guān)于R語言條形圖創(chuàng)建方法,有需要的朋友們可以跟著學(xué)習(xí)參考下。

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

語法

在 R 語言中創(chuàng)建條形圖的基本語法是

  • H 是包含在條形圖中使用的數(shù)值的向量或矩陣。
  • xlab 是 x 軸的標(biāo)簽。
  • ylab 是 y 軸的標(biāo)簽。
  • main 是條形圖的標(biāo)題。
  • names.arg 是在每個(gè)條下出現(xiàn)的名稱的向量。
  • col 用于向圖中的條形提供顏色。
barplot(H, xlab, ylab, main, names.arg, col)

以下是所使用的參數(shù)的描述 - 

使用輸入向量和每個(gè)條的名稱創(chuàng)建一個(gè)簡單的條形圖。
以下腳本將創(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é)果 -

,使用R條形圖

條形圖標(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é)果 -

條形圖使用R型

組合條形圖和堆積條形圖

我們可以使用矩陣作為輸入值,在每個(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()

,使用R堆疊條形圖

到此這篇關(guān)于R語言條形圖創(chuàng)建方法的文章就介紹到這了,更多相關(guān)R語言條形圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論