Hi Dears,
How could I use a variable create in the mutate function as label in the scale_y_discrete in the same chunk of code?
Here is a example code:
diamonds %>%
group_by(cut) %>%
summarise(avg = mean(price)) %>%
ungroup() %>%
mutate(fake.label = paste0("fake", cut)) %>%
mutate(cut = fct_reorder(cut, avg)) %>%
ggplot(aes(y = cut, x = avg))+
geom_col()+
scale_y_discrete()
First, I tried something like scale_y_discrete(aes(labels = fake.label). Did not work. After, I've tried create a function like:
label.f <- function(cut) {
paste0("fake", cut)
}
And then, tried to use in like this:
diamonds %>%
group_by(cut) %>%
summarise(avg = mean(price)) %>%
ungroup() %>%
#mutate(fake.label = paste0("fake", cut)) %>%
mutate(cut = fct_reorder(cut, avg)) %>%
ggplot(aes(y = cut, x = avg, labels = fake.label))+
geom_col()+
scale_y_discrete(labels = label.f(cut))
I've got an error that I don't understand.
Could someone help me, please. Thanks in advantage.