msc*_*lli 18 r surface plotly r-plotly
我有一个功能r(x),我想围绕x轴旋转,以获得我想要使用(着色)添加到现有绘图的旋转实体.plot_lyadd_surfacex
这是一个例子:
library(dplyr)
library(plotly)
# radius depends on x
r <- function(x) x^2
# interval of interest
int <- c(1, 3)
# number of points along the x-axis
nx <- 20
# number of points along the rotation
ntheta <- 36
# set x points and get corresponding radii
coords <- data_frame(x = seq(int[1], int[2], length.out = nx), r = r(x))
# for each x: rotate r to get y and z coordinates
# edit: ensure 0 and pi are both amongst the angles used
coords %<>%
rowwise() %>%
do(data_frame(x = .$x, r = .$r,
theta = seq(0, pi, length.out = ntheta / 2 + 1) %>%
c(pi + .[-c(1, length(.))]))) %>%
ungroup %>%
mutate(y = r * cos(theta), z = r * sin(theta))
# plot points to make sure the coordinates define the desired shape
coords %>%
plot_ly(x = ~x, y = ~y, z = ~z, color = ~x) %>%
add_markers()
Run Code Online (Sandbox Code Playgroud)
plotly曲面(理想情况下,两端都是开放的)?编辑(1):
到目前为止,这是我最好的尝试:
# get all x & y values used (sort to connect halves on the side)
xs <-
unique(coords$x) %>%
sort
ys <-
unique(coords$y) %>%
sort
# for each possible x/y pair: get z^2 value
coords <-
expand.grid(x = xs, y = ys) %>%
as_data_frame %>%
mutate(r = r(x), z2 = r^2 - y^2)
# format z coordinates above x/y plane as matrix where columns
# represent x and rows y
zs <- matrix(sqrt(coords$z2), ncol = length(xs), byrow = TRUE)
# format x coordiantes as matrix as above (for color gradient)
gradient <-
rep(xs, length(ys)) %>%
matrix(ncol = length(xs), byrow = TRUE)
# plot upper half of shape as surface
p <- plot_ly(x = xs, y = ys, z = zs, surfacecolor = gradient,
type = "surface", colorbar = list(title = 'x'))
# plot lower have of shape as second surface
p %>%
add_surface(z = -zs, showscale = FALSE)
Run Code Online (Sandbox Code Playgroud)
虽然这给出了所需的形状,
x/ y平面有"剃刀齿" .0和pi在theta向量中解决)x代替它z(尽管到目前为止我没有看到它).gradient矩阵解决)编辑(2):
以下是使用单个表面的尝试:
# close circle in y-direction
ys <- c(ys, rev(ys), ys[1])
# get corresponding z-values
zs <- rbind(zs, -zs[nrow(zs):1, ], zs[1, ])
# as above, but for color gradient
gradient <-
rbind(gradient, gradient[nrow(gradient):1, ], gradient[1, ])
# plot single surface
plot_ly(x = xs, y = ys, z = zs, surfacecolor = gradient,
type = "surface", colorbar = list(title = 'x'))
Run Code Online (Sandbox Code Playgroud)
令人惊讶的是,虽然这应该连接与x/ y平面正交的两个半部分创建完整的形状,但它仍然遭受与上述解决方案相同的"剃刀齿"效果:
编辑(3):
事实证明,当值接近0时,缺失部分的结果z是NaN:
# color points 'outside' the solid purple
gradient[is.nan(zs)] <- -1
# show those previously hidden points
zs[is.nan(zs)] <- 0
# plot exactly as before
plot_ly(x = xs, y = ys, z = zs, surfacecolor = gradient,
type = "surface", colorbar = list(title = 'x'))
Run Code Online (Sandbox Code Playgroud)
这可能是由于减法的数值不稳定性导致的,r^2并且y过于接近,导致负输入,sqrt实际输入仍然是非负的.
这种接缝与数值问题无关,即使考虑到+ -4'接近'为零,也不能完全避免"剃刀牙齿"效应:
# re-calculate z-values rounding to zero if 'close'
eps <- 4
zs <- with(coords, ifelse(abs(z2) < eps, 0, sqrt(z2))) %>%
matrix(ncol = length(xs), byrow = TRUE) %>%
rbind(-.[nrow(.):1, ], .[1, ])
# plot exactly as before
plot_ly(x = xs, y = ys, z = zs, surfacecolor = gradient,
type = "surface", colorbar = list(title = 'x'))
Run Code Online (Sandbox Code Playgroud)
小智 7
有趣的问题,我一直在努力使用表面密度来改进您的解决方案.你可以通过分层多行进行黑客攻击,这对于此非常好.例如,只对原始版本所做的更改是使用更多x点:nx到1000,并将add_markers更改为add_lines.可能不是可扩展的,但适用于这种大小的数据:)
library(dplyr)
library(plotly)
# radius depends on x
r <- function(x) x^2
# interval of interest
int <- c(1, 3)
# number of points along the x-axis
nx <- 1000
# number of points along the rotation
ntheta <- 36
# set x points and get corresponding radii
coords <- data_frame(x = seq(int[1], int[2], length.out = nx), r = r(x))
# for each x: rotate r to get y and z coordinates
# edit: ensure 0 and pi are both amongst the angles used
coords %<>%
rowwise() %>%
do(data_frame(x = .$x, r = .$r,
theta = seq(0, pi, length.out = ntheta / 2 + 1) %>%
c(pi + .[-c(1, length(.))]))) %>%
ungroup %>%
mutate(y = r * cos(theta), z = r * sin(theta))
# plot points to make sure the coordinates define the desired shape
coords %>%
plot_ly(x = ~x, y = ~y, z = ~z, color = ~x) %>%
add_lines()
Run Code Online (Sandbox Code Playgroud)
最好的,乔尼
小智 6
我有另一个裂缝,并使用"表面"类型有一个更接近的解决方案.通过nx = 5和ntheta = 18来查看第一个曲面图的结果有什么帮助.它是jaggardy的原因是因为它连接zs中的列的方式(跨越x点).它必须从它周围的较大环的一部分向上连接,这导致密度达到峰值以满足这一点.
我不能100%摆脱这种jaggardy行为.我做了这些改变:
步骤来自它如何尝试加入x环.从理论上讲,如果你有更多的x环,它应该消除这种不稳定性,但这是耗时的.
我不认为这回答Q 100%,我不确定这个库是否适合这项工作.如果有任何Q,请联系.
library(dplyr)
library(plotly)
# radius depends on x
r <- function(x) x^2
# interval of interest
int <- c(1, 3)
# number of points along the x-axis
nx <- 40
# number of points along the rotation
ntheta <- 18
# set x points and get corresponding radii
coords <- data_frame(x = seq(int[1], int[2], length.out = nx), r = r(x))
# theta: add small increments at the extremities for the density plot
theta <- seq(0, pi, length.out = ntheta / 2 + 1)
theta <- c(theta, pi + theta)
theta <- theta[theta != 2*pi]
inc <- 0.00001
theta <- c(theta, inc, pi + inc, pi - inc, 2*pi - inc)
theta <- sort(theta)
coords %<>%
rowwise() %>%
do(data_frame(x = .$x, r = .$r, theta = theta)) %>%
ungroup %>%
mutate(y = r * cos(theta), z = r * sin(theta))
# get all x & y values used (sort to connect halves on the side)
xs <-
unique(coords$x) %>%
sort
ys <-
unique(coords$y) %>%
sort
# for each possible x/y pair: get z^2 value
coords <-
expand.grid(x = xs, y = ys) %>%
as_data_frame %>%
mutate(r = r(x), z2 = r^2 - y^2)
# format z coordinates above x/y plane as matrix where columns
# represent x and rows y
zs <- matrix(sqrt(coords$z2), ncol = length(xs), byrow = TRUE)
zs2 <- zs
L <- ncol(zs)
for(i in (L-1):1){
w <- which(!is.na(zs[, (i+1)]) & is.na(zs[, i]))
zs2[w, i] <- 0
}
# format x coordiantes as matrix as above (for color gradient)
gradient <-
rep(xs, length(ys)) %>%
matrix(ncol = length(xs), byrow = TRUE)
# plot upper half of shape as surface
p <- plot_ly(x = xs, y = ys, z = zs2, surfacecolor = gradient,
type = "surface", colorbar = list(title = 'x'))
# plot lower have of shape as second surface
p %>%
add_surface(z = -zs2, showscale = FALSE)
Run Code Online (Sandbox Code Playgroud)
这不会回答您的问题,但它会给出您可以在网页中进行交互的结果:不要使用plot_ly,使用rgl.例如,
library(rgl)
# Your initial values...
r <- function(x) x^2
int <- c(1, 3)
nx <- 20
ntheta <- 36
# Set up x and colours for each x
x <- seq(int[1], int[2], length.out = nx)
cols <- colorRampPalette(c("blue", "yellow"), space = "Lab")(nx)
clear3d()
shade3d(turn3d(x, r(x), n = ntheta, smooth = TRUE,
material = list(color = rep(cols, each = 4*ntheta))))
aspect3d(1,1,1)
decorate3d()
rglwidget()
Run Code Online (Sandbox Code Playgroud)
您可以通过一些摆弄来更好地处理颜色:您可能想要创建一个使用x或r(x)设置颜色的功能,而不是像我一样重复颜色.
这是结果:
一种解决方案是翻转轴,以便围绕 z 轴而不是 x 轴旋转。鉴于您要添加此数字的现有图表,我不知道这是否可行,但它确实可以轻松解决“牙齿”问题。
xs <- seq(-9,9,length.out = 20)
ys <- seq(-9,9,length.out = 20)
coords <-
expand.grid(x = xs, y = ys) %>%
mutate(z2 = (x^2 + y^2)^(1/4))
zs <- matrix(coords$z2, ncol = length(xs), byrow = TRUE)
plot_ly(x = xs, y = ys, z = zs, surfacecolor = zs,
type = "surface", colorbar = list(title = 'x')) %>%
layout(scene = list(zaxis = list(range = c(1,3))))
Run Code Online (Sandbox Code Playgroud)