visualization of variable by another var categories

# Step 1: Install and load necessary packages (if not already installed)
install.packages("ggplot2")
library(ggplot2)

# Step 2: Create a data frame with your variables
# Replace 'data' with your actual data frame name, and 'x' and 'y' with your variable names
data <- data.frame(x = c(1, 2, 3, 4, 5),
                   y = c(10, 15, 7, 22, 18),
                   category = c("A", "B", "A", "B", "A"))

# Step 3: Create a bar plot to visualize the variable by categories
ggplot(data, aes(x = category, y = y, fill = as.factor(x))) +
  geom_bar(stat = "identity", position = "dodge") +
  labs(title = "Variable by Categories",
       x = "Category",
       y = "Variable Value") +
  scale_fill_manual(values = c("1" = "blue", "2" = "red", "3" = "green", "4" = "orange", "5" = "purple")) +
  theme_minimal()