Add straight line and text on a hexbinplot
I want to add a standard 1:1 line (with intercept 0 and slope 1) and some text (for example R-square) on my hexbinplot. The trial codes could be something like
require(hexbin)
x1 <- rnorm(10000)
x2 <- rnorm(10000)
df <- data.frame(cbind(x1, x2))
hex <- hexbin(x1, x2, xbins = 300)
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5))
hexVP.abline(hexViewport(hex), 0, 1)
This gives me the plot below

The line added have two issues which are
- It should be from the lower left corner to the top right corner,but the slope (should be 1) and intercept (should be 0) looks change when I zoom in/out the graphic window in RStudio and
- the both ends of the line does not extend to the plot border
Another question is about how to add a text on the plot.
The ideal plot could be looks like

r plot
add a comment |
I want to add a standard 1:1 line (with intercept 0 and slope 1) and some text (for example R-square) on my hexbinplot. The trial codes could be something like
require(hexbin)
x1 <- rnorm(10000)
x2 <- rnorm(10000)
df <- data.frame(cbind(x1, x2))
hex <- hexbin(x1, x2, xbins = 300)
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5))
hexVP.abline(hexViewport(hex), 0, 1)
This gives me the plot below

The line added have two issues which are
- It should be from the lower left corner to the top right corner,but the slope (should be 1) and intercept (should be 0) looks change when I zoom in/out the graphic window in RStudio and
- the both ends of the line does not extend to the plot border
Another question is about how to add a text on the plot.
The ideal plot could be looks like

r plot
add a comment |
I want to add a standard 1:1 line (with intercept 0 and slope 1) and some text (for example R-square) on my hexbinplot. The trial codes could be something like
require(hexbin)
x1 <- rnorm(10000)
x2 <- rnorm(10000)
df <- data.frame(cbind(x1, x2))
hex <- hexbin(x1, x2, xbins = 300)
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5))
hexVP.abline(hexViewport(hex), 0, 1)
This gives me the plot below

The line added have two issues which are
- It should be from the lower left corner to the top right corner,but the slope (should be 1) and intercept (should be 0) looks change when I zoom in/out the graphic window in RStudio and
- the both ends of the line does not extend to the plot border
Another question is about how to add a text on the plot.
The ideal plot could be looks like

r plot
I want to add a standard 1:1 line (with intercept 0 and slope 1) and some text (for example R-square) on my hexbinplot. The trial codes could be something like
require(hexbin)
x1 <- rnorm(10000)
x2 <- rnorm(10000)
df <- data.frame(cbind(x1, x2))
hex <- hexbin(x1, x2, xbins = 300)
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5))
hexVP.abline(hexViewport(hex), 0, 1)
This gives me the plot below

The line added have two issues which are
- It should be from the lower left corner to the top right corner,but the slope (should be 1) and intercept (should be 0) looks change when I zoom in/out the graphic window in RStudio and
- the both ends of the line does not extend to the plot border
Another question is about how to add a text on the plot.
The ideal plot could be looks like

r plot
r plot
edited Nov 28 '18 at 8:44
zhaojiacheng
asked Nov 28 '18 at 8:22
zhaojiachengzhaojiacheng
358
358
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
Cute that a package still uses lattice for graphics. That's really retro!
Here is the lattice way:
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5),
panel = function(x, y, ...) {
panel.hexbinplot(x, y, ...)
lattice::panel.abline(a = 0, b = 1)
})

(Edit: After you added additional requirements: Use panel.text to add text to a lattice plot.)
Personally, I'd use ggplot2 and its geom_hex:
library(ggplot2)
ggplot(df, aes(x = x1, y = x2)) +
geom_hex(bins = 300) +
xlim(-5, 5) + ylim(-5, 5) +
geom_abline(intercept = 0, slope = 1)
add a comment |
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%2f53515004%2fadd-straight-line-and-text-on-a-hexbinplot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Cute that a package still uses lattice for graphics. That's really retro!
Here is the lattice way:
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5),
panel = function(x, y, ...) {
panel.hexbinplot(x, y, ...)
lattice::panel.abline(a = 0, b = 1)
})

(Edit: After you added additional requirements: Use panel.text to add text to a lattice plot.)
Personally, I'd use ggplot2 and its geom_hex:
library(ggplot2)
ggplot(df, aes(x = x1, y = x2)) +
geom_hex(bins = 300) +
xlim(-5, 5) + ylim(-5, 5) +
geom_abline(intercept = 0, slope = 1)
add a comment |
Cute that a package still uses lattice for graphics. That's really retro!
Here is the lattice way:
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5),
panel = function(x, y, ...) {
panel.hexbinplot(x, y, ...)
lattice::panel.abline(a = 0, b = 1)
})

(Edit: After you added additional requirements: Use panel.text to add text to a lattice plot.)
Personally, I'd use ggplot2 and its geom_hex:
library(ggplot2)
ggplot(df, aes(x = x1, y = x2)) +
geom_hex(bins = 300) +
xlim(-5, 5) + ylim(-5, 5) +
geom_abline(intercept = 0, slope = 1)
add a comment |
Cute that a package still uses lattice for graphics. That's really retro!
Here is the lattice way:
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5),
panel = function(x, y, ...) {
panel.hexbinplot(x, y, ...)
lattice::panel.abline(a = 0, b = 1)
})

(Edit: After you added additional requirements: Use panel.text to add text to a lattice plot.)
Personally, I'd use ggplot2 and its geom_hex:
library(ggplot2)
ggplot(df, aes(x = x1, y = x2)) +
geom_hex(bins = 300) +
xlim(-5, 5) + ylim(-5, 5) +
geom_abline(intercept = 0, slope = 1)
Cute that a package still uses lattice for graphics. That's really retro!
Here is the lattice way:
hexbinplot(x2 ~ x1, data = df, aspect = '1', xbins = 300, xlim = c(-5, 5), ylim = c(-5, 5),
panel = function(x, y, ...) {
panel.hexbinplot(x, y, ...)
lattice::panel.abline(a = 0, b = 1)
})

(Edit: After you added additional requirements: Use panel.text to add text to a lattice plot.)
Personally, I'd use ggplot2 and its geom_hex:
library(ggplot2)
ggplot(df, aes(x = x1, y = x2)) +
geom_hex(bins = 300) +
xlim(-5, 5) + ylim(-5, 5) +
geom_abline(intercept = 0, slope = 1)
edited Nov 28 '18 at 8:50
answered Nov 28 '18 at 8:41
RolandRoland
101k6113190
101k6113190
add a comment |
add a comment |
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.
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%2f53515004%2fadd-straight-line-and-text-on-a-hexbinplot%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