r - 使用 2 个 y 轴在同一图上绘制多个数据集

标签 r plot scatter-plot

我想将第三个数据集添加到散点图中,但它的值比其他两个数据集大几个数量级。

有没有一种方法可以在一个窗口中绘制所有三个数据集的值,以与我为数据集 3 的点使用的颜色匹配的颜色在右侧 y 轴(轴 4)上表示数据集 3 的 y 值?这是我尝试过的:

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 个值绘制为绿色,并让不同比例的 y 值显示在右侧(也为绿色)。基本上,用满足这些参数绘制这些 Y 值

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

最佳答案

(这个问题可能更好地迁移到 stats.SE,因为问题不在于调用什么函数,而在于理解这些东西背后的想法。)

这里的基本策略是在绘图之前缩放数据集,正如@Carl Witthoft 指出的那样。它的工作原理如下(要了解所使用的任何函数,请在 R 控制台的提示符处输入 ?<function name>):

# 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"))

enter image description here

所有这些都有点痛苦。从@Carl Wittholf 的回答中,我收集了函数 plotyy()会自动为您执行此操作(我从未使用过它),但您必须安装(并随后加载)pracma先打包。

关于r - 使用 2 个 y 轴在同一图上绘制多个数据集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18872649/

相关文章:

python - Matplotlib : set "bad" colour in scatter plot

r - 是否有 R 函数可以帮助我修改巴黎图中散点图点的大小?

python - plt.bar 错误,类型错误 : bar() missing 1 required positional argument: 'height'

python - 如何获得同一图表上两个曲面图的独立缩放比例

R:使用我自己的方程将曲线添加到 x,y 散点图

R- 基于其他列的现值的新列

python - 如何在 Python 中将不同轴的绘图标签添加到同一图例中?

r - 具有 4 个级别 False、FALSE、True、TRUE 的因子,但只需要 2 个级别

r - 如何在 R 中创建多个空矩阵?

r - 如何更改 R 中 corrplot 中特定变量的字体颜色?