简体   繁体   中英

Pie chart legend with multiple lines of text in R

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)))

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)))

Code for the Plot

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM