# [R] rotate pie chart

Katrin Schweitzer Katrin.Schweitzer at ims.uni-stuttgart.de
Wed May 25 08:25:07 CEST 2005

``` > I think you have to get your hands dirty on this one, but it's not too
> hard.  Here's a function pie90() which is a tiny modification of pie().
>   Does that do the trick?
>

Yes, it works perfectly fine, at least for what I wanted... :)
Thanks a lot, to Lars for asking, and to Paul for getting your hands dirty!

Kati

PS: I know one shouldn't use pie charts at all... :) but if I do so, is
there a  reason
why they work counter-clockwise in R? Is that convention?
Sorry if its a silly question, my intuition (which might very likely be
horrible)
just expected them to start at 12 o'clock and fill the pie clockwisely.

> pie90 <- function (x, labels = names(x), edges = 200, radius = 0.8,
> density = NULL,
>      angle = 45, col = NULL, border = NULL, lty = NULL, main = NULL,
>      ...)
> {
>      if (!is.numeric(x) || any(is.na(x) | x <= 0))
>          stop("'x' values must be positive.")
>      if (is.null(labels))
>          labels <- as.character(1:length(x))
>      x <- c(0, cumsum(x)/sum(x))
>      dx <- diff(x)
>      plot.new()
>      pin <- par("pin")
>      xlim <- ylim <- c(-1, 1)
>      if (pin[1] > pin[2])
>          xlim <- (pin[1]/pin[2]) * xlim
>      else ylim <- (pin[2]/pin[1]) * ylim
>      plot.window(xlim, ylim, "", asp = 1)
>      nx <- length(dx)
>      if (is.null(col))
>          col <- if (is.null(density))
>              c("white", "lightblue", "mistyrose", "lightcyan",
>                  "lavender", "cornsilk")
>          else par("fg")
>      col <- rep(col, length.out = nx)
>      border <- rep(border, length.out = nx)
>      lty <- rep(lty, length.out = nx)
>      angle <- rep(angle, length.out = nx)
>      density <- rep(density, length.out = nx)
>      for (i in 1:nx) {
>          n <- max(2, floor(edges * dx[i]))
> # modified line below
>          t2p <- 2 * pi * seq(x[i], x[i + 1], length = n) + pi/2
>          xc <- c(cos(t2p), 0) * radius
>          yc <- c(sin(t2p), 0) * radius
>          polygon(xc, yc, density = density[i], angle = angle[i],
>              border = border[i], col = col[i], lty = lty[i])
> # modified line below
>          t2p <- 2 * pi * mean(x[i + 0:1]) + pi/2
>          xc <- cos(t2p) * radius
>          yc <- sin(t2p) * radius
>          if (!is.na(lab <- labels[i]) && lab != "") {
>              lines(c(1, 1.05) * xc, c(1, 1.05) * yc)
>              text(1.1 * xc, 1.1 * yc, lab, xpd = TRUE, adj = ifelse(xc <
>                  0, 1, 0), ...)
>          }
>      }
>      title(main = main, ...)
>      invisible(NULL)
> }
>
>
> Paul
>
>
> Sean Davis wrote:
> > You might want to look at grid graphics and gridBase.  I don't know in
> > detail how to go about what you are asking, but grid allows you to
> > rotate plots arbitrarily.  Here are a couple of links that I think are
> > useful.
> >
> > http://www.stat.auckland.ac.nz/~paul/grid/grid.html
> > http://www.stat.auckland.ac.nz/~paul/grid/doc/rotated.pdf
> >
> > Sean
> >
> > On May 24, 2005, at 10:09 AM, Lars wrote:
> >
> >> hey,
> >>
> >> about two weeks ago i posted a question concerning the display of two
> >> piecharts on one plot. after now being able to do so, i need to rotate
> >> them. the first piece of my pie is suppose to start at 0° but at 90°.
> >> i tried several things, all failing in the end. anyone out there who
> >> has an idea?
> >>
> >> Lars
> >>
> >> ______________________________________________
> >> R-help at stat.math.ethz.ch mailing list
> >> https://stat.ethz.ch/mailman/listinfo/r-help
> >> http://www.R-project.org/posting-guide.html
> >
> >
> > ______________________________________________
> > R-help at stat.math.ethz.ch mailing list
> > https://stat.ethz.ch/mailman/listinfo/r-help
> > http://www.R-project.org/posting-guide.html
>
>
> --
> Dr Paul Murrell
> Department of Statistics
> The University of Auckland
> Private Bag 92019
> Auckland
> New Zealand
> 64 9 3737599 x85392
> paul at stat.auckland.ac.nz
> http://www.stat.auckland.ac.nz/~paul/
>
> ______________________________________________
> R-help at stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help