推荐!超赞的3D可视化图表绘制工具大汇总~~

2022-05-24 15:59:51 浏览数 (1)

点击上方“DataCharm”,选择“星标”公众号

前几期的给大家推荐了关于3D图表的绘制,好多读者私信私信小编推荐一些R语言相关的3D绘图工具? 小编这就安排,比较读者中R语言的使用者还是蛮多的。本期推文内容如下:

  • R语言3D图表绘制工具介绍
  • R语言3D图表包样例

R语言3D图表绘制工具介绍

和Python、MATLAB以及Julia相比,R语言中绘制3D图表的工具较少,且其绘制结果较前几者而言,美观程度稍逊。但也有其独特的优点所在,下面,小编就列举几个R语言中用于绘制3D图表的第三方包,如下所示:

  • R-plot3D包

R语言中绘制3D图表最常见的一个绘图工具,其可绘制3D散点图、3D线图、3D回归平面、3D文本等,此外,还可以对绘图背景、刻度等图层属性进行设置。网址:R-plot3D[1]

  • R-wzRfun包

R-wzRfun包中panel.3d.contour()函数,用于绘制3D拟合曲线,也是较为常用的一种3D图表类型。网址:R-wzRfun[2]

  • R-rayshader包

R-rayshader包作为R语言中为数不多的可将ggplot2对象转变成3D可视化对象的第三方工具,其绘制的可视化图表效果拉满,该库通常是将高程数据进行3D可视化展示,更多展示的是3D立体效果。网址:R-rayshader包[3]

  • R-rgl包

要想使绘制的3D可视化结果可以交互展示,R-rgl包可实现该效果,但该包的因其特有的语法结构,导致其适用性方面不如R-plot3D包,导致学习成本较高。网址:R-rgl包[4]

  • R-plotly包

R-plotly包,超强的3D交互功能,不仅支持R语言,Python和MATLAB的版本3D效果同样惊人,喜欢实时交互可视化效果的同学千万不要错过。网址:R-plotly包[5]

R语言3D图表包样例

这一小节,小编主要列举出各个包的3D可视化示例,大家可根据自己喜好进行学习哈~

R-plot3D包

  • 样例一:3D散点图
代码语言:javascript复制
data(iris)
x <- sep.l <- iris$Sepal.Length
y <- pet.l <- iris$Petal.Length
z <- sep.w <- iris$Sepal.Width
scatter3D(x, y, z, clab = c("Sepal", "Width (cm)"))

Basic scatter plot

修改属性:

代码语言:javascript复制
scatter3D(x, y, z, bty = "g", colkey = FALSE, main ="bty= 'g'")
# User defined
scatter3D(x, y, z, pch = 18, bty = "u", colkey = FALSE, 
   main ="bty= 'u'", col.panel ="steelblue", expand =0.4, 
   col.grid = "darkblue")

Change the type of the box around the plot

  • 样例二:3D线图
代码语言:javascript复制
 scatter3D(x, y, z, phi = 0, bty = "g", type = "l", 
           ticktype = "detailed", lwd = 4)

Line plots

  • 样例三:置信区间散点
代码语言:javascript复制
# Confidence interval
CI <- list(z = matrix(nrow = length(x),
                    data = rep(0.1, 2*length(x))))
# 3D Scatter plot with CI
scatter3D(x, y, z, phi = 0, bty = "g", col = gg.col(100), 
          pch = 18, CI = CI)

Add confidence interval

  • 样例四:回归平面
代码语言:javascript复制
data(mtcars)
# x, y, z variables
x <- mtcars$wt
y <- mtcars$disp
z <- mtcars$mpg
# Compute the linear regression (z = ax   by   d)
fit <- lm(z ~ x   y)
# predict values on regular xy grid
grid.lines = 26
x.pred <- seq(min(x), max(x), length.out = grid.lines)
y.pred <- seq(min(y), max(y), length.out = grid.lines)
xy <- expand.grid( x = x.pred, y = y.pred)
z.pred <- matrix(predict(fit, newdata = xy), 
                 nrow = grid.lines, ncol = grid.lines)
