在公共基础上对齐主要和次要 y 轴,在条形中心设置点?
Align the primary and secondary y-axis on the common base, set points in the center of bars?
我正在尝试显示在次要 y 轴上叠加线图的条形图。我在这里遵循示例:http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/。我成功地显示了我的数据,但是 y1 和 y2 轴的开始并不是从公共基础(公共 0)开始的,y2 位于更靠上的位置。
如何在公共基础上正确对齐y1和y2轴?我可以将我的 y1 和 y2 轴都扩展成相同的尺寸吗?还有,如何调整条形中间点的位置?
我的虚拟数据:
x <- 1:5
y1 <- c(10,53,430,80,214)
y2 <- c(0.2,1.2,3.3, 3.5, 4.2)
# create new window
windows()
# set margins
par(mar=c(5,4,4,5)+.1)
# create bar plot with primary axis (y1)
barplot(y1, ylim= c(0,500))
mtext("y1",side=2,line=3)
# add plot with secondary (y2) axis
par(new=TRUE)
plot(x, y2,,type="b",col="red",xaxt="n",yaxt="n",xlab="",ylab="", ylim= c(0,10), lwd = 2, lty = 2, pch = 18)
axis(4)
mtext("y2",side=4,line=3)
当您查看 par()
的文档时,您会发现 xaxs
和 yaxs
选项,您可以使用它们控制两个轴的间隔计算。在 plot()
命令之前调用 par(yaxs = 'i')
或直接使用该选项作为 plot()
的参数将按以下方式更改间隔计算:
Style "i" (internal) just finds an axis with pretty labels that fits
within the original data range.
有关他评论的 TO 的其他信息:
为了使直线的点居中,请改用 lines
,您可以使用 barplot 创建的 x 轴:
par(mar=c(5,4,4,5)+.1)
# create bar plot with primary axis (y1)
par(xpd = F)
ps <- barplot(y1, ylim= c(0,500), xpd = F)
axis(4, at = 0:5 * 100, labels = 0:5 * 2) # transform values
mtext('y1',side = 2, line = 3)
lines(x = ps, y = y2 * 50, type = 'b', col = 'red') # transform values
我正在尝试显示在次要 y 轴上叠加线图的条形图。我在这里遵循示例:http://robjhyndman.com/hyndsight/r-graph-with-two-y-axes/。我成功地显示了我的数据,但是 y1 和 y2 轴的开始并不是从公共基础(公共 0)开始的,y2 位于更靠上的位置。
如何在公共基础上正确对齐y1和y2轴?我可以将我的 y1 和 y2 轴都扩展成相同的尺寸吗?还有,如何调整条形中间点的位置?
我的虚拟数据:
x <- 1:5
y1 <- c(10,53,430,80,214)
y2 <- c(0.2,1.2,3.3, 3.5, 4.2)
# create new window
windows()
# set margins
par(mar=c(5,4,4,5)+.1)
# create bar plot with primary axis (y1)
barplot(y1, ylim= c(0,500))
mtext("y1",side=2,line=3)
# add plot with secondary (y2) axis
par(new=TRUE)
plot(x, y2,,type="b",col="red",xaxt="n",yaxt="n",xlab="",ylab="", ylim= c(0,10), lwd = 2, lty = 2, pch = 18)
axis(4)
mtext("y2",side=4,line=3)
当您查看 par()
的文档时,您会发现 xaxs
和 yaxs
选项,您可以使用它们控制两个轴的间隔计算。在 plot()
命令之前调用 par(yaxs = 'i')
或直接使用该选项作为 plot()
的参数将按以下方式更改间隔计算:
Style "i" (internal) just finds an axis with pretty labels that fits within the original data range.
有关他评论的 TO 的其他信息:
为了使直线的点居中,请改用 lines
,您可以使用 barplot 创建的 x 轴:
par(mar=c(5,4,4,5)+.1)
# create bar plot with primary axis (y1)
par(xpd = F)
ps <- barplot(y1, ylim= c(0,500), xpd = F)
axis(4, at = 0:5 * 100, labels = 0:5 * 2) # transform values
mtext('y1',side = 2, line = 3)
lines(x = ps, y = y2 * 50, type = 'b', col = 'red') # transform values