YouTip LogoYouTip

R Func Barplot

# R barplot() Function - Drawing Bar Charts [![Image 3: R Language Examples](#) R Language Examples](#) The R barplot() function is used to create bar charts, suitable for displaying comparisons of categorical data. The height of the bars represents the numerical value, commonly used for comparisons between different categories. The syntax format of the barplot() function is as follows: barplot(height, names.arg = NULL, col = NULL, main = "", xlab = "", ylab = "", horiz = FALSE) **Parameter Description:** * **height** Vector or matrix of bar heights. * **names.arg** Labels below each bar. * **horiz** Whether to draw a horizontal bar chart. ## Example # Product sales data products <-c("ProductA", "ProductB", "ProductC", "ProductD", "ProductE") sales <-c(120, 85, 150, 90, 110) # Vertical bar chart barplot(sales, names.arg= products, main ="Sales Volume by Product", xlab ="Product", ylab ="Sales Volume", col=c("steelblue", "coral", "seagreen", "gold", "purple")) Executing the above code will display a colored bar chart comparing the sales volume of each product. barplot() can also draw grouped bar charts: ## Example # Grouped data: sales comparison for two quarters quarter_data <-matrix(c(120, 85, 150, 90, 110, 130, 90, 140, 95, 125), nrow=2, byrow = TRUE) rownames(quarter_data)<-c("Q1", "Q2") colnames(quarter_data)<-c("A", "B", "C", "D", "E") # Grouped bar chart barplot(quarter_data, beside = TRUE, legend=rownames(quarter_data), main ="Quarterly Product Sales Comparison", col=c("steelblue", "coral")) Executing the above code will display a grouped comparison chart of product sales volumes across two quarters. [![Image 4: R Language Examples](#) R Language Examples](#)
← R Func CeilingR Func Apply β†’