0

I saw this kind of questions about ggplot2 or other libraries, but cannot find one about the base graphics.

enter image description here

The data used for the plot does not have blank rows with NAs,

but they appear in the plot automatically.

1) How can I remove the blank space in the plot? 2) Can I manually adjust the gap size in the plot?

** edit

** I provide an example code with smaller # of data, but it can show you the exact problem.

x = c(seq.POSIXt(from = as.POSIXct("2018-07-27 00:00:00", format = "%Y-%m-%d 
%H:%M:%S"),
             to = as.POSIXct("2018-07-27 10:00:00", format = "%Y-%m-%d 
%H:%M:%S"),
             by = "1 hour"),
  seq.POSIXt(from = as.POSIXct("2018-09-01 00:00:00", format = "%Y-%m-%d 
%H:%M:%S"),
             to = as.POSIXct("2018-09-01 08:00:00", format = "%Y-%m-%d 
%H:%M:%S"),
             by = "1 hour"))

y1 = 20 + rnorm(20)
y2 = 10 + rnorm(20)
y3 = 5 + rnorm(20)
date0601 = as.POSIXct("2018-06-01", format = "%Y-%m-%d")
date0930 = as.POSIXct("2018-09-30", format = "%Y-%m-%d")
dates = seq.POSIXt(from = date0601, to = date0930, by = "15 days")
lab_dates = strftime(dates, format = "%m-%d")

par(mfrow = c(1, 1),
mar = c(5.1, 5.1, 2.1, 5.1))
plot(x, y1,
 axes = FALSE, xaxs = "i",yaxs = "i",
 xlab = "Date", ylab = "Moisture Content (%)", ylim = c(0, 35),
 type = "l", lwd = 2, col = "black", cex.lab = 2.0, cex.axis = 2.0, mgp = 
c(3, 0, 0))
points(x, y2, type = "l", lwd = 2, col = "red")
bot = c(x[11], x[12])
top = c(10, 28)
rect(bot[1], top[1], bot[2], top[2], border = "white",
 col = "white") # rectangular to mask gap interval

axis(1, at = dates, labels = lab_dates, 
 cex.axis = 2.0, mgp = c(0, 1.0, 0.0))
axis(2, at = seq(0, 35, 10), labels = seq(0, 35, 10),
 cex.axis = 2.0, mgp = c(10, 0.5, 0.0))

par(new = TRUE)
plot(x, y3, type = "h", lwd = 2, col = "blue", ylim = c(0, 40),
 axes = FALSE, xaxs = "i", yaxs = "i", xlab = "", ylab = "")
axis(4, at = seq(0, 40, 10), labels = seq(0, 40, 10),
 cex.axis = 2.0, mgp = c(10, 1.0, 0.0))

note that four lines from "bot = c~~" are for masking the gap in the plot.

Without the code, a line for each dataset will appear in the gap in the plot.

hlee
  • 195
  • 2
  • 12
  • 4
    Without seeing the data, I can't help but disagree and say that there is "obviously" a gap somehow in the data. If you make this question reproducible, we might be able to help. This includes sample code (including listing non-base R packages) and sample data (e.g., `dput(head(x))`). Refs: https://stackoverflow.com/questions/5963269, https://stackoverflow.com/help/mcve, and https://stackoverflow.com/tags/r/info. – r2evans Nov 20 '18 at 04:59
  • The default R line plot joins points unless they are `NA`, so I too cast some doubt on this output. `plot(x=c(1:4,10:12), y=1:7, type="l")` for example . Are you just plotting many points rather than a line? Try adding the `type="l"` argument as an initial check. – thelatemail Nov 20 '18 at 05:07
  • I have seen a similar issue many years ago with this plot - https://stackoverflow.com/questions/11255210/reason-for-strange-gap-in-the-plot/11255913 - which might be the same (or at least a similar) issue you are experiencing. – thelatemail Nov 20 '18 at 05:39
  • I edited my question to give an short example code. Thank you. – hlee Nov 20 '18 at 06:24

0 Answers0