Search code examples
rggplot2donut-chart

How to make from geom_tile a donut graph


I want to make a simple circular donut annotation plot from categorical data. Therefore, I first make a simple geom_tile plot, and add later the coord_polar() function. However, it seems that I cannot control the ylim to make the donut figure, because it will produce the following error:

Error: Discrete value supplied to continuous scale.

Now I am stuck with this figure:

See current plot

To reproduce the plot, and to help me out:

examp = data.frame(rep(c("A","B"),each=12),
               rep(c("C","D"),times=12),
               rep(c("E","F","G"),each=8),
               rep(c("E","H","G"),each=8),
               1:24)
colnames(examp) = c("col1", "col2", "col3","col4","person")
examp$person = paste0("per.",examp$person)
examp2 <- melt(examp, id.var = 'person')

ggplot(examp2, aes(x=person,y=variable)) +
  geom_tile(aes(fill = value), colour = "white")+ 
  scale_y_discrete(breaks=y_breaks, labels=y_labels) +
  coord_polar()+
  theme(panel.background=element_blank(),
        axis.title=element_blank(),
        panel.grid=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks=element_blank(),
        axis.text.y=element_text(size=5))

Solution

  • Does specifying the expansion amount within scale_y_discrete() work for you?

    ggplot(examp2, aes(x=person, y=variable)) +
      geom_tile(aes(fill = value), colour = "white")+ 
      scale_y_discrete(expand = expansion(mult = c(0.5, 0), add = 0)) + # change amount if needed
      coord_polar()+
      theme(panel.background=element_blank(),
            axis.title=element_blank(),
            panel.grid=element_blank(),
            axis.text.x=element_blank(),
            axis.ticks=element_blank(),
            axis.text.y=element_text(size=5))
    

    (I skipped the breaks / labels, since you didn't actually define them within the question.)

    enter image description here