Search code examples
rggplot2axis-labels

ggplot how to edit axis labels in R


I have a dataframe to plot where y_axis variable is a character one. I want to take only the last part of character with '_' as separation.

Here an example with iris dataset. As you can see, all y_axis labels are the same. How can I do it? thanks

iris$trial = paste('hello', 'good_bye', iris$Sepal.Length, sep = '_')

myfun = function(x) {
  tail(unlist(strsplit(x, '_')), n = 1)
}

ggplot(iris, aes(x = Species, y = trial, color = Species)) +
  geom_point() +
  scale_y_discrete(labels = function(x) myfun(x)) +
  theme_bw()

enter image description here


Solution

  • You can make use of regex instead to extract the required value.

    library(ggplot2)
    
    #This removes everything until the last underscore
    myfun = function(x) sub('.*_', '', x)
    
    ggplot(iris, aes(x = Species, y = trial, color = Species)) +
      geom_point() +
      scale_y_discrete(labels = myfun) +
      theme_bw()
    

    enter image description here

    If you want to extract numbers from y-axis value, you can also use scale_y_discrete(labels = readr::parse_number).