3

I am trying to change (not to remove) the "a" that gets displayed by geom_label_repel in the legend. I already found the following Thread: Why does text appear in the legend? that told me how to remove it.

library(ggrepel)

data <- data.frame(xVal,yVal, stringsAsFactors = FALSE)    
plot <- ggplot(data, aes(x=xVal, y=yVal)) +
      geom_point() + 
      geom_label_repel( aes( label=pointName, fill=factor( yVal ) ), nudge_x = 1.25, nudge_y = 1.2 ) +
      scale_fill_manual(values=colorPallet, labels = yVal) 

This Code gives me the following image: enter image description here

I want to change it so that the "a" in the legend is displaying the numbers i have in the vector pointName. (that are the numbers in the nicture that range from 48 to 96).

Thank you all.

uv239
  • 135
  • 10
  • Possible duplicate of [How to have "a" removed from a ggraph plot legend?](https://stackoverflow.com/questions/49989158/how-to-have-a-removed-from-a-ggraph-plot-legend) – NelsonGon Dec 18 '18 at 14:51
  • I dont want to remove the "a" i want to change it. – uv239 Dec 18 '18 at 15:01
  • The answer to that question shows a way to change it. – NelsonGon Dec 18 '18 at 15:06
  • @NelsonGon could you explain to me how? i dont see where i would need to add something to be able to change it. Or lets say i am sure it is in the drawKey function, but idk what to add. thank you. – uv239 Dec 18 '18 at 15:20
  • 2
    Possible duplicate of [Remove 'a' from legend when using aesthetics and geom\_text](https://stackoverflow.com/questions/18337653/remove-a-from-legend-when-using-aesthetics-and-geom-text) – divibisan Dec 18 '18 at 15:30
  • 1
    I think the above duplicate might have a more clear answer. – divibisan Dec 18 '18 at 15:30
  • @divibisan geom_text is not the same as geom_label, as there i would need the solution posted by NelsonGon. But both solutions only remove the "a" and i want it to display something else. SO instead of having the "a" there i want, lets say a "b" or a "42" or something along those lines. I thought if the "a" is there, there needs to be a way to change it but all i found where solutions to remove it. – uv239 Dec 18 '18 at 15:37
  • I see, my mistake. I think you might have to do something hacky with your legend, since I believe the letter 'a' is actually a shape from the ggplot shapes list, not text, so you would not be able to change it into something more than a single character. – divibisan Dec 18 '18 at 15:56
  • It might help if you add a reproducible example of your data using `dput`. Then, at least, we could play around with your actual problem. – divibisan Dec 18 '18 at 15:57

1 Answers1

1

"Guides for geom_text() now accept custom labels with guide_legend(override.aes = list(label = "foo")) (@brianwdavis, #2458)."

As per release 3.0.0.

So here you'd use scale_fill_manual(values=colorPallet, labels = yVal, guide = guide_legend(override.aes = list(label = "foo"))).

Brian
  • 7,900
  • 1
  • 27
  • 41
  • 1
    Thank you, thats what i was looking for. As a note for everyone curious, you can replace "foo" also with a vector and it will use the values in it. – uv239 Dec 19 '18 at 06:50