61686

Align grid with ticks

Question:

When I let <a href="https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/plot.html" rel="nofollow">plot</a> draw the axis, <a href="https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/grid.html" rel="nofollow">grid</a> aligns with ticks <a href="https://stackoverflow.com/q/8081931/850781" rel="nofollow">by default</a>.

However, my plot is somewhat involved:

mytime <- as.POSIXct("2015-08-20") + seq(0,by=3600,length.out=7*24) plot(x=mytime,y=rnorm(7*24),xaxt="n") ticks <- mytime[seq(1,by=12,to=length(mytime))] axis(1, ticks, strftime(ticks, "%a %H:%M")) grid(ny=NULL,nx=14)

and I cannot get the grid to align with the ticks:

<a href="https://i.stack.imgur.com/Ytxbc.png" rel="nofollow"><img alt="grid and ticks do not align" class="b-lazy" data-src="https://i.stack.imgur.com/Ytxbc.png" data-original="https://i.stack.imgur.com/Ytxbc.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

How do I make them align?

Answer1:

Looks like <a href="https://stat.ethz.ch/R-manual/R-devel/library/graphics/html/abline.html" rel="nofollow">abline</a> is the answer:

mytime <- as.POSIXct("2015-08-20") + seq(0,by=3600,length.out=7*24) plot(x=mytime,y=rnorm(7*24),xaxt="n") ticks <- c(mytime[seq(1,by=12,to=length(mytime))],mytime[1]+7*24*3600) axis(1, ticks, strftime(ticks, "%a %H:%M")) grid(ny=NULL,nx=NA) abline(v=ticks[seq(1,length(ticks),2)],lty="dotted",col="lightgray")

<a href="https://i.stack.imgur.com/mQA1U.png" rel="nofollow"><img alt="enter image description here" class="b-lazy" data-src="https://i.stack.imgur.com/mQA1U.png" data-original="https://i.stack.imgur.com/mQA1U.png" src="https://etrip.eimg.top/images/2019/05/07/timg.gif" /></a>

Recommend

  • .js help with the Date object and if/else
  • how to search between 2 dates and times in access?
  • Time zone PHP refresh
  • Timezone issue when filtering XTS using .indexhour
  • custom axis labels plotting a forecast in R
  • AWS SES Production Access setup with Meteor
  • Plot a decision tree with R
  • How to increase the python speed over loops?
  • Matplotlib: Custom colormap with three colors [duplicate]
  • Singular Value Decomposition Implementation
  • Count the number of non-null values in a Spark DataFrame
  • How to deal with SpiderWebPlot in JFreeChart?
  • Keep same scale in different graphs ggplot2
  • Converting float[,] to list in f#?
  • How to customize whisker lines on a geom_box plot differently than the lines of the box itself
  • how to automatically enter password when using ssh?
  • JFreechart X and Y axis scaling
  • Extract data between rows r
  • WPF Image control clipping
  • phpmailer - How to verify a sent email arrived at its destination
  • date format change with DT and shiny
  • Geom_jitter colour based on values
  • How to plot large time series (thousands of administration times/doses of a medication)?
  • How to create a 2D image by rotating 1D vector of numbers around its center element?
  • Breaking out column by groups in Pandas
  • Moving Android View and preventing onDraw to be called over and over again
  • Blackberry - Custom EditField Cursor
  • Update CALayer sublayers immediately
  • Why HTML5 Canvas with a larger size stretch a drawn line?
  • Spray.io: When (not) to use non-blocking route handling?
  • Modifying destination and filename of gulp-svg-sprite
  • GridView Sorting works once only
  • retrieve vertices with no linked edge in arangodb
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • Change div Background jquery
  • apache spark aggregate function using min value
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • UserPrincipal.Current returns apppool on IIS
  • java string with new operator and a literal