# fitted points for droplines to surface
fitpoints <- predict(fit)
# scatter plot with regression plane
scatter3D(x, y, z, pch = 18, cex = 2, 
    theta = 20, phi = 20, ticktype = "detailed",
    xlab = "wt", ylab = "disp", zlab = "mpg",  
    surf = list(x = x.pred, y = y.pred, z = z.pred,  
    facets = NA, fit = fitpoints), main = "mtcars")

Regression plane

  • 样例五:3D直方图
代码语言:javascript复制
data(VADeaths)
#  hist3D and ribbon3D with greyish background, rotated, rescaled,...
hist3D(z = VADeaths, scale = FALSE, expand = 0.01, bty = "g", phi = 20,
        col = "#0072B2", border = "black", shade = 0.2, ltheta = 90,
        space = 0.3, ticktype = "detailed", d = 2)

3D Histogram

R-wzRfun包

  • 3D曲面拟合
代码语言:javascript复制

library(lattice)
library(latticeExtra)#> Loading required package: RColorBrewerlibrary(RColorBrewer)

#--------------------------------------------
# Example 1.

# display.brewer.all()
colr <- brewer.pal(11, "RdYlGn")
colr <- colorRampPalette(colr, space = "rgb")

grid <- expand.grid(x = seq(0, 1, by = 0.05),
                    y = seq(0, 1, by = 0.05))
grid$z <- with(grid, x   y)

wireframe(z ~ x   y, data = grid,
          scales = list(arrows = FALSE),
          panel.3d.wireframe = panel.3d.contour,
          levels = seq(0.5, 1.5, by = 0.1), type = "on",
          col.regions = colr(101), drape = TRUE)
#--------------------------------------------
# Example 2.

colr <- brewer.pal(11, "Spectral")
colr <- colorRampPalette(colr, space = "rgb")

grid <- expand.grid(x = seq(-1, 1, by = 0.1),
                    y = seq(-1, 1, by = 0.1))
grid$z <- with(grid, 1   0.01 * x   0.05 * y -
                     0.5 * x * y - 0.5 * x^2 - 0.2 * y^2)

wireframe(z ~ x   y, data = grid,
          scales = list(arrows = FALSE),
          zlim = extendrange(grid$z, f = 0.5),
          panel.3d.wireframe = panel.3d.contour,
          nlevels = 18, col = "gray30",
          type = c("on", "top", "bottom"),
          col.regions = colr(101), drape = TRUE)

样例一

代码语言:javascript复制
colr <- brewer.pal(11, "Spectral")
colr <- colorRampPalette(colr, space = "rgb")

grid <- expand.grid(x = seq(-1, 1, by = 0.1),
                    y = seq(-1, 1, by = 0.1))
grid$z <- with(grid, 1   0.01 * x   0.05 * y -
                     0.5 * x * y - 0.5 * x^2 - 0.2 * y^2)

wireframe(z ~ x   y, data = grid,
          scales = list(arrows = FALSE),
          zlim = extendrange(grid$z, f = 0.5),
          panel.3d.wireframe = panel.3d.contour,
          nlevels = 18, col = "gray30",
          type = c("on", "top", "bottom"),
          col.regions = colr(101), drape = TRUE)

样例二

R-rayshader包

这个包小编主要介绍一个例子,如下:

代码语言:javascript复制
a = data.frame(x = rnorm(20000, 10, 1.9), y = rnorm(20000, 10, 1.2))
b = data.frame(x = rnorm(20000, 14.5, 1.9), y = rnorm(20000, 14.5, 1.9))
c = data.frame(x = rnorm(20000, 9.5, 1.9), y = rnorm(20000, 15.5, 1.9))
data = rbind(a, b, c)

