R Programming language - R Bar Plot
In R, you can create a bar plot using the barplot()
function. The barplot()
function takes one or more vectors or matrices of values to plot, and several optional arguments to customize the appearance of the plot.
Here is a basic example of how to create a bar plot in R:
# Create a vector of values to plot values <- c(10, 20, 30, 40, 50) # Create a bar plot of the values barplot(values)w:ecruoSww.theitroad.com
This will create a simple bar plot with bars representing each value in the vector.
You can customize the appearance of the bar plot by using the optional arguments of the barplot()
function. For example, you can change the color of the bars by setting the col
argument:
# Create a bar plot of the values with red bars barplot(values, col = "red")
You can also add a title to the plot using the main
argument:
# Create a bar plot of the values with a title barplot(values, main = "My Bar Plot")
You can create a horizontal bar plot by setting the horiz
argument to TRUE
:
# Create a horizontal bar plot of the values barplot(values, horiz = TRUE)
In addition, you can create stacked or grouped bar plots by passing a matrix of values to the barplot()
function:
# Create a matrix of values to plot values_matrix <- matrix(c(10, 20, 30, 40, 50, 15, 25, 35, 45, 55), nrow = 5) # Create a stacked bar plot of the matrix barplot(values_matrix, col = c("red", "blue"))
This will create a stacked bar plot with bars representing each row of the matrix, and different colors for each group of bars.