简体   繁体   English

在直方图中创建辅助 y 轴

[英]Creating a secondary y-axis in histogram

I have found multiple ways to create a secondary y-axis in plot but I couldn't find a way to create a secondary y-axis in histogram.我找到了多种在绘图中创建辅助 y 轴的方法,但找不到在直方图中创建辅助 y 轴的方法。

Here is a sample code:这是一个示例代码:

a <- sample(90:110, 50, replace=TRUE)
b <- runif(50, min=0, max=1)
hist(a)
lines(b)

b is too small to show in hist(a) so is there any way that I can see both in the histogram? b 太小而无法在hist(a)显示,所以有什么方法可以在直方图中看到两者?

Technically a solution may be quite an identical to the approach proposed for the plots in this answer .从技术上讲,解决方案可能与针对此答案中的绘图提出的方法完全相同。 The idea is to use overlapping of two plots as proposed by @r2evans.这个想法是使用@r2evans 提出的两个图的重叠。

It makes sense to use color coding:使用颜色编码是有意义的:

# set color rules
col_a <- "red"
col_b <- "darkblue"
col_common <- "black"

Then let's draw the histogram and the plot:然后让我们绘制直方图和绘图:

# draw a histogram first
par(mar = c(5, 5, 5, 5) + 0.3)
hist(a, col = col_a, axes = FALSE, xlab = "", ylab = "", main = "")
# add both axes with the labels
axis(side = 1, xlim = seq(along.with = b), col = col_a, col.axis = col_a)
mtext(side = 1, text = "a_value", col = col_a, line = 2.5)
axis(side = 2, col = col_a, col.axis = col_a, ylab = "")
mtext(side = 2, text = "a_Frequency", col = col_a, line = 2.5)
# ... and add an overlaying plot
par(new=TRUE)
plot(b, ylim = c(0, 1), axes = FALSE, col = col_b, type = "l", xlab = "", ylab = "")
points(b, col = col_b, pch = 20, xlab = "", ylab = "")
axis(side = 3, xlim = seq(along.with = b), col = col_b, col.axis = col_b)
mtext(side = 3, text = "b_index", col = col_b, line = 2.5)
axis(side = 4, ylim = c(0, 1), col = col_b, col.axis = col_b)
mtext(side = 4, text = "b_value", col = col_b, line = 2.5)
box(col = col_common)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM