减少 plot( x, y) type=n 中绘制点之间的空间 [英] reducing the space between plotted points in plot( x, y) type=n
问题描述
我想减少我制作的一些图之间的间距.我没有通过搜索工具获得问题的具体答案.
I want to reduce the spacing between some plots that I made. I didn't get specific answers to my problem via the search facility.
我想要的只是将绘制点之间的水平距离减半.
All I want is to halve the horizontal distances between the plotted points.
欢迎提出任何想法.
我的脚本:
x=c(1,1,1)
x2=c(1.1,1.1,1.1)
x1=c(1.2,1.2,1.2)
x3=c(1.3,1.3,1.3)
b=c(12.142,12.076,12.208)
d=c(12.8,12.7,12.9)
g=c(12.1,12.0,12.2)
p=c(12.669, 12.528,12.811)
ptidx = seq(1,12,by=3)
lciidx = seq(2,12,by=3)
uciidx = seq(3,12,by=3)
plot(x,b,type="n",font.lab=2,axes=FALSE,main="ADJ MEAN",xlab=" ",
ylab="Mean, g/dl (95% CI)",
ylim=c(11.8,13.0),xlim=c(1,1.3))
axis(1, at=c(1,2,3,4))
axis(2, at=seq(11.8,13.0,by=0.4))
points(x[ptidx],b[ptidx],pch=19,cex=5,col="red")
points(x[lciidx],b[lciidx],pch="_",cex=4,col="red")
points(x[uciidx],b[uciidx],pch="_",cex=4,col="red")
points(x2[ptidx],d[ptidx],pch=15,cex=5,col="blue")
points(x2[lciidx],d[lciidx],pch="_",cex=4,col="blue")
points(x2[uciidx],d[uciidx],pch="_",cex=4,col="blue")
points(x1[ptidx],g[ptidx],pch=19,cex=5,col="red")
points(x1[lciidx],g[lciidx],cex=4,pch="_",col="red")
points(x1[uciidx],g[uciidx],cex=4,pch="_",col="red")
points(x3[ptidx],p[ptidx],cex=5,pch=15,col="blue")
points(x3[lciidx],p[lciidx],cex=4,pch="_",col="blue")
points(x3[uciidx],p[uciidx],cex=4,pch="_",col="blue")
for(i in 1:4)
{ lines(c(x[lciidx[i]],x[uciidx[i]]),c(b[lciidx[i]],b[uciidx[i]]),lwd=6,cex=4,col="red")
lines(c(x2[lciidx[i]],x2[uciidx[i]]),c(d[lciidx[i]],d[uciidx[i]]),lwd=6,cex=4,col="blue")
lines(c(x1[lciidx[i]],x1[uciidx[i]]),c(g[lciidx[i]],g[uciidx[i]]),lwd=6,cex=4,col="red")
lines(c(x3[lciidx[i]],x3[uciidx[i]]),c(p[lciidx[i]],p[uciidx[i]]),lwd=6,cex=4,col="blue")
}
box()
推荐答案
除了您的主要问题,这可能是我见过的最复杂的绘图程序.为了简化事情,我会认真考虑首先将您的数据收集到 data.frame 中,如下所示:
Aside from your main question, this is possibly the most convoluted plotting procedure I've ever seen. To simplify things, I would seriously consider collecting your data into a data.frame first like so:
dat <- setNames(data.frame(rbind(b,d,g,p)),c("value","low","high"))
dat
# value low high
#b 12.142 12.076 12.208
#d 12.800 12.700 12.900
#g 12.100 12.000 12.200
#p 12.669 12.528 12.811
转到主查询 - 如果您想将这些点推"到一起,您可能有几个选择.要么缩小绘图本身,要么将左右边距推入.
On to the main query - if you want to "push" the points together, you probably have a couple of options. Either make the plot itself smaller, or push the left and right margins in.
要推入边距,请通过调用 ?par
来设置它们,例如:
To push the margins in, set them with a call to ?par
like:
# in order - margins for bottom,left,top,right
# for reference, the defaults are c(5.1,4.1,4.1,2.1)
par(mar=c(5.1,9,4.1,9))
否则,只需指定一个具有所需宽度和高度的较小绘图窗口即可压缩图形:
Otherwise, just specify a smaller plotting window with your desired width and height which will compress the graph:
dev.new(width=5,height=4)
然后可以使用本文开头生成的 data.frame 大大简化您的代码.例如:
Your code could then be simplified greatly using the data.frame generated at the beginning of this post. E.g.:
# set some constants
# nominal locations of points on x-axis
xpts <- 1:nrow(dat)
# 95CI bar width
bar <- 0.05
# colour scheme
palette(c("red","blue"))
# make the base plot
plot(xpts, dat$value, ylim=c(min(dat),max(dat)),
col=1:2, pch=19, cex=2, xaxt="n")
# add the axis back with proper labels
axis(1,at=xpts,labels=rownames(dat))
# add the 95% CI bars and lines
segments(xpts,dat$low,xpts,dat$high,col=1:2,lwd=2)
segments(xpts-bar,dat$low,xpts+bar,dat$low,col=1:2,lwd=2)
segments(xpts-bar,dat$high,xpts+bar,dat$high,col=1:2,lwd=2)
使用边距压缩方法时如下所示:
Which looks like the below, when using the margin squashing method:
这篇关于减少 plot( x, y) type=n 中绘制点之间的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!