reducing the space between plotted points in plot( x, y) type=n

前端 未结 1 634
清酒与你
清酒与你 2021-01-27 14:06

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

1条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-27 14:46

    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

    0 讨论(0)
提交回复
热议问题