Circular density plots

545 views
Skip to first unread message

fbielejec

unread,
Aug 12, 2011, 9:21:33 AM8/12/11
to ggplot2
Hello everybody.

I'm struggling with density estimation for my (circular ~ time) data.
Here is some simulated data, but it return the same error message as
my real data-set:

require(circular)
require(ggplot2)

set.seed(123)
X = rbeta(100, shape1 = 2, shape2 = 4)
X = 2 * pi * X
X = circular(X, type = "angle", units = "radians", rotation = "clock")
X = as.data.frame.circular(X)
p <- ggplot(X, aes(x = x))
p <- p + geom_histogram(aes(y = ..count..))
p <- p + coord_polar(theta = "x", start = 2*pi, direction = 1, expand
= FALSE)
print(p)

#Error in if (test == TRUE) { : argument is of length zero

Yet it works when using native circular methods:

set.seed(123)
X = rbeta(100, shape1 = 2, shape2 = 4)
X = 2 * pi * X
X = circular(X, type = "angle", units = "radians", rotation = "clock")
rose.diag(X, bins = 12, shrink = 0.33, xlim = c(-2, 2), ylim = c(-2,
2), axes = FALSE,
prop = 1.5)

#or kernel density estimation:
set.seed(123)
X = rbeta(100, shape1 = 2, shape2 = 4)
X = 2 * pi * X
X = circular(X, type = "angle", units = "radians", rotation = "clock")
plot.circular(X, stack = TRUE, shrink = 0.35, cex = 0, sep = 0.0, axes
= FALSE,
tol = .8, zero = c(0), bins = 12, xlim = c(-2, 2), ylim = c (-2,
2),
ticks = TRUE, tcl = .075)
vonmises = density.circular(X, kernel = "vonmises", n = 512, bw = 300)
lines(vonmises, col = "darkgrey", lwd = 3)

Dennis Murphy

unread,
Aug 12, 2011, 9:58:39 AM8/12/11
to fbielejec, ggplot2
Hi:

As far as the wind rose plot is concerned, that's not too hard:

df <- data.frame(x = 2 * pi * rbeta(100, shape1 = 2, shape2 = 4))
ggplot(df, aes(x)) +
geom_histogram(aes(y = ..count..), colour = 'orange', binwidth = 0.5) +


coord_polar(theta = "x", start = 2*pi, direction = 1, expand = FALSE)

I think the meta-data that the circular package adds as attributes to
the data frame didn't compute with ggplot(). Simplifying its structure
apparently works, though.

I tried a similar thing with geom_density() but got nothing that
looked like your von Mises plot, so perhaps someone else can provide a
better solution there.

HTH,
Dennis

> --
> 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
>

Kohske Takahashi

unread,
Aug 12, 2011, 10:19:59 AM8/12/11
to Dennis Murphy, fbielejec, ggplot2
hi,

as Dennis pointed out, the attributes make things worse.
try these examples:

histogram :

set.seed(123)
X = rbeta(100, shape1 = 2, shape2 = 4)
X = 2 * pi * X
X = circular(X, type = "angle", units = "radians", rotation = "clock")

X = data.frame(x=unclass(X)) # drop unnecessary attributes

p <- ggplot(X, aes(x = x))

p <- p + geom_histogram(aes(y = ..count..), binwidth=pi/6)


p <- p + coord_polar(theta = "x", start = 2*pi, direction = 1, expand = FALSE)
print(p)


density estimatin:

# use vonmises and convert it into data.frame before calling ggplot2
D <- data.frame(lapply(vonmises[c("x", "y")], as.numeric))

p <- ggplot(D, aes(x, y))
p + geom_line() + coord_polar(theta = "x", start = 2*pi, direction =
1, expand = FALSE) + ylim(-1, max(D$y))

--
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

filip

unread,
Aug 12, 2011, 12:03:14 PM8/12/11
to Kohske Takahashi, Dennis Murphy, ggplot2
Thank You gents, that's a great and elegant solution indeed.
Reply all
Reply to author
Forward
0 new messages