1

I have a question regarding the modification of axis labels in ggplot. I know that what I am trying to get at can be done via other means (e.g. creating a new column with case_when). My interest is in the approach/concept outlined below.

Let's say I have a discrete variable at the x-axis. In order to get the desired axis labels, the values are 1) modified with a function, and then 2) changed by assigning the values contained in a named vector to the result of the function. I am able to do each of the two steps individually; but how can they be combined? Can this be done?

Again, I am aware that there are other, probably more sane ways, to do this. And the example below doesn't make any substantive sense. I am just curious whether/how this can be done directly in the e.g. scales_x_discrete function. Below I am trying to detail what I mean. The result would be that all Merc labels become Mercedes, and all instances of Hornet become Super Hornet.

library(tidyverse)

labeller_cars <- c("Hornet"="Super Hornet",
                   "Merc"="Mercedes")

mtcars %>% 
  rownames_to_column(var = "name") %>% 
  filter(str_detect(name, regex("Hornet|Merc"))) %>% 
  ggplot()+
  geom_bar(aes(x=name,
               y=disp),
           stat="identity")+
  #here is what I am interested in; this works, but it's only first step
  scale_x_discrete(labels=function(x) str_extract(x, regex("[:alpha:]*")))
  #this is an attempt, but doesn't work.
  # scale_x_discrete(labels=function(x) str_extract(x, regex("[:alpha:]*")) %>% labeller_cars)

Created on 2021-07-24 by the reprex package (v2.0.0)

0

1 Answer 1

2

We could use str_replace_all instead of str_extract

library(dplyr)
library(stringr)
library(ggplot2)
labeller_cars <- c(".*Hornet.*"="Super Hornet",
                    ".*Merc.*"="Mercedes")
mtcars %>% 
   rownames_to_column(var = "name") %>% 
   filter(str_detect(name, regex("Hornet|Merc"))) %>% 
   ggplot()+
   geom_bar(aes(x=name,
                y=disp),
            stat="identity")+
   #here is what I am interested in; this works, but it's only first step
   scale_x_discrete(labels=function(x) str_replace_all(x, labeller_cars)) + 
 theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1))

-output

enter image description here


Or with str_extract, after extracting the substring, use the named vector to match and replace

labeller_cars <- c("Hornet"="Super Hornet",
                    "Merc"="Mercedes")
mtcars %>% 
   rownames_to_column(var = "name") %>% 
   filter(str_detect(name, regex("Hornet|Merc"))) %>% 
   ggplot()+
   geom_bar(aes(x=name,
                y=disp),
            stat="identity")+
   scale_x_discrete(labels=function(x)
      labeller_cars[str_extract(x, regex("[:alpha:]*"))])

-OUTPUT

enter image description here

Sign up to request clarification or add additional context in comments.

3 Comments

many thanks. I realize my example is not very good since with the intended results 2 columns would be called "Super Hornet" and all others would be called "Mercedes". What I am interested is to modify labels via a named vector AFTER having applied a function to the labels (here - str_extract, which lumps all Hornets and Merc together). –
many thanks for your reply and apologies if I am not clear enough. str_extract ìn my example results in multiple Hornet and Merc (since it takes only this part from the labels). This was the first step. Now I would like to match (?) these results with a named vector to get the final label. The function str_extract is only used here as an example. The point i am interested in is how can I use the outcome of a function (used in scale_x_discrete(labels=...) to match it w named vector. I work with examples where I use str_remove etc. I assume I somehow ve to integrate this into my fn.
the second solution is what I was looking for. thanks again.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.