簡體   English   中英

使用ggplot2繪制2D極坐標網格

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM