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:
But the image below shows what I need:
How can I do it? Thanks.
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.
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.
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.
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.
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)
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:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With