Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Manually adding suffixes to the legend ticks in ggplot

How can I manually add an arbitrary suffix to the legend ticks in ggplot?

For example, can I add something to the below code to generate the below plot with those unique legend ticks?

ggplot(iris, aes(Sepal.length, Sepal.Width, color = Species)) + geom_point()

enter image description here

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Note that I manually added "(s)" to the end of the legend ticks using Photoshop.

>Solution :

We can use the labels argument in scale_color_discrete. This can take an rlang style lambda function that pastes an "(S)" at the end.

ggplot(iris, aes(Sepal.Length, Sepal.Width, color = Species)) + 
  geom_point() +
  scale_color_discrete(labels = ~paste(.x, "(S)"))

enter image description here

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading