breaks and labels in coord_polar

677 views
Skip to first unread message

baptiste auguie

unread,
May 14, 2012, 5:09:00 PM5/14/12
to ggplot2-dev
Dear list,

My planar package is seemingly broken on CRAN, and I believe the issue
comes from a bug in ggplot2. Consider the following code:

d <- data.frame(x=runif(50, 0, 360), y=runif(50, 1, 10))

ggplot(d, aes(x, y))+
geom_path() +
scale_x_continuous(breaks=c(0, 90, 180), labels=c("a", "b", "c"))

so far so good. But now, I wrap the plot in polar coordinates,

last_plot() + coord_polar()

and I get an error
"Error in scale_labels.continuous(scale, major) :
Breaks and labels are different lengths"

Am I missing something, or is it a new bug? Any workaround that would
also be compatible with the next version?

Thanks,

baptiste

sessionInfo()
R version 2.15.0 Patched (2012-03-31 r58891)
Platform: x86_64-apple-darwin9.8.0/x86_64 (64-bit)

locale:
[1] C

attached base packages:
[1] grid stats graphics grDevices utils datasets methods
[8] base

other attached packages:
[1] gridExtra_0.9 ggplot2_0.9.1 planar_1.2
[4] cubature_1.1-1 statmod_1.4.14 RcppArmadillo_0.3.0.3
[7] Rcpp_0.9.10 reshape2_1.2.1

loaded via a namespace (and not attached):
[1] MASS_7.3-18 RColorBrewer_1.0-5 colorspace_1.1-1 dichromat_1.2-4
[5] digest_0.5.2 labeling_0.1 memoise_0.1 munsell_0.3
[9] plyr_1.7.1 proto_0.3-9.2 scales_0.2.1 stringr_0.6

Winston Chang

unread,
May 14, 2012, 5:25:31 PM5/14/12
to baptiste auguie, ggplot2-dev
I think this is happening because 0 (usually) isn't in the range of the x data, so it's getting dropped from the breaks.

It works if you specify the x limits:
d <- data.frame(x=runif(50, 0, 360), y=runif(50, 1, 10))

ggplot(d, aes(x, y)) + geom_path() +
    scale_x_continuous(limits=c(0, 360), breaks=c(0, 90, 180)) +
    coord_polar()

It may be related to:

-Winston

Winston Chang

unread,
May 14, 2012, 5:35:28 PM5/14/12
to baptiste auguie, ggplot2-dev
One more thing -

When you run the code *without* coord_polar, it works sometimes but not always:

d <- data.frame(x=runif(50, 0, 360), y=runif(50, 1, 10))
ggplot(d, aes(x, y)) + geom_path() +
 scale_x_continuous(breaks=c(0, 90, 180), labels=c("a", "b", "c"))

I think this is because with coord_cartesian (the default), the x limits are expanded (with the expand parameter); sometimes the random data will be close enough to zero that the expansion includes 0; sometimes not.

With coord_polar, there is by default no expansion of the x/theta limits, because it would make things look strange. This is why some data works with coord_cartesian, but not with coord_polar.

This expansion behavior is new, but it unfortunately isn't well documented at this point.

-Winston

baptiste auguie

unread,
May 14, 2012, 5:45:17 PM5/14/12
to Winston Chang, ggplot2-dev
I see, that makes sense. Thanks.

Specifying labels is not so easy with polar coordinates. I cannot
reproduce the plot that I had before (see attached); ggplot2 insists
on using xxx/yyy for the last break and the order is messed up when I
use start = pi/2. For now I'll just keep to the seq(0, 360) labels in
their default order.

Cheers,

baptiste
fig2.pdf
Reply all
Reply to author
Forward
0 new messages