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