-1

I have this pandas dataframe:

df =

GROUP   MARK
ABC     1
ABC     0
ABC     1
DEF     1
DEF     1
DEF     1
DEF     1
XXX     0

I need to create a piechart (using Python or R). The size of each pie should correspond to the proportional count (i.e. the percent) of rows with particular GROUP. Moreover, each pie should be divided into 2 sub-parts corresponding to the percent of rows with MARK==1 and MARK==0 within given GROUP.

I was googling for this type of piecharts and found this one. But this example seems to be overcomplicated for my case. Another good example is done in JavaScript, which doesn't serve for me because of the language.

Can somebody tell me what's the name of this type of piecharts and where can I find some examples of code in Python or R.

Community
  • 1
  • 1
Dinosaurius
  • 8,306
  • 19
  • 64
  • 113
  • You can use the `highcharter` which is an R wrapper for the highcharts JS library (used by your 2nd "good example"). I don't think this is a great way to present the data though. – arvi1000 Dec 24 '16 at 17:17
  • @arvi1000: Can you post some example of pie chart? I can only find bar plots and timeseries. – Dinosaurius Dec 24 '16 at 17:25
  • 1
    While you could use a pie, I don't think that this is the best way to represent hierarchical data. There are a lot of alternatives such as a treemap or a waffle plot. If you want to stick with radial options, I'd look at a coxcomb plot or a sunburst plot. All of these options have packages to facilitate plotting. – Jake Kaupp Dec 24 '16 at 19:03

1 Answers1

1

Here is a solution in R that uses base R only. Not sure how you want to arrange your pies, but I used par(mfrow=...).

df <- read.table(text="  GROUP   MARK
ABC     1
ABC     0
ABC     1
DEF     1
DEF     1
DEF     1
DEF     1
XXX     0", header=TRUE)

plot_pie <- function(x, multiplier=1, label){
   pie(table(x), radius=multiplier * length(x), main=label)
}

par(mfrow=c(1,3), mar=c(0,0,2,0))
invisible(lapply(split(df, df$GROUP), function(x){
  plot_pie(x$MARK, label=unique(x$GROUP),
       multiplier=0.2)
}))

This is the result:

threepies

Remko Duursma
  • 2,741
  • 17
  • 24