![]() ![]() As you calculate where to place the labels based on the ordering in your data frame, this works out wrong.Īs a general principle of readability, do all the fancy calculations of labels and positions they go before the actual code drawing the graphic. your problem comes from the order in which the wedges are drawn, which will default to alphabetical. Solved-r pie chart labels overlap ggplot2 score:4 Accepted answer Here. I agree with a waffle chart would be better. Rotate the percentage and label things so they dont have to be ridiculously small. Guides(fill = guide_legend(title = "Group"))ĭATA mydf <- structure(list(Group = structure(c(3L, 1L, 2L). Geom_label_repel(aes(label = prop), size=5, show.legend = F, nudge_x = 1) + Pie <- ggplot(mydf, aes(x = "", y = value, fill = fct_inorder(Group))) + Mutate(prop = percent(value / sum(value))) -> mydf When geom_label_repel() added labels to the pie, the order of label was identical to that of the pie. When I drew the ggplot figure, I specified the order of Group in the order in mydf (i.e., Negative, Positive, and Neutral) using fct_inorder(). I also calculated the percentage in advance. I sorted the data in descending order by value. I can now use this function for any other projects we work on.Here is an idea matching the order of groups in the pie chart and the order of labels. Here’s a video walkthrough with the code used in the video below that.Īnd there we have it: a donut chart with a big number in the middle. ![]() Create our data frameįirst, we create our data frame. I’ve written code and recorded videos to show each step. Let’s break apart this function piece by piece in order to see how it works. highlight_color sets the color of the donut chart and the big number.font_family to use any font for the big number (of course, you have to have the font installed in order for it to work).For this function, I assume it’s a percentage (you could adjust the function for non-percentages). value is the number (from 0 to 1) that we want to highlight.In order to use the function, we’d write code like this. # Add the big number in the center of the hole # Set theme_void() to remove grid lines and everything else from the plot Scale_fill_manual(values = c(highlight_color, "grey90")) + # Set a color scale with the highlighted section in whatever color # is chosen with the highlight_color argument and the rest in a light gray # Set the limits, which is important for adding the hole # A pie/donut chart is a bar chart with polar coordinates # Add polar coordinates and set the direction to -1 # so the filled in part starts at the top and goes clockwise # Create a nicely formatted big number to go in the donut holeīig_number_text_label <- percent(value, accuracy = 1) Plot the donut chart and add the big number to the middle of it.Use the percent() function to create a big number as a percentage. ![]()
0 Comments
Leave a Reply. |