0

The following example does not work, but I hope it is helpful to have and idea on what I'm trying to do --- change the label that meets the condition --- E.G., I would like to change one specific label as follows:

scale_color_manual(labels = function(x) {ifelse(x == "Target", as.character("This is the target"), x)})
Nip
  • 387
  • 4
  • 11
  • It didn't work, Jared. I came this question because `scale_y_continuous(labels=function(n){format(n*-1, scientific = FALSE)})` works. – Nip Feb 24 '22 at 02:14
  • It would be easier to help you if you provide [a minimal reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) including a snippet of your data or some fake data. From one line of code one can only guess what's the issue. – stefan Feb 24 '22 at 07:37

1 Answers1

1

Your approach seems to work just fine. Please update your question with a minimal reproducible example if the code below doesn't capture your use-case.

library(ggplot2)

df <- data.frame(
  x = c(1:2),
  y = c(1:2),
  col = c("Target", "Schmarget")
)

ggplot(df, aes(x, y, colour = col)) +
  geom_point() +
  scale_colour_manual(
    values = c("blue", "red"),
    labels = function(x) {
      ifelse(x == "Target", as.character("This is the target"), x)
    }
  )

Created on 2022-02-24 by the reprex package (v2.0.1)

teunbrand
  • 33,645
  • 4
  • 37
  • 63