OppongY
OppongY

Reputation: 1

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.

Any ideas will be very welcome.

My script:

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()

Upvotes: 0

Views: 1639

Answers (1)

thelatemail
thelatemail

Reputation: 93833

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.

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)

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:

enter image description here

Upvotes: 2

Related Questions