Smoothing 3d plot in R











up vote
1
down vote

favorite
1












I made 3d plot in rgl.persp3d but I don't know how to smooth that to see trend. Or maybe next solution is to implement wireframe in rgl.persp3d (because I need this plot to be interactive). Please, help.



library(mgcv)


x<- rnorm(200)
y<- rnorm(200)
z<-rnorm(200)

tab<-data.frame(x,y,z)
tab

#surface wireframe:

mod <- gam(z ~ te(x, y), data = tab)

wyk <- matrix(fitted(mod), ncol = 20) #8 i 10 też ok

wireframe(wyk, drape=TRUE, colorkey=TRUE)


wireframe



#surface persp3d


library(rgl)
library(akima)


z_interpolation <- 200

tabint <- interp(x, y, z)

x.si <- tabint$x
y.si <- tabint$y
z.si <- tabint$z
nbcol <- 200
vertcol <- cut(t, nbcol)
color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
persp3d(x.si, y.si, z.si, col = color[vertcol], smooth=T)


persp3d



So wireframe is neither smoothed nor interactive
...and rgl.persp3d is interactive but no smoothed. And I can't have both smoothed and interactive.










share|improve this question




























    up vote
    1
    down vote

    favorite
    1












    I made 3d plot in rgl.persp3d but I don't know how to smooth that to see trend. Or maybe next solution is to implement wireframe in rgl.persp3d (because I need this plot to be interactive). Please, help.



    library(mgcv)


    x<- rnorm(200)
    y<- rnorm(200)
    z<-rnorm(200)

    tab<-data.frame(x,y,z)
    tab

    #surface wireframe:

    mod <- gam(z ~ te(x, y), data = tab)

    wyk <- matrix(fitted(mod), ncol = 20) #8 i 10 też ok

    wireframe(wyk, drape=TRUE, colorkey=TRUE)


    wireframe



    #surface persp3d


    library(rgl)
    library(akima)


    z_interpolation <- 200

    tabint <- interp(x, y, z)

    x.si <- tabint$x
    y.si <- tabint$y
    z.si <- tabint$z
    nbcol <- 200
    vertcol <- cut(t, nbcol)
    color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
    persp3d(x.si, y.si, z.si, col = color[vertcol], smooth=T)


    persp3d



    So wireframe is neither smoothed nor interactive
    ...and rgl.persp3d is interactive but no smoothed. And I can't have both smoothed and interactive.










    share|improve this question


























      up vote
      1
      down vote

      favorite
      1









      up vote
      1
      down vote

      favorite
      1






      1





      I made 3d plot in rgl.persp3d but I don't know how to smooth that to see trend. Or maybe next solution is to implement wireframe in rgl.persp3d (because I need this plot to be interactive). Please, help.



      library(mgcv)


      x<- rnorm(200)
      y<- rnorm(200)
      z<-rnorm(200)

      tab<-data.frame(x,y,z)
      tab

      #surface wireframe:

      mod <- gam(z ~ te(x, y), data = tab)

      wyk <- matrix(fitted(mod), ncol = 20) #8 i 10 też ok

      wireframe(wyk, drape=TRUE, colorkey=TRUE)


      wireframe



      #surface persp3d


      library(rgl)
      library(akima)


      z_interpolation <- 200

      tabint <- interp(x, y, z)

      x.si <- tabint$x
      y.si <- tabint$y
      z.si <- tabint$z
      nbcol <- 200
      vertcol <- cut(t, nbcol)
      color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
      persp3d(x.si, y.si, z.si, col = color[vertcol], smooth=T)


      persp3d



      So wireframe is neither smoothed nor interactive
      ...and rgl.persp3d is interactive but no smoothed. And I can't have both smoothed and interactive.










      share|improve this question















      I made 3d plot in rgl.persp3d but I don't know how to smooth that to see trend. Or maybe next solution is to implement wireframe in rgl.persp3d (because I need this plot to be interactive). Please, help.



      library(mgcv)


      x<- rnorm(200)
      y<- rnorm(200)
      z<-rnorm(200)

      tab<-data.frame(x,y,z)
      tab

      #surface wireframe:

      mod <- gam(z ~ te(x, y), data = tab)

      wyk <- matrix(fitted(mod), ncol = 20) #8 i 10 też ok

      wireframe(wyk, drape=TRUE, colorkey=TRUE)


      wireframe



      #surface persp3d


      library(rgl)
      library(akima)


      z_interpolation <- 200

      tabint <- interp(x, y, z)

      x.si <- tabint$x
      y.si <- tabint$y
      z.si <- tabint$z
      nbcol <- 200
      vertcol <- cut(t, nbcol)
      color = rev(rainbow(nbcol, start = 0/6, end = 4/6))
      persp3d(x.si, y.si, z.si, col = color[vertcol], smooth=T)


      persp3d



      So wireframe is neither smoothed nor interactive
      ...and rgl.persp3d is interactive but no smoothed. And I can't have both smoothed and interactive.







      r rgl wireframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 17 '17 at 15:30

























      asked Jan 17 '17 at 15:01









      aniusni

      346




      346
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          2
          down vote













          rgl just draws what you give it. You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. For example,



          library(mgcv)

          x<- rnorm(200)
          y<- rnorm(200)
          z<-rnorm(200)

          tab<-data.frame(x,y,z)
          tab

          #surface wireframe:

          mod <- gam(z ~ te(x, y), data = tab)

          library(rgl)
          library(deldir)

          zfit <- fitted(mod)
          col <- cm.colors(20)[1 +
          round(19*(zfit - min(zfit))/diff(range(zfit)))]

          persp3d(deldir(x, y, z = zfit), col = col)
          aspect3d(1, 2, 1)


          This gives a nice smooth surface, for example



          enter image description here






          share|improve this answer




























            up vote
            0
            down vote













            A simpler way, without the delaunay stuff:



            library(mgcv)

            x <- rnorm(200)
            y <- rnorm(200)
            z <- rnorm(200)

            tab <- data.frame(x,y,z)

            mod <- mgcv::gam(z ~ te(x, y), data = tab)
            grid <- -5:5
            zfit <- predict(mod, expand.grid(x = grid, y = grid))
            persp3d(grid, grid, zfit)





            share|improve this answer





















              Your Answer






              StackExchange.ifUsing("editor", function () {
              StackExchange.using("externalEditor", function () {
              StackExchange.using("snippets", function () {
              StackExchange.snippets.init();
              });
              });
              }, "code-snippets");

              StackExchange.ready(function() {
              var channelOptions = {
              tags: "".split(" "),
              id: "1"
              };
              initTagRenderer("".split(" "), "".split(" "), channelOptions);

              StackExchange.using("externalEditor", function() {
              // Have to fire editor after snippets, if snippets enabled
              if (StackExchange.settings.snippets.snippetsEnabled) {
              StackExchange.using("snippets", function() {
              createEditor();
              });
              }
              else {
              createEditor();
              }
              });

              function createEditor() {
              StackExchange.prepareEditor({
              heartbeatType: 'answer',
              convertImagesToLinks: true,
              noModals: true,
              showLowRepImageUploadWarning: true,
              reputationToPostImages: 10,
              bindNavPrevention: true,
              postfix: "",
              imageUploader: {
              brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
              contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
              allowUrls: true
              },
              onDemand: true,
              discardSelector: ".discard-answer"
              ,immediatelyShowMarkdownHelp:true
              });


              }
              });














              draft saved

              draft discarded


















              StackExchange.ready(
              function () {
              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41700400%2fsmoothing-3d-plot-in-r%23new-answer', 'question_page');
              }
              );

              Post as a guest















              Required, but never shown

























              2 Answers
              2






              active

              oldest

              votes








              2 Answers
              2






              active

              oldest

              votes









              active

              oldest

              votes






              active

              oldest

              votes








              up vote
              2
              down vote













              rgl just draws what you give it. You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. For example,



              library(mgcv)

              x<- rnorm(200)
              y<- rnorm(200)
              z<-rnorm(200)

              tab<-data.frame(x,y,z)
              tab

              #surface wireframe:

              mod <- gam(z ~ te(x, y), data = tab)

              library(rgl)
              library(deldir)

              zfit <- fitted(mod)
              col <- cm.colors(20)[1 +
              round(19*(zfit - min(zfit))/diff(range(zfit)))]

              persp3d(deldir(x, y, z = zfit), col = col)
              aspect3d(1, 2, 1)


              This gives a nice smooth surface, for example



              enter image description here






              share|improve this answer

























                up vote
                2
                down vote













                rgl just draws what you give it. You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. For example,



                library(mgcv)

                x<- rnorm(200)
                y<- rnorm(200)
                z<-rnorm(200)

                tab<-data.frame(x,y,z)
                tab

                #surface wireframe:

                mod <- gam(z ~ te(x, y), data = tab)

                library(rgl)
                library(deldir)

                zfit <- fitted(mod)
                col <- cm.colors(20)[1 +
                round(19*(zfit - min(zfit))/diff(range(zfit)))]

                persp3d(deldir(x, y, z = zfit), col = col)
                aspect3d(1, 2, 1)


                This gives a nice smooth surface, for example



                enter image description here






                share|improve this answer























                  up vote
                  2
                  down vote










                  up vote
                  2
                  down vote









                  rgl just draws what you give it. You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. For example,



                  library(mgcv)

                  x<- rnorm(200)
                  y<- rnorm(200)
                  z<-rnorm(200)

                  tab<-data.frame(x,y,z)
                  tab

                  #surface wireframe:

                  mod <- gam(z ~ te(x, y), data = tab)

                  library(rgl)
                  library(deldir)

                  zfit <- fitted(mod)
                  col <- cm.colors(20)[1 +
                  round(19*(zfit - min(zfit))/diff(range(zfit)))]

                  persp3d(deldir(x, y, z = zfit), col = col)
                  aspect3d(1, 2, 1)


                  This gives a nice smooth surface, for example



                  enter image description here






                  share|improve this answer












                  rgl just draws what you give it. You need to use mgcv as in your first example to do the smoothing, but you don't get a matrix of fitted values back at the end, so you'll want to use deldir to turn the results into a surface. For example,



                  library(mgcv)

                  x<- rnorm(200)
                  y<- rnorm(200)
                  z<-rnorm(200)

                  tab<-data.frame(x,y,z)
                  tab

                  #surface wireframe:

                  mod <- gam(z ~ te(x, y), data = tab)

                  library(rgl)
                  library(deldir)

                  zfit <- fitted(mod)
                  col <- cm.colors(20)[1 +
                  round(19*(zfit - min(zfit))/diff(range(zfit)))]

                  persp3d(deldir(x, y, z = zfit), col = col)
                  aspect3d(1, 2, 1)


                  This gives a nice smooth surface, for example



                  enter image description here







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Jan 17 '17 at 17:20









                  user2554330

                  8,37511237




                  8,37511237
























                      up vote
                      0
                      down vote













                      A simpler way, without the delaunay stuff:



                      library(mgcv)

                      x <- rnorm(200)
                      y <- rnorm(200)
                      z <- rnorm(200)

                      tab <- data.frame(x,y,z)

                      mod <- mgcv::gam(z ~ te(x, y), data = tab)
                      grid <- -5:5
                      zfit <- predict(mod, expand.grid(x = grid, y = grid))
                      persp3d(grid, grid, zfit)





                      share|improve this answer

























                        up vote
                        0
                        down vote













                        A simpler way, without the delaunay stuff:



                        library(mgcv)

                        x <- rnorm(200)
                        y <- rnorm(200)
                        z <- rnorm(200)

                        tab <- data.frame(x,y,z)

                        mod <- mgcv::gam(z ~ te(x, y), data = tab)
                        grid <- -5:5
                        zfit <- predict(mod, expand.grid(x = grid, y = grid))
                        persp3d(grid, grid, zfit)





                        share|improve this answer























                          up vote
                          0
                          down vote










                          up vote
                          0
                          down vote









                          A simpler way, without the delaunay stuff:



                          library(mgcv)

                          x <- rnorm(200)
                          y <- rnorm(200)
                          z <- rnorm(200)

                          tab <- data.frame(x,y,z)

                          mod <- mgcv::gam(z ~ te(x, y), data = tab)
                          grid <- -5:5
                          zfit <- predict(mod, expand.grid(x = grid, y = grid))
                          persp3d(grid, grid, zfit)





                          share|improve this answer












                          A simpler way, without the delaunay stuff:



                          library(mgcv)

                          x <- rnorm(200)
                          y <- rnorm(200)
                          z <- rnorm(200)

                          tab <- data.frame(x,y,z)

                          mod <- mgcv::gam(z ~ te(x, y), data = tab)
                          grid <- -5:5
                          zfit <- predict(mod, expand.grid(x = grid, y = grid))
                          persp3d(grid, grid, zfit)






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Nov 11 at 1:58









                          dash2

                          1254




                          1254






























                              draft saved

                              draft discarded




















































                              Thanks for contributing an answer to Stack Overflow!


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.





                              Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


                              Please pay close attention to the following guidance:


                              • Please be sure to answer the question. Provide details and share your research!

                              But avoid



                              • Asking for help, clarification, or responding to other answers.

                              • Making statements based on opinion; back them up with references or personal experience.


                              To learn more, see our tips on writing great answers.




                              draft saved


                              draft discarded














                              StackExchange.ready(
                              function () {
                              StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f41700400%2fsmoothing-3d-plot-in-r%23new-answer', 'question_page');
                              }
                              );

                              Post as a guest















                              Required, but never shown





















































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown

































                              Required, but never shown














                              Required, but never shown












                              Required, but never shown







                              Required, but never shown







                              Popular posts from this blog

                              Florida Star v. B. J. F.

                              Danny Elfman

                              Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues