0

I tried to use the following code and the result is shown in the image:

enter image description here

How can I have a y axis of the upper graph with the same shape as the one below?

The scrip I used is the following:

   count<-matrix(ESTRATTORE$VALORI, nrow=5,ncol=3)

    lower=c(0,4500)
    upper=c(7500,9000)
    y_outer=16.5
    lowspan=c(0,11)
    topspan=c(lowspan[2]+1,16.5)
    ylabel="Time (sec)"
    cnvrt.coords <-function(x,y=NULL){

      xy <- xy.coords(x,y, recycle=TRUE)
      cusr <- par('usr')
      cplt <- par('plt')    
      plt <- list()
      plt$x <- (xy$x-cusr[1])/(cusr[2]-cusr[1])
      plt$y <- (xy$y-cusr[3])/(cusr[4]-cusr[3])
      fig <- list()
      fig$x <- plt$x*(cplt[2]-cplt[1])+cplt[1]
      fig$y <- plt$y*(cplt[4]-cplt[3])+cplt[3]
      return( list(fig=fig) )
    }

    subplot <- function(fun, x, y=NULL){

      old.par <- par(no.readonly=TRUE)
      on.exit(par(old.par))
      xy <- xy.coords(x,y)
      xy <- cnvrt.coords(xy)$fig
      par(plt=c(xy$x,xy$y), new=TRUE)
      fun
      tmp.par <- par(no.readonly=TRUE)
      return(invisible(tmp.par))
    }


    plot(c(0,1),c(0,y_outer),type='n',axes=FALSE,ylab=ylabel,xlab='')

    subplot(barplot(as.matrix(count),col=terrain.colors(5),ylim=lower,xpd=FALSE,las=1,axis.lty=1,names.arg = c("H", "M", "L")),x=c(0,1),y=lowspan)


    subplot(barplot(as.matrix(count),col=terrain.colors(5),ylim=upper,xpd=FALSE,xaxt='n',axes=T,las=2), x=c(0,1),y=topspan)

    lowertop=lowspan[2]+0.1     # Where to end the lower axis
    breakheight=0.5   # Height of the break
    upperbot=lowertop+breakheight # Where to start the upper axes
    markerheight=0.4 # Heightdifference for the break markers
    markerwidth=.04  # With of the break markers

    lines(c(0,0),c(1,lowertop))
    lines(c(markerwidth/-2,markerwidth/2),c(lowertop-markerheight/2,lowertop+markerheight/2))
    lines(c(0,0),c(upperbot,14))
    lines(c(markerwidth/-2,markerwidth/2),c(upperbot-markerheight/2,upperbot+markerheight/2))
Tung
  • 26,371
  • 7
  • 91
  • 115
  • 2
    Take a look at the "plotrix" package, that should provide the options you are looking for. – Dave2e Jan 31 '20 at 13:53
  • See also this answer https://stackoverflow.com/questions/19612348/break-x-axis-in-r – Tung Jan 31 '20 at 14:14
  • Someone mentioned `facet_zoom()` from the `ggforce` package here https://stackoverflow.com/questions/7194688/using-ggplot2-can-i-insert-a-break-in-the-axis/56000944#56000944 – Tung Jan 31 '20 at 14:22

0 Answers0