【问题标题】:Is there an r function to produce excel 3d plot?是否有生成 excel 3d 绘图的 r 函数?
【发布时间】:2019-10-17 06:07:54
【问题描述】:

有没有办法在 r 中生成 excel 3d 绘图?

我有一个矩阵,我想使用 R 以 3d 曲面格式绘制它。 我设法在excel中做到了,它给了我这个情节https://drive.google.com/open?id=1hNqCguPX_ar8ueSK-u-yHTUYZxdyT_cE

我想制作相同的情节,但使用 R 此图是使用此数据生成的

m<-matrix(c(7,73,195,195,416,625,120,52,178,178,349,454,6,83,164,164,244,0,3,52,150,150,329,330),nrow = 4, ncol = 6, byrow = T)

【问题讨论】:

  • 也许这是你可以用 ggplot2gg3D 包做的事情。
  • 如何解释这个矩阵?列/行是否可变?每个单元格是否代表强度?
  • @Gainz 你能给我一个代码吗?我尝试了所有可能性,但最终失败了
  • @bbiasi 是的,行和列是变量,比如说纬度和经度

标签: r plot 3d


【解决方案1】:

很遗憾,使用的数据集不可用,因此无法忠实再现图像并展开细节。

要创建轮廓曲面,使用 ggplot2 包和 geom_raster 函数很有趣。但这会返回一个连续的表面。

library(ggplot2)
library(dplyr)

x <- seq(1, 6, 1)
y <- seq(0, 3, 1)
grid <- expand.grid(x = x,
                    y = y)
m <- matrix(c(7,73,195,195,416,625,120,52,178,178,349,454,6,
            83,164,164,244,0,3,52,150,150,329,330), 
            nrow = 4, ncol = 6, byrow = T)
m2 <- m %>% 
  t %>% as.data.frame()
grid <- grid %>% 
  dplyr::mutate(response = c(m2$V1, m2$V2, m2$V3, m2$V4))

Palet <- c("royalblue2", "orangered3", "lavenderblush3", "gold3")

ggplot2::ggplot(grid, aes(x, y, z = response)) +
  geom_raster(aes(fill = response)) + 
  scale_fill_gradientn(colours = Palet, limits = c(0, 800)) +
  theme_void() +
  theme(legend.position = "bottom")

此外,可以执行数据操作并使用离散的fill。对于每个间隔,将分配一种颜色。

grid2 <- grid %>% 
  dplyr::mutate(cor = ifelse(response >= 0 & response < 200, 1,
                             ifelse(response >= 200 & response < 400, 2,
                                    ifelse(response >= 400 & response < 600, 3,
                                           ifelse(response >= 600, 4, "error")))))

ggplot2::ggplot(grid2, aes(x, y, z = response)) +
  geom_raster(aes(fill = cor)) + 
  scale_fill_manual(values = Palet, labels = c("0-200", "200-400",
                                               "400-600", "600-800"),
                    name = "") +
  theme_void() +
  theme(legend.position = "bottom")


但是,如果考虑矩阵m中的每个单元格都是强度,则可以生成3D图。

library(plot3D)
persp3D(z = m, theta = 60)

3D surface plot from 2D matrix

Plot 3D data in R

Impressive package for 3D and 4D graph - R software and data visualization

【讨论】:

  • 感谢帮助,不过离微软excel做的图还很远(附)
  • @Alaa 看到这个:link1link2。谢谢。
猜你喜欢
  • 2023-01-08
  • 2021-07-28
  • 1970-01-01
  • 1970-01-01
  • 2018-03-15
  • 1970-01-01
  • 2012-04-29
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多