Label specific points in a plot












0















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?










share|improve this question























  • 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
















0















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?










share|improve this question























  • 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














0












0








0








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?










share|improve this question














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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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



















  • 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












2 Answers
2






active

oldest

votes


















1














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))





share|improve this answer


























  • 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



















1














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)


Labeled Plot






share|improve this answer























    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
    });


    }
    });














    draft saved

    draft discarded


















    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









    1














    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))





    share|improve this answer


























    • 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
















    1














    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))





    share|improve this answer


























    • 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














    1












    1








    1







    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))





    share|improve this answer















    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))






    share|improve this answer














    share|improve this answer



    share|improve this answer








    edited Nov 23 '18 at 14:25

























    answered Nov 23 '18 at 14:19









    RABRAB

    1,202317




    1,202317













    • 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





    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













    1














    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)


    Labeled Plot






    share|improve this answer




























      1














      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)


      Labeled Plot






      share|improve this answer


























        1












        1








        1







        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)


        Labeled Plot






        share|improve this answer













        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)


        Labeled Plot







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 23 '18 at 15:01









        G5WG5W

        22.4k92041




        22.4k92041






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Wiesbaden

            Marschland

            Dieringhausen