I have a dataset cars which describes how common are brands of cars sold at two dealerships.
cars <- data.frame(dealership = rep(c("a", "b"), 3), car = rep(c("toyota", "ford", "opel"), each = 2), percent = c(10, 65, 40, 5, 50, 30))
I want to visualise this data using stacked barplot, with two bars, one for each dealerships. The car brands should be ordered in the bar from hghest sales to the lowest sale.
I tried running the following code.
#Arrange cars by dealership and sort them
cars <- cars %>%
group_by(dealership) %>%
arrange(percent, .by_group = TRUE)
#Create the stacked barplot
ggplot(cars, aes(x = dealership, y = percent, fill = car)) +
geom_bar(stat = "identity") +
labs(title = "Car brands sold by dealership",
x = "Dealership",
y = "Percent") +
coord_flip() +
theme_minimal() +
scale_fill_brewer(palette = "Set2")
However, only the dealership B got sorted and displayed correctly - dealership A was displayed with the variables according to the order of those in B (toyota-opel-ford), rather than the correct ones (opel-ford-toyota). Is there a way to sort the data in such way that this would be possible?
Original plot with stacked bars ordered by overall values of the same variable:
To reorder levels of a variable within another variable, you can use the function reorder_within()
from {tidytext}
, that creates a new variable with the desired order within the second variable (in this case, dealership
).
The resulting variable will look like this:
# A tibble: 6 × 2
# Groups: dealership [2]
dealership car2
<chr> <fct>
1 a toyota___a
2 a ford___a
3 a opel___a
4 b ford___b
5 b opel___b
6 b toyota___b
Then we can use that variable as the group
aesthetic in the plot, and continue using car
as the fill
aesthetic.
cars |>
mutate(car2 = tidytext::reorder_within(car, percent, within = dealership)) |>
ggplot(aes(x = dealership, y = percent, group = car2, fill = car)) +
geom_bar(stat = "identity") +
labs(title = "Car brands sold by dealership",
x = "Dealership",
y = "Percent") +
coord_flip() +
theme_minimal() +
scale_fill_brewer(palette = "Set2")
Resulting plot with ordered stacked bars for each value of a second variable: