0

I am attempting to use the following code to split my long line of text into multiple lines in the legend of a pie chart. The current code creates a legend with a lot of white space and the text runs over the box. The box is also over the pie chart. What am I doing wrong?

v4_class_combined_slices <- c(27, 121, 27)
v4_class_combined_labels <- c("Bacteroidia", "Clostridia", "Actinobacteria/Coriobacteriia/\n4C0d.2/Bacilli/Erysipelotrichi/\nBetaproteobacteria/Gammaproteobacteria/\nMollicutes/RF3")
piepercent <- round(100*v4_class_combined_slices/sum(v4_class_combined_slices), 1)
piepercent2 <- paste(piepercent, sep = " ", "%")
pie(v4_class_combined_slices, labels = piepercent2, col = rainbow(3))
legend("topright", v4_class_combined_labels, cex = 0.9, text.width = 1.5, yjust = 0, fill = rainbow(length(v4_class_combined_labels)))

2 Answers 2

0

You just need to adjust the cex and text.width.

legend("topright", v4_class_combined_labels, cex = 0.4, text.width = 1.0, yjust = 0.0, fill = rainbow(length(v4_class_combined_labels)))

2
  • Hmm that didn't work for me. I still have the same problem. See the image: imgur.com/a/kw9oFOv Commented Jan 19, 2023 at 22:01
  • Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.
    – Community Bot
    Commented Jan 20, 2023 at 6:59
-1

Code for the Plot

It will work.I have used ggplot2, ggrepel library. Make sure you install [1]: https://i.sstatic.net/Q4ZcB.png

1
  • Greetings! It's not advisable to share a screenshot of your code. It is better to simply add it here. That way people can just copy and paste your code into their respective scripts. Commented Jan 27, 2023 at 1:45

Not the answer you're looking for? Browse other questions tagged or ask your own question.