Search code examples
rggplot2geom-text

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


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

Solution

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