3 min read

複数層の3次元プロットを1枚の図で (R)

## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'agricolae'
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'slackr'
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'akima'
## Warning in library(package, lib.loc = lib.loc, character.only = TRUE,
## logical.return = TRUE, : there is no package called 'rgl'

多層の3Dプロットを作る (メモ

英語で検索しても、日本語で検索しても、Rcmdrを用いた対話的な3D散布図くらいしか簡単に見つからなかったので (20160210現在)

データ作り

df_3d <-
  data_frame(x = rep(1:20, each = 20),
             y = rep(1:20, times = 20),
             z1 = x + y + 0.5 * x * y,
             z2 = 5 * x + 3 * y - 0.5 * x * y) %>%
  tidyr::gather(group, z, -x, -y)

df_3d_group1 <-
  df_3d %>%
  filter(group == "z1")
df_3d_group2 <-
  df_3d %>%
  filter(group == "z2")

df_3d_group1 %>% str
## Classes 'tbl_df', 'tbl' and 'data.frame':    400 obs. of  4 variables:
##  $ x    : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ y    : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ group: chr  "z1" "z1" "z1" "z1" ...
##  $ z    : num  2.5 4 5.5 7 8.5 10 11.5 13 14.5 16 ...
df_3d_group2 %>% str
## Classes 'tbl_df', 'tbl' and 'data.frame':    400 obs. of  4 variables:
##  $ x    : int  1 1 1 1 1 1 1 1 1 1 ...
##  $ y    : int  1 2 3 4 5 6 7 8 9 10 ...
##  $ group: chr  "z2" "z2" "z2" "z2" ...
##  $ z    : num  7.5 10 12.5 15 17.5 20 22.5 25 27.5 30 ...

グラフ作り

rglをjekyll生成のページに埋め込むのがきつそうなのでコードのみ

library(rgl)

plot3d(df_3d_group1$x, df_3d_group1$y, df_3d_group1$z, zlim = c(-50, 250))
plot3d(df_3d_group2$x, df_3d_group2$y, df_3d_group2$z, col = "red", zlim = c(-50, 250))

曲面を追加する

library(akima)

surf1 <- interp(df_3d_group1$x, df_3d_group1$y, df_3d_group1$z)
surf2 <- interp(df_3d_group2$x, df_3d_group2$y, df_3d_group2$z)

plot3d(df_3d_group1$x, df_3d_group1$y, df_3d_group1$z, col = "grey10", zlim = c(-50, 250))
plot3d(df_3d_group2$x, df_3d_group2$y, df_3d_group2$z, col = "red", zlim = c(-50, 250))

surface3d(surf1$x, surf1$y, surf1$z, col = "grey", alpha = .3)
surface3d(surf2$x, surf2$y, surf2$z, col = "red", alpha = .3)

出力 (画像)

surface

surface

参考ページ

不規則な間隔のデータ点を曲面プロットする (yohm13さん@Qiita)

session_info()
##  setting  value                       
##  version  R version 3.5.0 (2018-04-23)
##  system   x86_64, darwin15.6.0        
##  ui       X11                         
##  language (EN)                        
##  collate  en_US.UTF-8                 
##  tz       Australia/Brisbane          
##  date     2018-05-16                  
## 
##  package      * version   date       source                          
##  assertthat     0.2.0     2017-04-11 CRAN (R 3.5.0)                  
##  backports      1.1.2     2017-12-13 CRAN (R 3.5.0)                  
##  base         * 3.5.0     2018-04-24 local                           
##  bindr          0.1.1     2018-03-13 CRAN (R 3.5.0)                  
##  bindrcpp     * 0.2.2     2018-03-29 CRAN (R 3.5.0)                  
##  bitops       * 1.0-6     2013-08-17 CRAN (R 3.5.0)                  
##  blogdown       0.6       2018-04-18 CRAN (R 3.5.0)                  
##  bookdown       0.7       2018-02-18 CRAN (R 3.5.0)                  
##  codetools      0.2-15    2016-10-05 CRAN (R 3.5.0)                  
##  colorspace     1.3-2     2016-12-14 CRAN (R 3.5.0)                  
##  compiler       3.5.0     2018-04-24 local                           
##  data.table   * 1.10.4-3  2017-10-27 CRAN (R 3.5.0)                  
##  datasets     * 3.5.0     2018-04-24 local                           
##  devtools     * 1.13.5    2018-02-18 CRAN (R 3.5.0)                  
##  digest         0.6.15    2018-01-28 CRAN (R 3.5.0)                  
##  doParallel     1.0.11    2017-09-28 CRAN (R 3.5.0)                  
##  doRNG          1.6.6     2017-04-10 CRAN (R 3.5.0)                  
##  dplyr        * 0.7.4     2017-09-28 CRAN (R 3.5.0)                  
##  evaluate       0.10.1    2017-06-24 CRAN (R 3.5.0)                  
##  foreach      * 1.4.4     2017-12-12 CRAN (R 3.5.0)                  
##  ggplot2      * 2.2.1     2016-12-30 CRAN (R 3.5.0)                  
##  glue           1.2.0     2017-10-29 CRAN (R 3.5.0)                  
##  graphics     * 3.5.0     2018-04-24 local                           
##  grDevices    * 3.5.0     2018-04-24 local                           
##  grid         * 3.5.0     2018-04-24 local                           
##  gridExtra    * 2.3       2017-09-09 CRAN (R 3.5.0)                  
##  gtable       * 0.2.0     2016-02-26 CRAN (R 3.5.0)                  
##  htmltools      0.3.6     2017-04-28 CRAN (R 3.5.0)                  
##  iterators      1.0.9     2017-12-12 CRAN (R 3.5.0)                  
##  knitr        * 1.20      2018-02-20 CRAN (R 3.5.0)                  
##  lazyeval       0.2.1     2017-10-29 CRAN (R 3.5.0)                  
##  lubridate    * 1.7.4     2018-04-11 CRAN (R 3.5.0)                  
##  magrittr     * 1.5       2014-11-22 CRAN (R 3.5.0)                  
##  MASS         * 7.3-49    2018-02-23 CRAN (R 3.5.0)                  
##  memoise        1.1.0     2017-04-21 CRAN (R 3.5.0)                  
##  methods      * 3.5.0     2018-04-24 local                           
##  munsell        0.4.3     2016-02-13 CRAN (R 3.5.0)                  
##  parallel       3.5.0     2018-04-24 local                           
##  pforeach     * 1.3       2018-04-25 Github (hoxo-m/pforeach@2c44f3b)
##  pillar         1.2.1     2018-02-27 CRAN (R 3.5.0)                  
##  pkgconfig      2.0.1     2017-03-21 CRAN (R 3.5.0)                  
##  pkgmaker       0.22      2014-05-14 CRAN (R 3.5.0)                  
##  plyr         * 1.8.4     2016-06-08 CRAN (R 3.5.0)                  
##  purrr          0.2.4     2017-10-18 CRAN (R 3.5.0)                  
##  R6             2.2.2     2017-06-17 CRAN (R 3.5.0)                  
##  RColorBrewer * 1.1-2     2014-12-07 CRAN (R 3.5.0)                  
##  Rcpp           0.12.16   2018-03-13 CRAN (R 3.5.0)                  
##  RCurl        * 1.95-4.10 2018-01-04 CRAN (R 3.5.0)                  
##  registry       0.5       2017-12-03 CRAN (R 3.5.0)                  
##  reshape2     * 1.4.3     2017-12-11 CRAN (R 3.5.0)                  
##  rlang          0.2.0     2018-02-20 CRAN (R 3.5.0)                  
##  rmarkdown      1.9       2018-03-01 CRAN (R 3.5.0)                  
##  rngtools       1.2.4     2014-03-06 CRAN (R 3.5.0)                  
##  rprojroot      1.3-2     2018-01-03 CRAN (R 3.5.0)                  
##  scales       * 0.5.0     2017-08-24 CRAN (R 3.5.0)                  
##  stats        * 3.5.0     2018-04-24 local                           
##  stringi        1.2.2     2018-05-02 cran (@1.2.2)                   
##  stringr      * 1.3.1     2018-05-10 cran (@1.3.1)                   
##  tibble         1.4.2     2018-01-22 CRAN (R 3.5.0)                  
##  tidyr        * 0.8.0     2018-01-29 CRAN (R 3.5.0)                  
##  tidyselect     0.2.4     2018-02-26 CRAN (R 3.5.0)                  
##  tools          3.5.0     2018-04-24 local                           
##  utils        * 3.5.0     2018-04-24 local                           
##  withr          2.1.2     2018-03-15 CRAN (R 3.5.0)                  
##  xfun           0.1       2018-01-22 CRAN (R 3.5.0)                  
##  xtable         1.8-2     2016-02-05 CRAN (R 3.5.0)                  
##  yaml           2.1.18    2018-03-08 CRAN (R 3.5.0)