mark gill
mark gill

Reputation: 41

How to make a pie chart?

I want to plot (basically pie chart) this mysql data using R.

rs <- dbSendQuery(con, "select count(uid), school from info group by school order by count(uid) desc limit 5")

d2 <- fetch(rs, n = -1)
d2
>d2 

      count(uid)         school  
           109         A  
           88          B  
           77          C  
           44          D  
           32          E  

How do I plot count(uid) Vs school in R.

Upvotes: 1

Views: 1642

Answers (3)

Max Candocia
Max Candocia

Reputation: 4386

I came up with a pretty useful function for making pie charts with ggplot2. Essentially it will create pie charts with percentage labels for any sector with over 5% of the total area. survey is the data set you load from, varname is a string that has the variable name, threshold can change the level at which text is displayed, and palette is the set of colors you want to use if you don't want the default rainbow one (it's hard to read if you have too many factors.) Here's an example of one I made with it:

Pie chart

Here is the code so you can use it/modify it if need be. The real trick in getting it to be readable was adjusting the widths of both the geom_text() and the geom_bar(), which allows the percentages to have a greater radius. Unfortunately it doesn't work too well for smaller resolutions, but if you have at least 640x640 you shouldn't have any problems.

I used the eval(parse(...)) method since I find it quick to work with once you get used to it, but if anyone has any other suggestions, let me know.

piechart<-function(survey,varname,threshold=0.05,palette=-1){
    require(grid)
    require(ggplot2)
    n=dim(survey)[1]
    #print(names(survey))
    eval(parse(text=paste0(c("tab =   
    as.data.frame(table(survey$",varname,"))"),collapse="")))
    colnames(tab)[1]=varname
    n2=dim(tab)[1]
    scb=""
    if (palette !=-1) scb="scale_fill_brewer(palette=palette)+"
    .e<-environment()

    p=eval(parse(text=paste0(c('ggplot(tab,aes(x=3,fill=',varname,',y=Freq),environment=.e)+
geom_bar(width=11,stat="identity")+coord_polar(theta="y")+',
scb,'xlab("")+ylab("")+labs(title="Responses by ',varname,'") +scale_y_continuous()+
geom_text(aes(label=ifelse(Freq/n>threshold,sprintf("%.1f%%",Freq/n*100),""),
size=12,y=Freq/2+c(0,cumsum(Freq)[-n2]),x=6.5))+theme(axis.ticks.y=element_blank(),
axis.ticks.x=element_blank(),axis.text.x=element_blank(),axis.text.y=element_blank(),
panel.grid=element_blank(),plot.margin= unit(c(-1,0,-1,0), "cm"))+
guides(size="none")'),collapse="")))

    print(p)

}

Also, as Paul said, pie charts should be used sparingly, but they can be useful at times.

Upvotes: 1

Paul Hiemstra
Paul Hiemstra

Reputation: 60924

You can also use ggplot2, look at the coord_polar geometry:

pie <- ggplot(mtcars, aes(x = factor(1), fill = factor(cyl))) + 
         geom_bar(width = 1) 
pie + coord_polar(theta = "y") 

Although the literature on making nice graphs does not consider a pie chart to be a good option.

Upvotes: 0

James
James

Reputation: 66834

Your first column name may cause some problems, its better to rename it to something like count.uid. I'm assuming d2 is a data.frame.

pie(d2$count.uid,labels=d2$school)

Upvotes: 2

Related Questions