我有下面的情节
par(mar=c(5.1,10.1,4.1,1.1))
plot(1,1, type="l", lwd=5, ylim=c(0,1), ylab="", xlab="Chi lab", lty=1, main="Overlay Plots", cex.lab=1.5, cex.axis=1.25, cex.main=1.6, bty="n", xlim=c(-1,4), yaxt="n", xaxt="n")
axis(1, line = 1, cex.lab=1.5, cex.axis=1.25 , at=0:4)
axis(2, line = 4, cex.lab=1.5, cex.axis=1.25 )
mtext("Psi lab", las=3, side = 2, line = 7, cex=1.5)
points(rnorm(100, 2, 1), rnorm(100,0.5,0.2))
在y轴和x轴的零点之间,我想覆盖下面的图
df<-c("III", "III", "I", "I", "I", "II", "I", "I", "I", "I", "I",
"II", "I", "I", "I", "I", "I", "I", "I", "II", "I", "III", "II",
"II", "III", "I", "II", "II", "I", "I", "IV", "I", "III", "I",
"III", "I", "I", "II", "I", "II", "II", "I", "II", "I", "II",
"II", "II", "II", "I", "I", "II", "I", "I", "I", "I", "I", "I",
"II", "II", "III", "I", "III", "I", "I", "I", "I", "II", "I",
"II", "III", "I", "I", "I", "I", "III", "II", "II", "I", "I",
"II", "I", "II", "III", "II", "III", "II", "III", "I", "III",
"III")
barplot(as.matrix(table(df)*100/90), col=c("white", "gray70", "gray40", "black"), ylim=c(0,100))
如何在R中完成?请仅以R解决方案为基础。
由于
答案 0 :(得分:2)
我对这个解决方案并不满意(有点棘手)。这个想法是:
par(new=TRUE)
重叠(我尝试使用add = TRUE选项但没有成功)这是我的代码:
## plot your first plot ....
## play with layout and margin to add barplot
par(mar=c(5.1,1,6,5))
nf <- layout(matrix(c(1,1,0,0), 2, 2), respect = TRUE)
par(new=TRUE)
barplot(as.matrix(table(df)*100/90), col=c("white", "gray70", "gray40", "black"),
ylim=c(0,100),axes=F)
对于我正在使用此矩阵的布局:
matrix(c(1,1,0,0), 2, 2)
[,1] [,2]
[1,] 1 0
[2,] 1 0
要看到它,您可以使用方便的layout.show
:
nf <- layout(matrix(c(1,1,0,0), 2, 2), respect = TRUE)
layout.show(nf)
答案 1 :(得分:2)
agstudy方法的变体,直接使用layout
表示整个情节。我已经省略了剧情的许多细节,只是为了说明两个并排:
m <- matrix(1:2,1,2)
layout(m,widths = c(0.25,0.75))
par(mar = c(5,4,4,0) + 0.1)
barplot(as.matrix(table(df)*100/90) / 100,
col=c("white", "gray70", "gray40", "black"),
ylim=c(0,1),
ylab = "Psi Lab")
par(mar = c(5,0,4,2) + 0.1)
plot(rnorm(100,2,1),rnorm(100,0.5,0.2),
xlab = "Chi Lab",ylab = "",
bty = "n",yaxt = "n")
为了保持宽高比,我将图像保存得比较高。使用两个图的布局的主要优点是我认为0级更好地对齐。
这使用简单的1行2列布局。对于第一个绘图,我删除了右边距,对于第二个绘图,我删除了左边距(默认填充除外)。显然,我不得不首先重新调整条形图数据,但这并不是非常困难。轴可以像以前一样定制。