0

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

2
  • Why not use Sepal.length as a y-axis but first paste it and then strsplit it? stackoverflow.com/questions/41336606/… provides an answer on how to access elements of strsplit Commented May 10, 2021 at 11:12
  • I've used Sepal.Length because my problem variable, has characters + numeric. And I only want the numeric one. I cannot edit this variable on dataframe, because color variable needs all character+numeric, because color value is a named vector with character+numeric and I shouldn't edit those names, because it would contain duplicates Commented May 10, 2021 at 11:18

2 Answers 2

1

It seems to me that you function is only applied to the first row of the column. That value is replicated. Using lapply returns all the unique values. However, I don't know if it makes sense in this example without making it numeric (and sorting it) so you might want to add that as well.

ggplot(iris, aes(x = Species, y = trial, color = Species)) +
  geom_point() +
  scale_y_discrete(labels = lapply(iris$trial, myfun)) +
  theme_bw()
Sign up to request clarification or add additional context in comments.

1 Comment

Awesome!! lapply solved the problem. I thought it would be applied automatically, but recursive is needed here. Thanks
0

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).

Comments

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.