用ggplot2绘制2D极坐标网格

时间:2017-03-14 16:42:57

标签: r ggplot2

我有在2D极坐标网格上计算的数据:

# The mesh created is in two dimensions: r and theta.
# Mesh steps in theta are regular, while mesh steps in r are more refined
# close to the origin
nb.theta <- 50
theta.max <- 130
theta <- seq(0, theta.max, length.out = nb.theta)

nb.r <- 80
# r goes from r0 to rMax
q0 <- 1.1
z <- seq(1, nb.r)
rMax <- 30
r0 <- rMax / (q0 ^ nb.r - 1)
r <- r0 * (q0 ^ z - 1)

# Now let's add some data
mesh <- as.data.frame(expand.grid(r = r, theta = theta))
mesh$value <- mesh$r * mesh$theta / theta.max

现在,我想在R中绘制网格(最好使用ggplot2)。我试过了:

ggplot(mesh, aes(r, theta, color = value)) + geom_point() + coord_polar(theta = "y")

但结果远非令人满意:

enter image description here

理想情况下,我希望细胞充满而不仅仅是积分。我也希望情节不是一个完整的圆圈:我只有0到130度的数据。 这可能吗?

2 个答案:

答案 0 :(得分:2)

这应解决圈问题:

ggplot(mesh, aes(r, theta, color = value)) + 
    geom_point() + 
    coord_polar(theta = "y") + 
    scale_y_continuous(limits=c(0,360))

答案 1 :(得分:1)

我们可以使用geom_tile而不是geom_point,以便我们填充网格。我们需要计算每个窗口的宽度。在这里,我只是将其设置为r / 10,这几乎是正确的。你将能够准确地计算它。

添加ylim可确保只填充部分圆圈。

mesh <- expand.grid(r = r, theta = theta)
mesh$value <- mesh$r * mesh$theta / theta.max
mesh$width <- mesh$r/10


ggplot(mesh, aes(r, theta, fill = value, width = width)) + 
    geom_tile() + 
    coord_polar(theta = "y") +
    ylim(0, 360)

NB expand.grid返回data.frame,因此我们无需转换它。

enter image description here

相关问题