2

散布図に 3 番目のデータ セットを追加したいのですが、他の 2 つのデータ セットよりも桁違いに大きい値を持っています。

3 つのデータ セットすべての値を 1 つのウィンドウにプロットし、データ セット 3 の y 値を右の y 軸 (軸 4) に、セット 3 のポイントに使用する色と一致する色で示す方法はありますか? これが私が試したことです:

plot(xlab = "Mb", ylab = "Pi", 
      x, yAll,          pch = 6,  cex = .5, col = "blue", type ="b" )
lines(x, yAll_filtered, pch = 18, cex = .5, col = "red",  type = "b")

これで 3 つのデータ セットのうち 2 つがプロットされ、次のステップがわかりません。

理想的には、セット 3 の値を緑色でプロットし、異なるスケールの y 値を右側に緑色で表示することができます。基本的に、これらのパラメーターが満たされた状態でこれらの Y 値をプロットする

plot(x, yAll_normalized, pch = 19, cex = .5, col = "green", type = "b", 
     axis(4))
4

2 に答える 2

1

(問題はどの関数を呼び出すかではなく、これらの背後に あるアイデアを理解することであるため、この質問は stats.SE に移行した方がよいでしょう。)

ここでの基本的な戦略は、@Carl Witthoft が指摘しているように、プロットする前にデータセットをスケーリングすることです。これがどのように機能するかは次のとおりです (使用される関数を理解するには?<function name>、R コンソールのプロンプトで入力します)。

# here I generate some example data, set.seed makes it reproducible
set.seed(33)
x <- 1:20; y0 <- 20; y1 <- 25; y2 <- 300
for(i in 2:20){
  y0 <- c(y0, y0[i-1]+rnorm(1, mean=0.25, sd=1.5))
  y1 <- c(y1, y1[i-1]+rnorm(1, mean=0,    sd=1))
  y2 <- c(y2, y2[i-1]+rnorm(1, mean=-10,  sd=5))
}
max(y0, y1)  
# [1] 35.3668
min(y0, y1)
# [1] 17.77653
# from 0 to 50 seems like a reasonable Y range for the plotting area

windows()
  plot (x, y0, pch=6,  cex=.5, col="blue", type="b", 
        xlab="Mb", ylab="Pi", ylim=c(0, 50))
  lines(x, y1, pch=18, cex=.5, col="red",  type="b")

# We need to create a new variable that will fit within this plotting area
y2new <- scale(y2)        # this makes y2 have mean 0 & sd 1
y2new <- y2new*sd(y0)     # now its sd will equal that of y0
y2new <- y2new+mean(y0)   # now its mean will also equal that of y0

  lines(x, y2new, pch=24, cex=.5, col="green", type="b")

# now y2 fits within the window, but we need an axis which must map the 
#   plotted points to the original values

summary(y0)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#   17.78   20.64   24.34   25.62   30.25   35.37
summary(y2)
#    Min. 1st Qu.  Median    Mean 3rd Qu.    Max. 
#   125.1   178.2   222.2   220.0   266.3   300.0
sd(y0)
# [1] 5.627629
sd(y2)
#[1] 54.76167

# thus, we need an axis w/ 25.62 showing 220 instead, & where 5.63 higher
#   shows 54.76 higher instead

increments <- (mean(y0)-seq(from=0, to=50, by=10))/sd(y0)
increments
# [1]  4.5521432  2.7751960  0.9982488 -0.7786983 -2.5556455
# [6] -4.3325927
newTicks   <- mean(y2) - increments*sd(y2)
newTicks
# [1] -29.24281  68.06579 165.37438 262.68298 359.99158
# [6] 457.30017

# the bottom of the y axis in the plot is 4.55 sd's below y0's mean, 
#   thus the bottom of the new axis should be about -30, and the top of 
#   the new axis should be about 460

  axis(side=4, at=seq(0, 50, 10), labels=round(newTicks), col="green")
  legend("bottomleft", c("y0 (left axis)", "y1 (left axis)", 
         "y2 (right axis)"), pch=c(6, 18, 24), lty=1, 
         col=c("blue", "red", "green"))

ここに画像の説明を入力

これはすべて少し苦痛です。@Carl Wittholfの回答から、関数plotyy()が自動的にこれを行うと思いますが(私は一度も使用したことがありません)、最初にpracmaパッケージをインストール(およびその後ロード)する必要があります。

于 2013-09-18T15:02:51.563 に答える