R語言--條形圖與箱線圖

條形圖

R語言使用函數(shù)barplot()創(chuàng)建條形圖。

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

另外,超過兩個變量表示為用于創(chuàng)建組合條形圖和堆疊條形圖的矩陣。即使用matrix代替H向量,同時可以添加注釋與圖例(legend)。

# 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()

箱線圖

箱線圖是數(shù)據(jù)集中的數(shù)據(jù)分布良好的度量。 它將數(shù)據(jù)集分成三個四分位數(shù)。 此圖表表示數(shù)據(jù)集中的最小值,最大值,中值,第一四分位數(shù)和第三四分位數(shù)。 它還可用于通過繪制每個數(shù)據(jù)集的箱線圖來比較數(shù)據(jù)集之間的數(shù)據(jù)分布。R語言中使用boxplot()函數(shù)來創(chuàng)建箱線圖。

boxplot(x, data, notch, varwidth, names, main)
  • x是向量或公式。
  • 數(shù)據(jù)是數(shù)據(jù)幀。
  • notch是邏輯值。 設(shè)置為TRUE以繪制凹口。
  • varwidth是一個邏輯值。 設(shè)置為true以繪制與樣本大小成比例的框的寬度。
  • names是將打印在每個箱線圖下的一組標(biāo)簽。
  • main用于給圖表標(biāo)題。
# Give the chart file a name.
png(file = "boxplot.png")

# Plot the chart.
boxplot(mpg ~ cyl, data = mtcars, xlab = "Number of Cylinders",
   ylab = "Miles Per Gallon", main = "Mileage Data")

# Save the file.
dev.off()

參考

https://www.w3cschool.cn/

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容