how to adjust text location in a pie chart / with polar coordinates
Asked Answered
N

2

1

I would like to adjust the text on the piechart.

I want to put the values in the middle of each side of the piechart.

I've tried using hjust,vjust, and tried to use other function etc. But it hasn't worked.

Here is my data and state. Thank you for you help.

k<-data.frame(group=c('alpha','bets'),value=c(0.512,0.488))

ggplot(k,aes(x="",y=value,fill=group))+

  geom_bar(width=1,stat="identity")+

  coord_polar("y")+

  geom_text_repel(aes(label=round(value,3)))
Neurocoele answered 24/3, 2021 at 8:57 Comment(0)
V
1

Polar coordinate plots are often easier to debug in cartesian coordinates. You probably want position = position_stack(vjust = 0.5) for the text geom.

library(ggplot2)
k <- data.frame(group = c("alpha", "bets"), value = c(0.512, 0.488))

ggplot(k, aes(x = "", y = value, fill = group)) +
  geom_col(width = 1) +
  coord_polar("y") +
  geom_text(aes(label = round(value, 3)), position = position_stack(vjust = 0.5))

Created on 2021-03-24 by the reprex package (v1.0.0)

Vachell answered 24/3, 2021 at 9:19 Comment(1)
hahaha I like your strategy of making other answers worse so people will accept your solution to the problem xD just kidding ofcVachell
D
0

I'd create a separate data frame for the text labels and manually set the coordinates.

library(ggplot2)
library(ggrepel)
k <- data.frame(group = c("alpha", "bets"), value = c(0.512, 0.488))

data_text <- data.frame(x = 1, y = c(.75, .25), value = k$value)

ggplot(k, aes(x = "", y = value, )) +
  geom_bar(aes(fill = group), width = 1, stat = "identity") +
  coord_polar("y") +
  geom_text(data = data_text, aes(x = x, y = y, label = round(value, 3)))

Created on 2021-03-24 by the reprex package (v1.0.0)

Dowel answered 24/3, 2021 at 9:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.