I have the following bar chart:
I did this by using the following code:
NL_population <- ggplot(data = NL, aes(reorder(Country, -`Population (g)`, sum), `Population (g)`))
geom_col(fill = "#0099f9")
labs(title = "Average population growth in %",
subtitle = "Years: 2017-2021",
x = "",
y = "Population growth")
geom_hline(yintercept = mean(NL$`Population (g)`), linetype = "dashed", size = 1) coord_flip()
I would like to have the Netherlands another colour than the other countries. What is the best way to achieve this?
CodePudding user response:
Besides the option mentioned by @shafee in his comment a simple approach would be to map a condition for the country to highlight on the fill
aes and set the your desired colors via scale_fill_manual
.
Using some fake example data based on the gapminder
dataset:
library(gapminder)
library(dplyr, warn.conflicts = FALSE)
countries <- c("Netherlands", "Italy", "Belgium", "United States", "Iceland", "Ireland")
NL <- gapminder |>
filter(year %in% c(2007, 2002), country %in% countries) |>
group_by(Country = country) |>
summarise(`Population (g)` = 100 * (last(pop) / first(pop) - 1))
library(ggplot2)
ggplot(data = NL, aes(reorder(Country, -`Population (g)`, sum), `Population (g)`))
geom_col(aes(fill = Country != "Netherlands"))
scale_fill_manual(values = c("orange", "#0099f9"), guide = "none")
labs(
title = "Average population growth in %",
subtitle = "Years: 2017-2021",
x = "",
y = "Population growth"
)
geom_hline(yintercept = mean(NL$`Population (g)`), linetype = "dashed", size = 1)
coord_flip()
#> Warning: Using `size` aesthetic for lines was deprecated in ggplot2 3.4.0.
#> ℹ Please use `linewidth` instead.