Reputation: 1
I have this table
I would like to do a barplot with ggplot and have a custom color for each of the bar. To do this I started with this code:
ggplot(GoalsAndFouls, aes(x = name, y = goals)) +
labs(x = "Leagues", y = "Goals") +
geom_bar(stat = "identity")
I tried to add colors with the code addition as shown below. The colors do not change though.
ggplot(GoalsAndFouls, aes(x = name, y = goals)) +
labs(x = "Leagues", y = "Goals") +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("red", "green", "blue", "grey", "orange")) +
theme(legend.position="none")
And could I at the fouls variable as an additional bar to each league?
Upvotes: 0
Views: 46
Reputation: 2321
Since you've already specified the colors and the order of the colors, you just need to add fill = name
to aes()
.
library(tidyverse)
name <- c("Serie A", "Premier League", "La Liga", "Ligue 1", "Bundesliga")
goals <- c(7476, 7213, 7072, 6656, 6324)
fouls <- c(74232, 57506, 73053, 67042, 56720)
GoalsAndFouls <- data.frame(name, goals, fouls)
ggplot(GoalsAndFouls, aes(x = name, y = goals, fill = name)) +
labs(x = "Leagues", y = "Goals") +
geom_bar(stat = "identity") +
scale_fill_manual(values = c("red", "green", "blue", "grey", "orange")) +
theme(legend.position="none")
Upvotes: 1