Hi All,
I'm trying to find out a way to plot multi-panel pie charts. It may not be
the best way to present data, but I would still need one.
1. Is anyone aware of some in-built script/function which can do this for
me. I'm aware of one given in Deepayan's book, but anything apart from
this?
2. I tried using Deepayan's script on following data set but it doesn't
seem
to work as expected - labels are getting repeated/overlapping. I'm really
not sure what could be the problem, can anyone help please. I hope data is
in the right format, as expected.
Data read into object "foo":
variable month value
ProdA Jan 25
ProdA Feb 30
ProdA Mar 25
ProdA Apr 10
ProdB Jan 25
ProdB Feb 30
ProdB Mar 50
ProdB Apr 40
ProdC Jan 40
ProdC Feb 30
ProdC Mar 20
ProdC Apr 40
ProdD Jan 10
ProdD Feb 10
ProdD Mar 5
ProdD Apr 10
R Code: as it is from the book,
library(lattice)
library(grid)
library(gridBase)
panel.piechart <-
function(x, y, labels = as.character(y),
edges = 200, radius = 0.8, clockwise = FALSE,
init.angle = if(clockwise) 90 else 0,
density = NULL, angle = 45,
col = superpose.polygon$col,
border = superpose.polygon$border,
lty = superpose.polygon$lty, ...)
{
stopifnot(require("gridBase"))
superpose.polygon <- trellis.par.get("superpose.polygon")
opar <- par(no.readonly = TRUE)
on.exit(par(opar))
if (panel.number() > 1) par(new = TRUE)
par(fig = gridFIG(), omi = c(0, 0, 0, 0), mai = c(0, 0, 0, 0))
pie(as.numeric(x), labels = labels, edges = edges, radius = radius,
clockwise = clockwise, init.angle = init.angle, angle = angle,
density = density, col = col, border = border, lty = lty)
}
piechart <- function(x, data = NULL, panel = "panel.piechart", ...)
{
ocall <- sys.call(sys.parent())
ocall[[1]] <- quote(piechart)
ccall <- match.call()
ccall$data <- data
ccall$panel <- panel
ccall$default.scales <- list(draw = FALSE)
ccall[[1]] <- quote(lattice::barchart)
ans <- eval.parent(ccall)
ans$call <- ocall
ans
}
## Figure 14.5
par(new = TRUE)
piechart(foo)
Thanks in advance,
Gurmeet
[[alternative HTML version deleted]]
Gurmeet wrote:> > Hi All, > > I'm trying to find out a way to plot multi-panel pie charts. It may not be > the best way to present data, but I would still need one. >Would paneled bar charts not suffice? I don't mean to be harsh, but the only situation I can think of where I would consider a pie chart would be if I wanted to take advantage of the fact that people are worse at judging differences in area than they are at judging differences in length in order to hide some trend in my data. Anyway, the following code uses ggplot2 to produce a paneled bar plot from your data: require( ggplot2 ) productData <- structure(list(variable = structure(c(1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L), .Label = c("ProdA", "ProdB", "ProdC", "ProdD"), class = "factor"), month = structure(c(3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L, 3L, 2L, 4L, 1L), .Label c("Apr", "Feb", "Jan", "Mar"), class = "factor"), value = c(25, 30, 25, 10, 25, 30, 50, 40, 40, 30, 20, 40, 10, 10, 5, 10)), .Names c("variable", "month", "value"), class = "data.frame", row.names = c(NA, -16L)) productPlot <- qplot( variable, value, data = productData, geom = 'bar', xlab = 'Product', ylab = 'Percentage' ) + facet_wrap( ~ month ) + theme_bw() print( productPlot ) I know it's not what you want, but I personally need a strong argument for generating pie charts before I would perpetuate their use. Gurmeet wrote:> > > 1. Is anyone aware of some in-built script/function which can do this for > me. I'm aware of one given in Deepayan's book, but anything apart from > this? > > > 2. I tried using Deepayan's script on following data set but it doesn't > seem > to work as expected - labels are getting repeated/overlapping. I'm really > not sure what could be the problem, can anyone help please. I hope data is > in the right format, as expected. > > Data read into object "foo": > > variable month value > ProdA Jan 25 > ProdA Feb 30 > ProdA Mar 25 > ProdA Apr 10 > ProdB Jan 25 > ProdB Feb 30 > ProdB Mar 50 > ProdB Apr 40 > ProdC Jan 40 > ProdC Feb 30 > ProdC Mar 20 > ProdC Apr 40 > ProdD Jan 10 > ProdD Feb 10 > ProdD Mar 5 > ProdD Apr 10 > > {SNIP} > > Thanks in advance, > Gurmeet >Providing data as a printed table, like you did, is not the most effective way to transmit example data on this list. There are two major disadvantages: * Tabulated data often gets mangled in email * Tabulated data can not be copied and pasted directly into R to regenerate the example data.frame- it takes me ~4 minutes of mucking around with Excel to regenerate a .csv file that R can ingest. This added time will limit the number of people who will attempt to investigate your problem. The best way to transmit the contents of a data frame is to paste the output of the dput() function. This function dumps the data frame to an R command that can be simply copied and pasted into a R session to regenerate the data.frame. The results of dput is the structure() command I used in my example above. Hope this helps in some way! -Charlie ----- Charlie Sharpsteen Undergraduate-- Environmental Resources Engineering Humboldt State University -- View this message in context: http://n4.nabble.com/Multi-panel-Pie-Charts-tp1687026p1689524.html Sent from the R help mailing list archive at Nabble.com.
On 03/25/2010 06:00 AM, Gurmeet wrote:> Hi All, > > I'm trying to find out a way to plot multi-panel pie charts. It may not be > the best way to present data, but I would still need one. >Hi Gurmeet, Your message prompted me to finish a little function that I had almost forgotten. panes<-function(nrow=2,ncol=2,mar=c(0,0,1.6,0),oma=c(2,1,1,1)) { oldpar<-par("mar","mfrow","oma") par(mfrow=c(nrow,ncol),mar=mar,oma=oma) return(oldpar) } oldpar<-panes() pie(foo$value[foo$month=="Jan"],labels=foo$variable[foo$month=="Jan"], radius=0.7) tab.title("January sales",tab.col="#cccc66") box() pie(foo$value[foo$month=="Feb"],labels=foo$variable[foo$month=="Feb"], radius=0.7) tab.title("February sales",tab.col="#cccc66") box() pie(foo$value[foo$month=="Mar"],labels=foo$variable[foo$month=="Mar"], radius=0.7) tab.title("March sales",tab.col="#cccc66") box() pie(foo$value[foo$month=="Apr"],labels=foo$variable[foo$month=="Apr"], radius=0.7) tab.title("April sales",tab.col="#cccc66") box() par(xpd=TRUE) mtext("First quarter sales",at=-1.2,line=0.8,side=1,cex=1.5) par(xpd=FALSE) par(oldpar) Is this what you wanted? Jim