Let's say I have the following data, together with the corresponding bar chart:
library(tidyverse)
col <- tibble(
name = c("Merchant 1", "Merchant 2", "Merchant 3"),
share = c(0.3, 0.5, 0.7),
active_days = c(10, 20, 30)
)
col %>%
ggplot(aes(x = reorder(name, +share), y = share)) +
geom_col() +
coord_flip() +
labs(x = "Merchant",
y = "Share")
Created on 2021-10-21 by the reprex package (v2.0.1)
How would I go about adding to each merchant's name on the X axis (Y axis in this flipped plot) the value of the active_days
column? For Merchant 3, for example, I want the label to say "Merchant 3 (30)". Basically, I am looking to add something similar to the following in the appropriate ggplot code structure: paste0(name, " (", active_days, ")")
.
Is it possible? I am thinking that what I want to manipulate is scale_x_discrete()
but haven't managed to get it working (it says it cannot find the variable names, error message below).
col %>%
ggplot(aes(x = reorder(name, +share), y = share)) +
geom_col() +
coord_flip() +
labs(x = "Merchant",
y = "Share") +
scale_x_discrete(labels = paste0(name, " (", active_days, ")"))
#> Error in paste0(name, " (", active_days, ")"): object 'name' not found
Thanks!