2016-10-01 2 views
0

lapplyを実行しているときに、グラフィックのタイトルに要素名を入力しようとしています。問題は、名前が繰り返しではなく、最初の名前がす​​べてのプロットで示されているということです。lapply:異なるタイトルを持つ複数の3Dスキャッタプロットを作成する

データの例とコード:それが必要として

if(!require(scatterplot3d)) { 
    install.packages("scatterplot3d"); require(scatterplot3d)} 

head(palha_antes) 
     X.mm Y.mm Altura.mm Rugosidade.mm 
    1 0 0 198.421  24.677 
    2 20 0 189.377  33.721 
    3 40 0 199.212  23.886 
    4 60 0 196.857  26.241 
    5 80 0 193.048  30.050 
    6 100 0 204.922  18.176 

tratamentos = list(palha_antes, palha_depois, exposto_antes, exposto_depois) 

names = c("Tratamento com palha antes da chuva", 
      "Tratamento com palha depois da chuva", 
      "Tratamento sem palha antes da chuva", 
      "Tratamento sem palha depois da chuva") 

names(tratamentos) <- names 

par(mfrow = c(2,2)) 
lapply(tratamentos, function (x) { 
    scatterplot3d(as.numeric(unlist(x[1])), as.numeric(unlist(x[2])), 
       as.numeric(unlist(x[3])), xlab = "X (mm)",ylab = "Y (mm)", 
       main = lapply(names(tratamentos), function(y) y), zlab = "Altura (mm)", pch = 20) 
}) 
par(mfrow = c(1,1)) 

一部main = lapply(names(tratamentos), function(y) y)は実行されません:1

私はY main = lapply(names(tratamentos), function(y) x)の関数としてXを入れた場合、プロットは、すべての値を取得します変数x、名前ではありません。だから、コードのこの部分を解決するための提案はありますか?ありがとうございました。

答えて

0

なぜ2 lapplyが必要ですか?以下の作品。

tratamentos = list(palha_antes, palha_depois, exposto_antes, exposto_depois) 

## don't use "names"; that will mask function "names" 
NAMES <- c("Tratamento com palha antes da chuva", 
      "Tratamento com palha depois da chuva", 
      "Tratamento sem palha antes da chuva", 
      "Tratamento sem palha depois da chuva") 

par(mfrow = c(2,2)) 
lapply(1:length(tratamentos), function (i) { 
    scatterplot3d(tratamentos[[i]][[1]], tratamentos[[i]][[2]], 
       tratamentos[[i]][[3]], xlab = "X (mm)", ylab = "Y (mm)", 
       main = NAMES[i], zlab = "Altura (mm)", pch = 20) 
}) 
par(mfrow = c(1,1)) 

本質的にlapplyを使用する必要はありません。 forループは完璧です:

par(mfrow = c(2,2)) 
for (i in 1:length(tratamentos)) { 
    scatterplot3d(tratamentos[[i]][[1]], tratamentos[[i]][[2]], 
       tratamentos[[i]][[3]], xlab = "X (mm)", ylab = "Y (mm)", 
       main = NAMES[i], zlab = "Altura (mm)", pch = 20) 
    } 
par(mfrow = c(1,1)) 

再現例

set.seed(0) 
d <- data.frame(x = rnorm(50), y = rnorm(50), z = rnorm(50)) 
lst <- list(d, d, d, d) 
NAMES <- LETTERS[1:4] 

## `for` loop 
par(mfrow = c(2,2)) 
for (i in 1:length(lst)) { 
    scatterplot3d(lst[[i]][[1]], lst[[i]][[2]], 
       lst[[i]][[3]], xlab = "X (mm)", ylab = "Y (mm)", 
       main = NAMES[i], zlab = "Altura (mm)", pch = 20) 
    } 
par(mfrow = c(1,1)) 

## `lapply` 
par(mfrow = c(2,2)) 
lapply(1:length(lst), function (i) { 
    scatterplot3d(lst[[i]][[1]], lst[[i]][[2]], 
       lst[[i]][[3]], xlab = "X (mm)", ylab = "Y (mm)", 
       main = NAMES[i], zlab = "Altura (mm)", pch = 20) 
}) 
par(mfrow = c(1,1)) 

注、scatterplot3dは値を返したので、あなたはlapplyを使用している場合、あなたはあなたの画面上に印刷長いリストが表示されます。

enter image description here


あなたのコードへのコメント

  • あなたはx[[1]]代わりのunlist(x[1])を使用することができます。私はまた、余分なas.numericが何であるか分からない。
  • lapply(names(tratamentos), function(y) y)は、ちょうどas.list(names(tratamentos))です。
+0

昨日私はあなたのようなものを試していたし、多くの問題がありました。 Zheyuanありがとうございました。 –

関連する問題