Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing the position of the label in a donut chart

Tags:

r

ggplot2

ggrepel

I am trying to move the position of the label in a donut chart to be at the right side of the chart but I am not able to do it. I am using ggplot and ggrepel to make the graph.

library(ggplot2)
library(ggrepel)

expenditurePie = data.frame(
  value = c(98,2),
  area = c("A","B"),
  label = c("","This is a label"))

ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
  geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
  geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"), direction = "x") +
  xlim(0.5, 4.5) +
  annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color = "grey") +
  scale_fill_manual(values = c(A = "grey", B = "black")) +
  coord_polar(theta = "y", start = 1) +
  theme_void() +
  theme(legend.position = 'none')

The image below is the result of the code above: enter image description here

But the image below shows what I need: enter image description here

How can I do it? Thanks.

like image 346
Bruno Guarita Avatar asked May 24 '20 20:05

Bruno Guarita


People also ask

How do I change the position of a label in a pie chart?

Right-click the Chart > click Chart Options. Click Data Labels. Check the Show Values option. Select Inside Edge, Outside Edge, Outside Pie or Center Slice for Values Location option.

How do I change the position of data labels in Excel?

Click any data label once to select all of them, or double-click a specific data label you want to move. > Data Labels arrow, and select the placement option you want. Different options are available for different chart types.

How do you move data labels on a graph in Excel?

To make data labels easier to read, you can move them inside the data points or even outside of the chart. To move a data label, drag it to the location you want. Note: If the text inside the data labels is too hard to read, resize the data labels by clicking them, and then dragging them to the size you want.

How do you move data labels outside doughnut chart?

Thanks for your question! To move data labels outside of a doughnut chart, you'll need to use the "Data Labels" tab in the "Layout" section of the Visual Editor. From there, you can drag-and-drop your data labels to wherever you want them on your chart.


2 Answers

Maybe this is an option for you. Instead of relying on the magic of ggrepel my approach sets the label manually (still using ggrepel but with force set to 0) and while drawing the segment using geom_segement. Try this:

library(ggplot2)
library(ggrepel)

expenditurePie = data.frame(
  value = c(98,2),
  area = c("A","B"),
  label = c("","This is a label"))

ggplot(expenditurePie, aes(y = value, fill = area, label = label)) +
  geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
  geom_text_repel(size = 5, x = 7, point.padding = unit(1.8, "lines"), direction = "x", force = 0, seed = 42) +
  geom_segment(aes(x = 4.6, xend = 5.5, y = 1, yend = 1)) +
  xlim(0.5, 5.5) +
  annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 20, color = "grey") +
  scale_fill_manual(values = c(A = "grey", B = "black")) +
  coord_polar(theta = "y", start = 1) +
  theme_void() +
  theme(legend.position = 'none')

Created on 2020-05-25 by the reprex package (v0.3.0)

like image 58
stefan Avatar answered Nov 04 '22 16:11

stefan


Does this work for you? Utilizing nudge_x=5

library(ggplot2)
library(ggrepel)

expenditurePie = data.frame(
  value = c(98,2),
  area = c("A","B"),
  label = c("","This is a label"))

ggplot(expenditurePie, aes(y=value, fill = area, label = label)) +
  geom_bar(aes(x = 4), stat = "identity", show.legend = F) +
  geom_text_repel(size = 5, x= 4, point.padding = unit(1.8, "lines"), 
direction = "x", nudge_x = 5) +
  xlim(0.5, 4.5) +
  annotate(geom = "text", x=0.5, y=0, label = "24 444", size = 16, color = 
"grey") +
  scale_fill_manual(values = c(A = "grey", B = "black")) +
  coord_polar(theta = "y", start = 1) +
  theme_void() +
  theme(legend.position = 'none')

Output:

enter image description here

like image 1
iamericfletcher Avatar answered Nov 04 '22 15:11

iamericfletcher