question on "coord_polar()"

100 views
Skip to first unread message

Dongjun Chung

unread,
Oct 28, 2010, 3:26:03 PM10/28/10
to ggplot2
Hi all,

I recently started to use "ggplot2" and really enjoy using it.

I recently observed some weird cases while using "ggplot2".
When I try to draw pie chart using "coord_polar()",
the resulting pie chart has dent in some cases.

Please see the linked picture below.

http://picasaweb.google.com/dongjun.chung/DropBox#5533176768915174338

However, if I round the values, then such dent goes away
(it took some time to figure this out after lots of trials & errors).
This behavior was somewhat unexpected to me.
Does any one have similar experiences?
Is rounding values the only way to fix this problem?
And can someone explain to me why this happens?

I used the codes below to draw this pie chart.

----------------------------------------

library(ggplot2)
piedata <- data.frame(
c( 0.1159132, 21.7254512, 0.1987084, 55.5224375, 0.0496771,
22.3878126,
4.3984476, 1.9081501, 14.5860285, 12.3868047, 41.6235446,
25.0970246 ),
rep( c('A','B','C','D','E','F'),2 ), rep( c('A','B'), each=6 ) )
colnames(piedata) <- c("Ratio","Category","Set")

p <- ggplot( data=piedata, aes( x=factor(1), y=Ratio,
fill=Category ) )
p <- p + geom_bar( width=1 )
p <- p + facet_grid( facets=. ~ Set )
p <- p + coord_polar( theta="y" )
p

----------------------------------------

Thanks!

takahashi kohske

unread,
Oct 28, 2010, 6:14:13 PM10/28/10
to Dongjun Chung, ggplot2
hi

probably this is because coord-polar calculates the distance between
vertices using Euclidean data and polar coordinate.
an easy hack is like this:

CoordPolar$distance <- function(., x, y, details) {
max_dist <- dist_polar(details$r.range, details$theta.range)
if (.$theta == "x") {
d <- .$transform(data.frame(theta=x, r=y), details)
dist_polar(theta = d$theta, r = d$r) / max_dist
} else {
d <- .$transform(data.frame(theta=y, r=x), details)
dist_polar(r = d$r, theta = d$theta) / max_dist
}
}

> --
> You received this message because you are subscribed to the ggplot2 mailing list.
> Please provide a reproducible example: http://gist.github.com/270442
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>

Hadley Wickham

unread,
Nov 8, 2010, 9:29:42 PM11/8/10
to takahashi kohske, Dongjun Chung, ggplot2
Could you explain more what the problem is? I guess I'm missing what
I'm doing wrong here.
Hadley

--
Assistant Professor / Dobelman Family Junior Chair
Department of Statistics / Rice University
http://had.co.nz/

Kohske Takahashi

unread,
Nov 8, 2010, 10:52:59 PM11/8/10
to Hadley Wickham, Dongjun Chung, ggplot2
hi,

sorry for that my modification above is not general, and probably i
misunderstood something at that time.

please try this example:

ggplot(data.frame(xmin=0, ymin=0, xmax=2*pi, ymax=1), aes(xmin=xmin,
xmax=xmax,ymin=ymin,ymax=ymax))+geom_rect() + coord_polar()
ggplot(data.frame(xmin=0, ymin=0, xmax=314, ymax=1), aes(xmin=xmin,
xmax=xmax,ymin=ymin,ymax=ymax))+geom_rect() + coord_polar()

CoordPolar$distance calculate the distances between vertices, using
dist_polar that implementing the formula of polar distance.

In the first exmaple, max_dist = 1 because theta.range = 2 x pi = 0 on
polar coordinate, and the distance between two points on the
circumference is also 0 because of same reason.
using 2 x pi is somehow intentional.

Probably the worst thing occur in the second example. xmax=314, which
is near to 2 x n x pi, resulting in that distance is very small.
so the number of division point is also small. In this case, it is
hard for users to be aware what is happing.

so i think CoordPolar$distance needs to calculate the polar distance
as Euclidian distance... it is difficult to explain, though.
anyway, if the theta is near to 2 * n * pi, then the calculated
distance is very small.

I have committed some modification to github.
please find https://github.com/kohske/ggplot2/commit/057f8a2c746eca9998c17859a3b9442ff89cd8cd

this calculation is just approximation, but perhaps this is enough for
the purpose of drawing.

--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Kohske Takahashi

unread,
Nov 8, 2010, 11:07:26 PM11/8/10
to Hadley Wickham, Dongjun Chung, ggplot2
hi, this is follow up of my previous reply.
Now I get what i thought when i was writing the reply.

by this example:
ggplot(data.frame(xmin=0, ymin=0, xmax=1, ymax=1), aes(xmin=xmin,
xmax=xmax,ymin=ymin,ymax=ymax))+geom_rect() + coord_polar()

in CoordPolar$distance, it calls
dist_polar(theta = x, r = y) / max_dist
where x = c(0, 1, 1, 0, 0) and y = (1, 1, 0, 0, 1)
that is, theta passed to dist_polar is 1, not 2 * pi, but should be 2 * pi.

this is what i said by


> probably this is because coord-polar calculates the distance between
> vertices using Euclidean data and polar coordinate.

so perhaps i found 2 different problems: regarding input as theta
(described here), and the distance is small when the theta is near to
2 * pi.

ggplot(data.frame(x=c(0,2*pi+0.1,0,(2*pi+0.1)*2),
y=c(0.5,0.5,1,1)),aes(x,y,group=y))+geom_line()+coord_polar()+ylim(0,1)

this is combination of these two points.
The theta of inner circle should be pi, but the distance is calculated
using input |x| = theta=2*pi+0.1, which is near to 2*pi, resulting in
small distance.


--
Kohske Takahashi <takahash...@gmail.com>

Research Center for Advanced Science and Technology,
The University of  Tokyo, Japan.
http://www.fennel.rcast.u-tokyo.ac.jp/profilee_ktakahashi.html

Reply all
Reply to author
Forward
0 new messages