Search code examples
rggplot2geom-bar

r ggplot - placing annotations in concentric circles chart using coord_polar


I made a chart of concentric circles using ggplot + geom_bar + polar_coord, and I am struggling with placing the annotations in the correct spot. Take the following code, modified from a previous question I asked and its satisfactory answer.

df <- data.frame(A=letters[1:12],
                 B=c(rep("Dim_1",4),rep("Dim_2",4),rep("Dim_3",4)),
                 C=c(rep("Ind_1",2),rep("Ind_2",2),rep("Ind_3",2),rep("Ind_2",2),rep("Ind_5",2),rep("Ind_6",2)))

ggplot(df, aes(factor(1), fill = C)) +
  geom_bar(width = 1, colour = NA) +                       
  stat_count(aes(yintercept = cumsum(rev(..count..))),     
             geom = "hline") +                             
  coord_polar()+
  annotate("text",label = "A", x = 1, y = 2.5,size=2)+
  annotate("text",label = "B", x = 1, y = 3.5,size=2)

Here is what I get:

This is what I get

The problem is placement. I would like to place the annotate text around the circle. But since I created the chart from a geom_bar of 1 observation, I can move the texts only along the vertical axis.

How do I freely place my annotations in the chart? Many thanks in advance.


Solution

  • Don't clearly get the question but...annotate freely with geom_text instead as follows:

        ggplot(df, aes(factor(1), fill = C)) +
      geom_bar(width = 1, colour = NA) +                       
      stat_count(aes(yintercept = cumsum(rev(..count..))),     
                 geom = "hline") +                             
      coord_polar()+
      geom_text(label="A",x=1.2,y=2.5)+
      geom_text(label="B",x=1.5,y=3.5)
    

    This gives: enter image description here

    You can edit as you wish.