Label specific points in a plot
Here I have two vectors: "Pr" and "Z"
here is my code:
Z=seq(1,10,by=0.5)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
I want to select only two points in the plot, namely those where Z=5, and Z=7.5 and label them as "Class A" and "Class B" respectively. How can I do that?
r plot label
add a comment |
Here I have two vectors: "Pr" and "Z"
here is my code:
Z=seq(1,10,by=0.5)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
I want to select only two points in the plot, namely those where Z=5, and Z=7.5 and label them as "Class A" and "Class B" respectively. How can I do that?
r plot label
can you put i what functionz is so we can reproduce this? Also I know ggplot has a way to label only specific things, and it looks really nice too
– RAB
Nov 23 '18 at 14:12
Or just skip the function entirely and make up some simple number: x 1-5, y: 1-5, label 2 and 4...
– Henrik
Nov 23 '18 at 14:17
add a comment |
Here I have two vectors: "Pr" and "Z"
here is my code:
Z=seq(1,10,by=0.5)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
I want to select only two points in the plot, namely those where Z=5, and Z=7.5 and label them as "Class A" and "Class B" respectively. How can I do that?
r plot label
Here I have two vectors: "Pr" and "Z"
here is my code:
Z=seq(1,10,by=0.5)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
I want to select only two points in the plot, namely those where Z=5, and Z=7.5 and label them as "Class A" and "Class B" respectively. How can I do that?
r plot label
r plot label
asked Nov 23 '18 at 14:08
JesusJesus
133
133
can you put i what functionz is so we can reproduce this? Also I know ggplot has a way to label only specific things, and it looks really nice too
– RAB
Nov 23 '18 at 14:12
Or just skip the function entirely and make up some simple number: x 1-5, y: 1-5, label 2 and 4...
– Henrik
Nov 23 '18 at 14:17
add a comment |
can you put i what functionz is so we can reproduce this? Also I know ggplot has a way to label only specific things, and it looks really nice too
– RAB
Nov 23 '18 at 14:12
Or just skip the function entirely and make up some simple number: x 1-5, y: 1-5, label 2 and 4...
– Henrik
Nov 23 '18 at 14:17
can you put i what functionz is so we can reproduce this? Also I know ggplot has a way to label only specific things, and it looks really nice too
– RAB
Nov 23 '18 at 14:12
can you put i what functionz is so we can reproduce this? Also I know ggplot has a way to label only specific things, and it looks really nice too
– RAB
Nov 23 '18 at 14:12
Or just skip the function entirely and make up some simple number: x 1-5, y: 1-5, label 2 and 4...
– Henrik
Nov 23 '18 at 14:17
Or just skip the function entirely and make up some simple number: x 1-5, y: 1-5, label 2 and 4...
– Henrik
Nov 23 '18 at 14:17
add a comment |
2 Answers
2
active
oldest
votes
Your example is not reproducible, so I made up some random data and plotted with ggplot:
Z=seq(1,10,by=0.5)
Pr <- 1:19
L <- c("Point1", rep(NA, 17), "Point19")
df <- data.frame(Pr, Z, L)
library(ggplot2)
ggplot(aes(x=Pr, y=Z, label=L), data=df) +
geom_point() +
geom_label() +
xlab("Attachment Point") +
ylab("Price") +
ggtitle("CAT Bond Price with increasing attachment points") +
theme_classic()
You just define which ones you want to show labels for in a separate labels column and ggplot does the rest.
In your case, when you define L just do
L <- ifelse(Z==7, "Class A", ifelse(Z==7.5, "Class B", NA))
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data
– RAB
Nov 23 '18 at 14:32
add a comment |
To stick with your base R plotting, you can use the text
function.
Since you do not provide your FunctionZ
I just use some example. I placed the labels to the left of the points. Depending on the shape of your function, you may wish to adjust the pos
argument to place them elsewhere.
Z=seq(1,10,by=0.5)
functionZ = function(x) x + sin(x)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",
xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
text(x=c(5,7.5), y=functionZ(c(5,7.5)), labels= c("Class A", "Class B"), pos=2)
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%2f53448194%2flabel-specific-points-in-a-plot%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Your example is not reproducible, so I made up some random data and plotted with ggplot:
Z=seq(1,10,by=0.5)
Pr <- 1:19
L <- c("Point1", rep(NA, 17), "Point19")
df <- data.frame(Pr, Z, L)
library(ggplot2)
ggplot(aes(x=Pr, y=Z, label=L), data=df) +
geom_point() +
geom_label() +
xlab("Attachment Point") +
ylab("Price") +
ggtitle("CAT Bond Price with increasing attachment points") +
theme_classic()
You just define which ones you want to show labels for in a separate labels column and ggplot does the rest.
In your case, when you define L just do
L <- ifelse(Z==7, "Class A", ifelse(Z==7.5, "Class B", NA))
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data
– RAB
Nov 23 '18 at 14:32
add a comment |
Your example is not reproducible, so I made up some random data and plotted with ggplot:
Z=seq(1,10,by=0.5)
Pr <- 1:19
L <- c("Point1", rep(NA, 17), "Point19")
df <- data.frame(Pr, Z, L)
library(ggplot2)
ggplot(aes(x=Pr, y=Z, label=L), data=df) +
geom_point() +
geom_label() +
xlab("Attachment Point") +
ylab("Price") +
ggtitle("CAT Bond Price with increasing attachment points") +
theme_classic()
You just define which ones you want to show labels for in a separate labels column and ggplot does the rest.
In your case, when you define L just do
L <- ifelse(Z==7, "Class A", ifelse(Z==7.5, "Class B", NA))
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data
– RAB
Nov 23 '18 at 14:32
add a comment |
Your example is not reproducible, so I made up some random data and plotted with ggplot:
Z=seq(1,10,by=0.5)
Pr <- 1:19
L <- c("Point1", rep(NA, 17), "Point19")
df <- data.frame(Pr, Z, L)
library(ggplot2)
ggplot(aes(x=Pr, y=Z, label=L), data=df) +
geom_point() +
geom_label() +
xlab("Attachment Point") +
ylab("Price") +
ggtitle("CAT Bond Price with increasing attachment points") +
theme_classic()
You just define which ones you want to show labels for in a separate labels column and ggplot does the rest.
In your case, when you define L just do
L <- ifelse(Z==7, "Class A", ifelse(Z==7.5, "Class B", NA))
Your example is not reproducible, so I made up some random data and plotted with ggplot:
Z=seq(1,10,by=0.5)
Pr <- 1:19
L <- c("Point1", rep(NA, 17), "Point19")
df <- data.frame(Pr, Z, L)
library(ggplot2)
ggplot(aes(x=Pr, y=Z, label=L), data=df) +
geom_point() +
geom_label() +
xlab("Attachment Point") +
ylab("Price") +
ggtitle("CAT Bond Price with increasing attachment points") +
theme_classic()
You just define which ones you want to show labels for in a separate labels column and ggplot does the rest.
In your case, when you define L just do
L <- ifelse(Z==7, "Class A", ifelse(Z==7.5, "Class B", NA))
edited Nov 23 '18 at 14:25
answered Nov 23 '18 at 14:19
RABRAB
783116
783116
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data
– RAB
Nov 23 '18 at 14:32
add a comment |
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data
– RAB
Nov 23 '18 at 14:32
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data– RAB
Nov 23 '18 at 14:32
geom_text
is another label adding option. Between the two there is a lot of extra options like moving the position of the label or customising the appearance of the label to suit your data– RAB
Nov 23 '18 at 14:32
add a comment |
To stick with your base R plotting, you can use the text
function.
Since you do not provide your FunctionZ
I just use some example. I placed the labels to the left of the points. Depending on the shape of your function, you may wish to adjust the pos
argument to place them elsewhere.
Z=seq(1,10,by=0.5)
functionZ = function(x) x + sin(x)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",
xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
text(x=c(5,7.5), y=functionZ(c(5,7.5)), labels= c("Class A", "Class B"), pos=2)
add a comment |
To stick with your base R plotting, you can use the text
function.
Since you do not provide your FunctionZ
I just use some example. I placed the labels to the left of the points. Depending on the shape of your function, you may wish to adjust the pos
argument to place them elsewhere.
Z=seq(1,10,by=0.5)
functionZ = function(x) x + sin(x)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",
xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
text(x=c(5,7.5), y=functionZ(c(5,7.5)), labels= c("Class A", "Class B"), pos=2)
add a comment |
To stick with your base R plotting, you can use the text
function.
Since you do not provide your FunctionZ
I just use some example. I placed the labels to the left of the points. Depending on the shape of your function, you may wish to adjust the pos
argument to place them elsewhere.
Z=seq(1,10,by=0.5)
functionZ = function(x) x + sin(x)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",
xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
text(x=c(5,7.5), y=functionZ(c(5,7.5)), labels= c("Class A", "Class B"), pos=2)
To stick with your base R plotting, you can use the text
function.
Since you do not provide your FunctionZ
I just use some example. I placed the labels to the left of the points. Depending on the shape of your function, you may wish to adjust the pos
argument to place them elsewhere.
Z=seq(1,10,by=0.5)
functionZ = function(x) x + sin(x)
Pr=lapply(Z,functionZ)
plot(Z,Pr,main="CAT Bond Price with increasing attachment points",
xlab="Attachment Point",ylab="Price")
grid(nx = NULL, ny = NULL, col = "blue", lty = "dotted")
text(x=c(5,7.5), y=functionZ(c(5,7.5)), labels= c("Class A", "Class B"), pos=2)
answered Nov 23 '18 at 15:01
G5WG5W
21.6k82041
21.6k82041
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%2f53448194%2flabel-specific-points-in-a-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
can you put i what functionz is so we can reproduce this? Also I know ggplot has a way to label only specific things, and it looks really nice too
– RAB
Nov 23 '18 at 14:12
Or just skip the function entirely and make up some simple number: x 1-5, y: 1-5, label 2 and 4...
– Henrik
Nov 23 '18 at 14:17