سؤال

I am trying to generate pie charts for this data.frame where each row represents a unique identifier for a site, the errors count for the fields and the, number of fields. How best would I structure the the ggplot command to produce a pie chart with errors being a fraction of the the fields for each site? Currently, my code looks like the following:

ggplot(error_indicator,aes(x = Fields,y=Errors)) 
+ facet_grid(~Hospital) 
+ geom_bar(width = 1,stat="identity", position="fill") 
+ coord_polar(theta="y")

The dataframe looks like the following:

enter image description here

But the result from my ggplot code looks like the following:

enter image description here

How do I get the pie charts to show errors as ratios of the fields for each hospital and each hospital using its own row?

هل كانت مفيدة؟

المحلول

First, pie charts is not the best way to show your data!

But anyway here is one solution. Most important part here is scale_y_continuous() to set limits from 0 to 1 and then convert them to percents using percent_format() from library scales.

library(ggplot2)
library(scales)
ggplot(df,aes(x=1,y=Errors/Fields))+geom_bar(stat="identity")+
  facet_grid(~Hospital)+
  coord_polar(theta="y")+
  scale_y_continuous(labels = percent_format(),limits=c(0,1),
              breaks=c(0.25,0.5,0.75,1))+
  theme(axis.title.y=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank())
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top