rggplot2visualizationpie-chart

How to make a half-circle, semi circle,hemisphere diagram start on the the x-axis and attach correct labels to the chart


I have a semi circle plot that is displaying this data (named data_extract) :

Option      Count       Perc         Fract
Excellent    8           73            .73
Good         1            9            .09        
Fair         1            9            .09    
Poor         1            9            .09    

And I am trying to graph it into a semi-circle pie chart, using this code with the ggplot library:

  ymax <- cumsum(data_extract$fract)
  ymin <- c(0, head(ymax, n= -1))
  
  ggplot(data_extract, aes(fill = Option, ymax = ymax, ymin = ymin, xmax = 2, xmin = 1)) + 
    geom_rect() + 
    coord_polar(theta = "y",start=-pi/2) + xlim(c(0, 2)) + ylim(c(0,2)) +
    theme_classic() +
   theme( legend.position = "bottom", 
           axis.title = element_blank(), axis.text = element_blank(),
           axis.ticks = element_blank(), line = element_blank()) +
    labs(fill = "Key")

The problem that I am having is that I don't want the plot to float in the middle of the graph area like that and would like for it to start and finish on the x-axis.

I would also like the graph to display the percentages associated with each bar within the bar, and I haven't had much luck in doing either.

enter image description here


Solution

  • The coord_radial() function makes this a bit easier:

    library(tibble)
    library(ggplot2)
    
    data_extract <- tribble( ~Option, ~Count, ~Perc, ~fract,
      "Excellent", 8, 73, .73,
      "Good", 1, 9, .09,
      "Fair", 1, 9, .09,
      "Poor", 1, 9, .09
    )
    
    ymax <- cumsum(data_extract$fract)
    ymin <- c(0, head(ymax, n = -1))
    
    ggplot(
      data_extract,
      aes(fill = Option, ymax = ymax, ymin = ymin, xmax = 2, xmin = 1)
    ) +
      geom_rect() +
      coord_radial(
        theta = "y",
        start = -pi / 2,
        end = pi / 2,
        expand = FALSE,
        inner.radius = .5
      ) +
      theme_classic() +
      theme(
        legend.position = "bottom",
        axis.title = element_blank(),
        axis.text = element_blank(),
        axis.ticks = element_blank(),
        line = element_blank()
      ) +
      labs(fill = "Key")
    

    Created on 2025-10-24 with reprex v2.1.1