内部边距为0的复杂图

时间:2014-08-25 20:52:52

标签: r

附加代码用于三个区域的绘图设置 - 一个大区域,然后是侧面两个较小的直方图。直方图正确地与主要绘图区域对接,但我需要它们也相互接触,与虚线对齐。 (即,红色和绿色条应该触摸。)如何修改此代码来执行此操作?

def.par <- par(no.readonly = TRUE)

rawData = rnorm(n=1000, mean=0.002, sd=0.05)
d1 = cumprod(1 + rawData)
d2 = hist(rawData[rawData  > 0], breaks=100, plot=FALSE)
d3 = hist(rawData[rawData <= 0], breaks=100, plot=FALSE)

top <- max(c(d2$counts, d3$counts))

e1 = c(1,1,1,1,1,1,2)
e2 = c(1,1,1,1,1,1,3)

m1 = matrix(c(e1,e1,e1,e2,e2,e2), nrow=6, ncol=7, byrow = TRUE)

lay1 = layout(m1, respect = TRUE)
# margin size of overall page
marSize = 0.3
par(omi=c(marSize, marSize, marSize, marSize))
#Main chart - no right margin
par(mar=c(1,1,1,0))
plot(d1, type="l")
tmp1 = mean(c(max(d1), min(d1)))
abline(h=tmp1, lty=3)

#Upper histogram, no left or bottom margin
par(mar=c(0,0,1,1))
barplot(d2$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE, col="green")

#Lower histogram, no left or upper margin
par(mar=c(1,0,0,1))
barplot(d3$counts, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE, col="red")

par(def.par)  #- reset to default

1 个答案:

答案 0 :(得分:0)

好的,这是一个解决方案:

##-- Create a scatterplot with marginal histograms -----
def.par <- par(no.readonly = TRUE)

rawData = rnorm(n=1000, mean=0.002, sd=0.05)
d1 = cumprod(1 + rawData)
d2 = hist(rawData[rawData  > 0], breaks=100, plot=FALSE)
d3 = hist(rawData[rawData <= 0], breaks=100, plot=FALSE)

top <- max(c(d2$counts, d3$counts))

e1 = c(1,1,1,1,1,1,2)

m1 = matrix(c(e1,e1,e1,e1,e1,e1), nrow=6, ncol=7, byrow = TRUE)

lay1 = layout(m1, respect = TRUE)
# margin size of overall page
marSize = 0.3
par(omi=c(marSize, marSize, marSize, marSize))
#Main chart - no right margin
par(mar=c(1,1,1,0))

plot(d1, type="l")
tmp1 = mean(c(max(d1), min(d1)))
abline(h=tmp1, lty=3)

L2 = length(d2$counts)
L3 = length(d3$counts)
Lmax = max(L2, L3)
tmpD2 = c(rep(0,Lmax), d2$counts, rep(0,Lmax-L2))
tmpD3 = c(rep(0, Lmax-L3), d3$counts, rep(0, Lmax))

par(mar=c(1,0,1,1))

barplot(tmpD2, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE, col="green")
barplot(tmpD3, axes = FALSE, xlim = c(0, top), space = 0, horiz = TRUE, col="red", add=TRUE)