R base graphic: How to adjust gap size in the timeseries plot?
up vote
0
down vote
favorite
I saw this kind of questions about ggplot2 or other libraries, but cannot find one about the base graphics.
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.
r plot
add a comment |
up vote
0
down vote
favorite
I saw this kind of questions about ggplot2 or other libraries, but cannot find one about the base graphics.
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.
r plot
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: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info.
– r2evans
Nov 20 at 4:59
The default R line plot joins points unless they areNA
, 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 thetype="l"
argument as an initial check.
– thelatemail
Nov 20 at 5:07
I have seen a similar issue many years ago with this plot - stackoverflow.com/questions/11255210/… - which might be the same (or at least a similar) issue you are experiencing.
– thelatemail
Nov 20 at 5:39
I edited my question to give an short example code. Thank you.
– HoonTaek
Nov 20 at 6:24
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I saw this kind of questions about ggplot2 or other libraries, but cannot find one about the base graphics.
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.
r plot
I saw this kind of questions about ggplot2 or other libraries, but cannot find one about the base graphics.
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.
r plot
r plot
edited Nov 20 at 6:23
asked Nov 20 at 4:54
HoonTaek
156
156
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: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info.
– r2evans
Nov 20 at 4:59
The default R line plot joins points unless they areNA
, 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 thetype="l"
argument as an initial check.
– thelatemail
Nov 20 at 5:07
I have seen a similar issue many years ago with this plot - stackoverflow.com/questions/11255210/… - which might be the same (or at least a similar) issue you are experiencing.
– thelatemail
Nov 20 at 5:39
I edited my question to give an short example code. Thank you.
– HoonTaek
Nov 20 at 6:24
add a comment |
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: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info.
– r2evans
Nov 20 at 4:59
The default R line plot joins points unless they areNA
, 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 thetype="l"
argument as an initial check.
– thelatemail
Nov 20 at 5:07
I have seen a similar issue many years ago with this plot - stackoverflow.com/questions/11255210/… - which might be the same (or at least a similar) issue you are experiencing.
– thelatemail
Nov 20 at 5:39
I edited my question to give an short example code. Thank you.
– HoonTaek
Nov 20 at 6:24
4
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: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info.– r2evans
Nov 20 at 4:59
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: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info.– r2evans
Nov 20 at 4: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 at 5:07
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 at 5:07
I have seen a similar issue many years ago with this plot - stackoverflow.com/questions/11255210/… - which might be the same (or at least a similar) issue you are experiencing.
– thelatemail
Nov 20 at 5:39
I have seen a similar issue many years ago with this plot - stackoverflow.com/questions/11255210/… - which might be the same (or at least a similar) issue you are experiencing.
– thelatemail
Nov 20 at 5:39
I edited my question to give an short example code. Thank you.
– HoonTaek
Nov 20 at 6:24
I edited my question to give an short example code. Thank you.
– HoonTaek
Nov 20 at 6:24
add a comment |
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386450%2fr-base-graphic-how-to-adjust-gap-size-in-the-timeseries-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53386450%2fr-base-graphic-how-to-adjust-gap-size-in-the-timeseries-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
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: stackoverflow.com/questions/5963269, stackoverflow.com/help/mcve, and stackoverflow.com/tags/r/info.– r2evans
Nov 20 at 4: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 thetype="l"
argument as an initial check.– thelatemail
Nov 20 at 5:07
I have seen a similar issue many years ago with this plot - stackoverflow.com/questions/11255210/… - which might be the same (or at least a similar) issue you are experiencing.
– thelatemail
Nov 20 at 5:39
I edited my question to give an short example code. Thank you.
– HoonTaek
Nov 20 at 6:24