包plot3D
允許你創建3D繪圖中R.它帶有x,y和z座標爲輸入,所以需要一個額外的步驟來轉換球座標到笛卡兒。要做到這一步,予定義一個函數,如下所示:
spher2cart <- function(r, theta, phi) {
x <- r * sin(theta) * cos(phi)
y <- r * sin(theta) * sin(phi)
z <- r * cos(theta)
return(list(x = x, y = y, z = z))
}
下一步驟是定義值的網格的角theta
和phi
。 plot3D
提供功能mesh()
可以輕鬆完成此操作。
library(plot3D)
theta <- seq(0, pi, length = 50)
phi <- seq(0, 2*pi, length = 50)
M <- mesh(theta, phi)
names(M) <- c("theta", "phi")
然後,可以計算出r
(對應於Mathematica的SphericalPlot3d
的第一個參數)的值:
r <- 1 + cos(2 * M$theta)
正如我已經提到的,我們需要在直角座標系來表達這一點:
cart <- spher2cart(r, M$theta, M$phi)
最後,創建情節:
par(mar = c(0, 0, 0, 0))
surf3D(cart$x, cart$y, cart$z, border = "black",
colkey = FALSE, bty = "f",
phi = 20, theta = 30)
有很多選擇,surf3D()
,你可以用?surf3D
閱讀它們。那些我使用的是:
border = "black"
:這會打開黑色網格線。
colkey = FALSE
:關閉顏色圖例
bty = "f"
:繪製完整的座標軸。如果你不想要盒子,省略這個。
phi = 20
,theta = 30
:更改從中看情節
還有一個vignette for plot3d用許多實例的角度。
編輯: 作爲第二個例子我示出了具有一些修改的同積:
- 的表面被着色通過使用
col = "grey"
(其他顏色也是可能的)的灰色。
- 使用
xlab = "", ylab = "", zlab = ""
關閉軸標籤。
這導致了下面的代碼和情節:
surf3D(cart$x, cart$y, cart$z, border = "black",
colkey = FALSE, bty = "f",
phi = 20, theta = 30,
col = "grey",
xlab = "", ylab = "", zlab = "")
這? http://stackoverflow.com/questions/12085095/how-to-plot-a-three-dimensional-sphere-in-r-based-on-center-and-radius – Badger
@Badger,這是爲x,y, z,我正在尋找r,theta,phi。無論如何感謝鏈接 –
這是你的問題,所以你可以自由地編輯錯誤的術語,如果你不想讓這個問題有用(儘管如此,你不應該抱怨downvotes)。 [極座標](https://en.wikipedia.org/wiki/Polar_coordinate_system)是2D的,你在這裏有[球座標](https://en.wikipedia.org/wiki/Spherical_coordinate_system)。 Mathematica函數的名稱也有點暗示... – Stibu