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

条形图

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

barplot(H, xlab, ylab, main, names.arg, col)
  • H是包含在条形图中使用的数值的向量或矩阵。
  • xlab是x轴的标签。
  • ylab是y轴的标签。
  • main是条形图的标题。
  • names.arg是在每个条下出现的名称的向量。
  • col用于向图中的条形提供颜色。
    若使用多个颜色或names,则使用向量赋值。

另外,超过两个变量表示为用于创建组合条形图和堆叠条形图的矩阵。即使用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)
  • x是向量或公式。
  • 数据是数据帧。
  • notch是逻辑值。 设置为TRUE以绘制凹口。
  • varwidth是一个逻辑值。 设置为true以绘制与样本大小成比例的框的宽度。
  • 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/

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容