Next →

Output:


      

Plots:

Bar Plots

Bar plots are used to compare categorical data or discrete values. In R, you can create bar plots using the base barplot() function or ggplot2.

Basic Bar Plot

# Sample data
categories <- c("A", "B", "C", "D")
values <- c(5, 8, 2, 6)

# Create a bar plot
barplot(values, names.arg = categories, main = "Basic Bar Plot", col = "blue")

Using ggplot2

library(ggplot2)

df <- data.frame(category = categories, value = values)

ggplot(df, aes(x = category, y = value)) +
  geom_bar(stat = "identity", fill = "red") +
  ggtitle("Bar Plot with ggplot2") +
  theme_minimal()

Bar plots are ideal for comparing quantities across different groups or categories.