[英]Plotting a 2D polar mesh with 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")
但是结果远远不能令人满意:
理想情况下,我想填充单元格,而不仅仅是点。 我也希望该图不是一个完整的圆:我只有0到130度的数据。 这可能吗?
这应该可以解决圈子问题:
ggplot(mesh, aes(r, theta, color = value)) +
geom_point() +
coord_polar(theta = "y") +
scale_y_continuous(limits=c(0,360))
我们可以使用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)
注意, expand.grid
返回一个data.frame,所以我们不需要转换它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.