R语言--条形图与箱线图

2019-07-29  本文已影响0人  Dear_Mozart

条形图

R语言使用函数barplot()创建条形图。

barplot(H, xlab, ylab, main, names.arg, col)

另外,超过两个变量表示为用于创建组合条形图和堆叠条形图的矩阵。即使用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()

箱线图

箱线图是数据集中的数据分布良好的度量。 它将数据集分成三个四分位数。 此图表表示数据集中的最小值,最大值,中值,第一四分位数和第三四分位数。 它还可用于通过绘制每个数据集的箱线图来比较数据集之间的数据分布。R语言中使用boxplot()函数来创建箱线图。

boxplot(x, data, notch, varwidth, names, main)
# 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/

上一篇 下一篇

猜你喜欢

热点阅读