Adding additional values/variables in axis labels

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!

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(y = reorder(name, share), x = share)) +
    geom_col() +
    labs(x = "Merchant",
         y = "Share") +
    scale_y_discrete(labels = paste0(col$name, " (", col$active_days, ")"))

Created on 2021-10-21 by the reprex package (v2.0.0)

So close! Thank you for the quick reply!

Using the solution on my larger, actual dataset, I realised a strange behaviour when adding scale_y_discrete. I'll illustrate with a reprex below.

Assume I have the following data:

library(tidyverse)

set.seed(123)

col <- tibble(
  name = toupper(letters[1:20]),
  share = round(rnorm(20, 0.5, 0.1), 2),
  active_days = sample.int(50, 20)
)

Plotting this as per the code above without using scale_y_discrete(), we get:

col %>%
  ggplot(aes(y = reorder(name, share), x = share)) +
  geom_col() +
  labs(x = "Share",
       y = "Merchant")

Now, adding scale_y_discrete:

col %>%
  ggplot(aes(y = reorder(name, share), x = share)) +
  geom_col() +
  labs(x = "Share",
       y = "Merchant") +
  scale_y_discrete(labels = paste0(col$name, " (", col$active_days, ")"))

Adding scale_y_discrete changes the order of the labels to reverse alphabetical order, but the values/bars are displayed correctly in descending order, giving the impression, in this case, that merchant T has the highest share, while it is in fact merchant P! This is obviously highly unwanted. Anyone got any idea what is happening here, and how it can be remedied?

I asked this on Stack Overflow and it was answered, anyone interested in the solution can find it here.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.