#Lines
pp = ggplot(data, aes(x = x, y = y))  
  geom_hex(bins = 20, size = 0.5, color = "black")  
  scale_fill_viridis_c(option = "C")

par(mfrow = c(1, 2))
plot_gg(pp, width = 5, height = 4, scale = 300, raytrace = FALSE, preview = TRUE)
plot_gg(pp, width = 5, height = 4, scale = 300, multicore = TRUE, windowsize = c(1000, 800))
render_camera(fov = 70, zoom = 0.5, theta = 130, phi = 35)
Sys.sleep(0.2)
render_snapshot(clear = TRUE)

更多炫酷样例,小伙伴可参考:R-rayshader包更多样例[6]

R-rgl包

R-rgl包可绘制的图表,大部分plot3d包都可以绘制,这里介绍样例如下:

代码语言:javascript复制
library(rgl);

open3d(windowRect=c(34, 57, 727, 707));

plot3d(
 # ----------------------------------------
   function(x, y) {
     ( ((x 1.1*y)^2)   (3*(y 0.09)^2) ) * exp(-(x^2)-(y^2 ))
   },
 # ----------------------------------------
   col    = colorRampPalette(c('#4499ff', '#ee9933')),
   xlab   = 'x',
   ylab   = 'y',
   zlab   = 'f(x,y)',
   xlim   = c(-3, 3),
   ylim   = c(-3, 3),
   aspect = c(1, 1, 0.5)
)

更多可使用绘图函数,可参考:更多rgl函数[7]

R-plotly包

在R-plotly包可绘制3D散点、3D线图、3D表面图等图表,具体如下:

  • 3D散点图
代码语言:javascript复制
library(plotly)

mtcars$am[which(mtcars$am == 0)] <- 'Automatic'
mtcars$am[which(mtcars$am == 1)] <- 'Manual'
mtcars$am <- as.factor(mtcars$am)

fig <- plot_ly(mtcars, x = ~wt, y = ~hp, z = ~qsec, color = ~am, colors = c('#BF382A', '#0C4B8E'))
fig <- fig %>% add_markers()
fig <- fig %>% layout(scene = list(xaxis = list(title = 'Weight'),
                     yaxis = list(title = 'Gross horsepower'),
                     zaxis = list(title = '1/4 mile time')))

fig

3D Scatter Plots

可以看出:其图表细节(网格线、矢量化等)都是所有介绍的包中表现最好的一个。

  • 3D Surface
代码语言:javascript复制
library(plotly)
# volcano is a numeric matrix that ships with R
fig <- plot_ly(z = ~volcano) %>% add_surface(
  contours = list(
    z = list(
      show=TRUE,
      usecolormap=TRUE,
      highlightcolor="#ff0000",
      project=list(z=TRUE)
      )
    )
  )
fig <- fig %>% layout(
    scene = list(
      camera=list(
        eye = list(x=1.87, y=0.88, z=-0.64)
        )
      )
  )

fig

3D Surface

更多3D图表样例,可参考:R-plotly包更多样例[8]

总结

今天小编给大家汇总了R语言中所有绘制3D图表优秀包,希望小伙伴们可根据自己实际需求选择合理的工具进行图表绘制。

[1]

R-plot3D: https://cran.r-project.org/web/packages/plot3D/index.html。

[2]

R-wzRfun: http://leg.ufpr.br/~walmes/pacotes/wzRfun/index.html。

[3]

R-rayshader包: https://www.rayshader.com/。

[4]

R-rgl包: https://cran.r-project.org/web/packages/rgl/vignettes/rgl.html。

[5]

R-plotly包: https://plotly.com/r/。

[6]

R-rayshader包更多样例: https://www.rayshader.com/。

[7]

更多rgl函数: https://cran.r-project.org/web/packages/rgl/vignettes/rgl.html。

[8]

R-plotly包更多样例: https://plotly.com/r/3d-charts/。

0 人点赞