0

特定のデータセットの各数値変数の基本的なデータ統計を表示する小さな関数が必要です。これまでのところ、次のものがあります。

AnalyzeNumericData <- function(x,GroupVar=NA) {

VarList <- names(x)  
NumVars <- length(VarList)  

for (i in (1:NumVars)) {
     if (is.numeric(x[,VarList[i]])) {
       par(mfrow=c(2,2))
         hist(x[,VarList[i]],main=paste("Histogram of ",VarList[i]),xlab=NA)
         boxplot(x[,VarList[i]],main=paste("Boxplot of ",VarList[i]))
      if (!is.na(GroupVar)) {
        boxplot(x[,VarList[i]]~x[,GroupVar],main=paste("Boxplot of ",VarList[i]," by ", GroupVar))
      }   
        # Add some text to bottom right
        # I've tried plot(1)
        # and then text(1,"MyText"), but this only alows me to put text on one place (in the middle of the plot)
    }
  }
}

AnalyzeNumericData(mtcars,"cyl")

この関数は、数値変数ごとに 3 つのグラフを作成します。右下の領域に 4 番目のグラフの代わりにテキストを追加したいと思います。text() は知っていますが、これを使用するには、最初に空のチャートのようなものを作成する必要があります。

どんなアイデアでも大歓迎です。

4

2 に答える 2

1

を呼び出して、空のプロット エリアを取得しますplot(..., type="n")

plot(1:100, type="n", xaxt="n", yaxt="n", bty="n", xlab="", ylab="")
text(x=10, y=10, "foobar")

たぶん、プロットのさまざまな余白を調整したいでしょう。したがってpar、 も使用できます (詳細についてはpar("mar") <- c(0, 0, 0, 0)、 を参照?parしてください)。

于 2012-07-24T08:15:43.687 に答える
1
AnalyzeNumericData <- function(x,GroupVar=NA) {
    VarList <- names(x)
    NumVars <- length(VarList)

    for (i in 1) {
        if (is.numeric(x[,VarList[i]])) {
            par(mfrow=c(2,2))
            hist(x[,VarList[i]],main=paste("Histogram of ",VarList[i]),xlab=NA)
            boxplot(x[,VarList[i]],main=paste("Boxplot of ",VarList[i]))
            if (!is.na(GroupVar)) {
                boxplot(x[,VarList[i]]~x[,GroupVar],main=paste("Boxplot of ",VarList[i]," by ", GroupVar))
            }
            plot(NA,NA,axes=F,xlim=c(0,10),ylim=c(0,10),xlab="",ylab="")
            text(5,5,labels="MyText")
        }
    }
}

AnalyzeNumericData(mtcars,"cyl")

とを変更xして、とに従ってテキストの位置を調整できます。ytext(x,y,labels="MyText")xlimylim

于 2012-07-24T08:24:02.220 に答える