问题描述
我想向散点图中添加第三个数据集,但它的值比其他两个数据集大几个数量级.
I want to add a third data set to a scatterplot but it has values orders of magnitude larger than the other two data sets.
有没有办法在一个窗口中绘制所有三个数据集的值,以与我用于设置 3 的点的颜色匹配的颜色在右侧 y 轴(轴 4)上表示数据集 3 的 y 值?这是我尝试过的:
Is there a way to plot values from all three data sets in one window, signifying data set 3's y values on the right y-axis (axis 4) in a color matching the color I use for set 3's points? Here is what I've tried:
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")
这让我绘制了三个数据集中的两个,然后我不知道下一步.
This gets me two of the three data sets plotted, then I don't know the next step.
理想情况下,我可以用绿色绘制 3 个值,并在右侧显示不同比例的 y 值,也以绿色显示.基本上,使用满足这些参数绘制这些 Y 值
Ideally I can plot set 3 values in green and have the differently scaled y values appear on the right, also in green. Basically, plotting these Y values with these parameters met
plot(x, yAll_normalized, pch = 19, cex = .5, col = "green", type = "b",
axis(4))
推荐答案
三个简单的选项:
pracma::plotyy
graphics::axis
只需在绘图前缩放您的数据集.
Just scale your dataset before plotting.
如果你选择第二个,首先以通常的方式绘制你的左手"数据,然后调用 axis(side=4,{other setup arguments})
后跟 lines(data3,...)
If you go with number two, first plot your "left-hand" data the usual way, then call axis(side=4,{other setup arguments})
followed with lines(data3,...)
编辑 - 根据 gung 的有效评论,这里是 plotyy
帮助文件的一部分:
Edit - per gung's valid comment, here's part of the help file for plotyy
:
plotyy(x1, y1, x2, y2, gridp = TRUE, box.col = "grey",
type = "l", lwd = 1, lty = 1,
xlab = "x", ylab = "y", main = "",
col.y1 = "navy", col.y2 = "maroon", ...)
Arguments
x1, x2
x-coordinates for the curves
y1, y2
the y-values, with ordinates y1 left, y2 right.
type
type of the curves, line or points (for both data).
这将在左 y 轴上绘制带有自动缩放的 x1,y1
,在右 y 轴上类似地绘制 x2,y2
.
That will plot the x1,y1
with autoscaling on the left-y axis, and x2,y2
similarly on the right-y axis.
这篇关于在具有 2 个 y 轴的同一图形上绘制多个数据集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!