2016-11-15 10 views
7

私は3つの行列と3つの高さ(任意の正の実数)を持つベクトルhのリストaを持っています。これらの行列は三角形、すなわちプリズムの底辺を形成する。私はベクトルhの情報をプリズムを構成するために追加したいと思う。ggplot2とプロットを使ってプロット3Dプリズム

2D(pplot)でグラフィックをプロットする関数を作成しました。下の図のようにプリズムをプロットするにはどうすればよいですか?

pplotうと玩具の問題が一例で

library(ggplot2) 
pplot <- function(polygon){ 
    polygon <- lapply(polygon, function(x) {colnames(x) <- NULL; x}) 
    vertex_number = nrow(polygon[[1]]) 
    g = ggplot2::ggplot() 
    names(polygon) = 1:length(polygon) 
    k <- plyr::ldply(polygon, function(x) data.frame(x)) 
    g <- ggplot2::ggplot(k, ggplot2::aes(x = X1, y = X2, group = .id)) + ggplot2::geom_polygon(colour = "black", fill = NA) 
    return(g) 
} 

a <- list() 
b1 <- matrix(rnorm(6), ncol = 2) 
b2 <- matrix(rnorm(6), ncol = 2) 
b3 <- matrix(rnorm(6), ncol = 2) 

a[[1]] <- b1 
a[[2]] <- b2 
a[[3]] <- b3 

h <- c(.3, .5, .1) 
#pplot function example 
pplot(a) 

グラフィック座標a = db = fc = e頂点であり、すべての情報がある

An example desired

所望aにあります。

観測1:データはリストでなければなりません。

観測2:私はポルトガル語で投稿を作成しましたが、誰も答えませんでした。これを行うことはできますか、それとも不正行為ですか? (私はここに新しいです) https://pt.stackoverflow.com/questions/165538/plotar-figuras-3d-para-dados-em-lista

+0

私はggplotが3Dをしなかったと思いました。その仮定が間違っていると分かっている例を教えてください。 –

+0

例を見ることができます:https://www.r-bloggers.com/3d-plots-with-ggplot2-and-plotly/ –

+0

私はそのブログを読んでいるので、それはうんざりです。 –

答えて

4

私は正しくタスクを理解していません。それにもかかわらず、パッケージrglのソリューションの草案があります。私の意見では、Rのフレームワークをプロットするのに最高の3Dです。なぜなら、はるかに高速で、javascript API(プロット、rthreejsなど)よりも優れているからです。

#### load package rgl #### 
library(rgl) 

set.seed(1232) 

#### construct test list with coordinate matrices #### 
a <- list() 
b1 <- matrix(rnorm(6), ncol = 2) 
b2 <- matrix(rnorm(6), ncol = 2) 
b3 <- matrix(rnorm(6), ncol = 2) 

a[[1]] <- b1 
a[[2]] <- b2 
a[[3]] <- b3 

#### define test height vector #### 
h <- c(.3, .5, .1) 

#### simple plot prism function #### 
# a: list with coordinate matrices 
# h: height vector 
plotprism <- function(a, h){ 
    # general loop to plot every prism 
    for(i in 1:length(h)){ 
    # transform matrizes to data.frames and add height column 
    # -> separation of top and bottom triangle 
    top <- data.frame(a[[i]], h[i]) 
    bottom <- data.frame(a[[i]], 0) 
    # adjust colnames to axis names 
    colnames(top) <- c("x", "y", "z") 
    colnames(bottom) <- c("x", "y", "z") 
    # plot triangles (as wireframes) 
    triangles3d(bottom, front = "line", back = "line") 
    triangles3d(top, front = "line", back = "line") 
    # plot vertical lines to connect the triangles 
    for(i in 0:2){ 
     segments3d(
     x = c(bottom$x[1+i], top$x[1+i]), 
     y = c(bottom$y[1+i], top$y[1+i]), 
     z = c(bottom$z[1+i], top$z[1+i]) 
    ) 
    } 
    } 
    #### add coordinate system #### 
    axes3d() 
} 

#### call plot function for test data #### 
plotprism(a, h) 

結果: enter image description here

+0

ありがとうございました! –

関連する問題