Changing legend labels without changing legend markers or quantity










0















I am using ggplot2 to create a figure where the data plotted for different groups varies in both its linetype and color. I am trying to change the legend labels, however, each time I try to change the labels I am only able to change the labels for one aesthetic (i.e. color or linetype) and this produces two legends. Is there a way to change the legend labels when two aesthetics are mapped to the same variable?



Here is my working data:



# Generate working data
mydat <- structure(list(message_ineq_high_dum = c(0,0,1,1),
human_message_dum = c(1,0,1,0),
fit = c(65,60,76,66),
lwr = c(62,58,74,63),
upr = c(68,63,79,69),
var2 = structure(c(1L,1L, 2L, 2L),
.Label = c("Low", "High"),
class = c("ordered","factor")),
var1 = structure(c(2L, 1L, 2L, 1L),
.Label = c("Low","High"),
class = c("ordered", "factor"))),
.Names = c("message_ineq_high_dum","human_message_dum",
"fit", "lwr", "upr", "var2",
"var1"),
class = "data.frame",
row.names = c(NA, -4L))


Let's try to plot the data. The legend labels are "High" and "Low" in this plot.



# Plot: incorrect legend labels
library(ggplot2)

ggplot2::ggplot(data = mydat,
aes(x = var2,
y = fit,
group = var1)) +
theme_minimal() +
geom_point(aes(color = var1)) +
geom_line(aes(color = var1, linetype = var1)) +

geom_errorbar(aes(ymin = lwr,
ymax = upr,
width = 0.1,
color = var1,
linetype = var1)) +
labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
theme(panel.grid.minor = element_blank()) +
scale_color_manual(values = c("Low" = "black", "High" = "red")) +
theme(legend.position="bottom", legend.title = element_blank())


I am trying to change the legend labels from "High" and "Low" to "Foo" and "Bar" without changing anything else. Every approach I have tried changes the legend labels while also changing either (a) the number of legends and/or (b) the line type/color corresponding to the legend label.










share|improve this question






















  • Add a scale_linetype_discrete and use the labels argument there and in scale_color. Related: How do I manually change the key labels in a legend in ggplot2

    – Henrik
    Nov 13 '18 at 7:29
















0















I am using ggplot2 to create a figure where the data plotted for different groups varies in both its linetype and color. I am trying to change the legend labels, however, each time I try to change the labels I am only able to change the labels for one aesthetic (i.e. color or linetype) and this produces two legends. Is there a way to change the legend labels when two aesthetics are mapped to the same variable?



Here is my working data:



# Generate working data
mydat <- structure(list(message_ineq_high_dum = c(0,0,1,1),
human_message_dum = c(1,0,1,0),
fit = c(65,60,76,66),
lwr = c(62,58,74,63),
upr = c(68,63,79,69),
var2 = structure(c(1L,1L, 2L, 2L),
.Label = c("Low", "High"),
class = c("ordered","factor")),
var1 = structure(c(2L, 1L, 2L, 1L),
.Label = c("Low","High"),
class = c("ordered", "factor"))),
.Names = c("message_ineq_high_dum","human_message_dum",
"fit", "lwr", "upr", "var2",
"var1"),
class = "data.frame",
row.names = c(NA, -4L))


Let's try to plot the data. The legend labels are "High" and "Low" in this plot.



# Plot: incorrect legend labels
library(ggplot2)

ggplot2::ggplot(data = mydat,
aes(x = var2,
y = fit,
group = var1)) +
theme_minimal() +
geom_point(aes(color = var1)) +
geom_line(aes(color = var1, linetype = var1)) +

geom_errorbar(aes(ymin = lwr,
ymax = upr,
width = 0.1,
color = var1,
linetype = var1)) +
labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
theme(panel.grid.minor = element_blank()) +
scale_color_manual(values = c("Low" = "black", "High" = "red")) +
theme(legend.position="bottom", legend.title = element_blank())


I am trying to change the legend labels from "High" and "Low" to "Foo" and "Bar" without changing anything else. Every approach I have tried changes the legend labels while also changing either (a) the number of legends and/or (b) the line type/color corresponding to the legend label.










share|improve this question






















  • Add a scale_linetype_discrete and use the labels argument there and in scale_color. Related: How do I manually change the key labels in a legend in ggplot2

    – Henrik
    Nov 13 '18 at 7:29














0












0








0








I am using ggplot2 to create a figure where the data plotted for different groups varies in both its linetype and color. I am trying to change the legend labels, however, each time I try to change the labels I am only able to change the labels for one aesthetic (i.e. color or linetype) and this produces two legends. Is there a way to change the legend labels when two aesthetics are mapped to the same variable?



Here is my working data:



# Generate working data
mydat <- structure(list(message_ineq_high_dum = c(0,0,1,1),
human_message_dum = c(1,0,1,0),
fit = c(65,60,76,66),
lwr = c(62,58,74,63),
upr = c(68,63,79,69),
var2 = structure(c(1L,1L, 2L, 2L),
.Label = c("Low", "High"),
class = c("ordered","factor")),
var1 = structure(c(2L, 1L, 2L, 1L),
.Label = c("Low","High"),
class = c("ordered", "factor"))),
.Names = c("message_ineq_high_dum","human_message_dum",
"fit", "lwr", "upr", "var2",
"var1"),
class = "data.frame",
row.names = c(NA, -4L))


Let's try to plot the data. The legend labels are "High" and "Low" in this plot.



# Plot: incorrect legend labels
library(ggplot2)

ggplot2::ggplot(data = mydat,
aes(x = var2,
y = fit,
group = var1)) +
theme_minimal() +
geom_point(aes(color = var1)) +
geom_line(aes(color = var1, linetype = var1)) +

geom_errorbar(aes(ymin = lwr,
ymax = upr,
width = 0.1,
color = var1,
linetype = var1)) +
labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
theme(panel.grid.minor = element_blank()) +
scale_color_manual(values = c("Low" = "black", "High" = "red")) +
theme(legend.position="bottom", legend.title = element_blank())


I am trying to change the legend labels from "High" and "Low" to "Foo" and "Bar" without changing anything else. Every approach I have tried changes the legend labels while also changing either (a) the number of legends and/or (b) the line type/color corresponding to the legend label.










share|improve this question














I am using ggplot2 to create a figure where the data plotted for different groups varies in both its linetype and color. I am trying to change the legend labels, however, each time I try to change the labels I am only able to change the labels for one aesthetic (i.e. color or linetype) and this produces two legends. Is there a way to change the legend labels when two aesthetics are mapped to the same variable?



Here is my working data:



# Generate working data
mydat <- structure(list(message_ineq_high_dum = c(0,0,1,1),
human_message_dum = c(1,0,1,0),
fit = c(65,60,76,66),
lwr = c(62,58,74,63),
upr = c(68,63,79,69),
var2 = structure(c(1L,1L, 2L, 2L),
.Label = c("Low", "High"),
class = c("ordered","factor")),
var1 = structure(c(2L, 1L, 2L, 1L),
.Label = c("Low","High"),
class = c("ordered", "factor"))),
.Names = c("message_ineq_high_dum","human_message_dum",
"fit", "lwr", "upr", "var2",
"var1"),
class = "data.frame",
row.names = c(NA, -4L))


Let's try to plot the data. The legend labels are "High" and "Low" in this plot.



# Plot: incorrect legend labels
library(ggplot2)

ggplot2::ggplot(data = mydat,
aes(x = var2,
y = fit,
group = var1)) +
theme_minimal() +
geom_point(aes(color = var1)) +
geom_line(aes(color = var1, linetype = var1)) +

geom_errorbar(aes(ymin = lwr,
ymax = upr,
width = 0.1,
color = var1,
linetype = var1)) +
labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
theme(panel.grid.minor = element_blank()) +
scale_color_manual(values = c("Low" = "black", "High" = "red")) +
theme(legend.position="bottom", legend.title = element_blank())


I am trying to change the legend labels from "High" and "Low" to "Foo" and "Bar" without changing anything else. Every approach I have tried changes the legend labels while also changing either (a) the number of legends and/or (b) the line type/color corresponding to the legend label.







r ggplot2 plot






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 13 '18 at 7:16









user3614648user3614648

531619




531619












  • Add a scale_linetype_discrete and use the labels argument there and in scale_color. Related: How do I manually change the key labels in a legend in ggplot2

    – Henrik
    Nov 13 '18 at 7:29


















  • Add a scale_linetype_discrete and use the labels argument there and in scale_color. Related: How do I manually change the key labels in a legend in ggplot2

    – Henrik
    Nov 13 '18 at 7:29

















Add a scale_linetype_discrete and use the labels argument there and in scale_color. Related: How do I manually change the key labels in a legend in ggplot2

– Henrik
Nov 13 '18 at 7:29






Add a scale_linetype_discrete and use the labels argument there and in scale_color. Related: How do I manually change the key labels in a legend in ggplot2

– Henrik
Nov 13 '18 at 7:29













1 Answer
1






active

oldest

votes


















0














You van use scale_linetype_manual, but don't forget to set the same labels!



library(ggplot2)

ggplot2::ggplot(data = mydat,
aes(x = var2,
y = fit,
group = var1)) +
theme_minimal() +
geom_point(aes(color = var1)) +
geom_line(aes(color = var1, linetype = var1)) +

geom_errorbar(aes(ymin = lwr,
ymax = upr,
width = 0.1,
color = var1,
linetype = var1)) +
labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
theme(panel.grid.minor = element_blank()) +
scale_colour_manual(labels = c("Foo", "Bar"), values = c("black", "red")) +
scale_linetype_manual( labels = c("Foo", "Bar"),values = c("solid", "dashed") ) +
theme(legend.position="bottom", legend.title = element_blank())


enter image description here






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%2f53275734%2fchanging-legend-labels-without-changing-legend-markers-or-quantity%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









    0














    You van use scale_linetype_manual, but don't forget to set the same labels!



    library(ggplot2)

    ggplot2::ggplot(data = mydat,
    aes(x = var2,
    y = fit,
    group = var1)) +
    theme_minimal() +
    geom_point(aes(color = var1)) +
    geom_line(aes(color = var1, linetype = var1)) +

    geom_errorbar(aes(ymin = lwr,
    ymax = upr,
    width = 0.1,
    color = var1,
    linetype = var1)) +
    labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
    theme(panel.grid.minor = element_blank()) +
    scale_colour_manual(labels = c("Foo", "Bar"), values = c("black", "red")) +
    scale_linetype_manual( labels = c("Foo", "Bar"),values = c("solid", "dashed") ) +
    theme(legend.position="bottom", legend.title = element_blank())


    enter image description here






    share|improve this answer



























      0














      You van use scale_linetype_manual, but don't forget to set the same labels!



      library(ggplot2)

      ggplot2::ggplot(data = mydat,
      aes(x = var2,
      y = fit,
      group = var1)) +
      theme_minimal() +
      geom_point(aes(color = var1)) +
      geom_line(aes(color = var1, linetype = var1)) +

      geom_errorbar(aes(ymin = lwr,
      ymax = upr,
      width = 0.1,
      color = var1,
      linetype = var1)) +
      labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
      theme(panel.grid.minor = element_blank()) +
      scale_colour_manual(labels = c("Foo", "Bar"), values = c("black", "red")) +
      scale_linetype_manual( labels = c("Foo", "Bar"),values = c("solid", "dashed") ) +
      theme(legend.position="bottom", legend.title = element_blank())


      enter image description here






      share|improve this answer

























        0












        0








        0







        You van use scale_linetype_manual, but don't forget to set the same labels!



        library(ggplot2)

        ggplot2::ggplot(data = mydat,
        aes(x = var2,
        y = fit,
        group = var1)) +
        theme_minimal() +
        geom_point(aes(color = var1)) +
        geom_line(aes(color = var1, linetype = var1)) +

        geom_errorbar(aes(ymin = lwr,
        ymax = upr,
        width = 0.1,
        color = var1,
        linetype = var1)) +
        labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
        theme(panel.grid.minor = element_blank()) +
        scale_colour_manual(labels = c("Foo", "Bar"), values = c("black", "red")) +
        scale_linetype_manual( labels = c("Foo", "Bar"),values = c("solid", "dashed") ) +
        theme(legend.position="bottom", legend.title = element_blank())


        enter image description here






        share|improve this answer













        You van use scale_linetype_manual, but don't forget to set the same labels!



        library(ggplot2)

        ggplot2::ggplot(data = mydat,
        aes(x = var2,
        y = fit,
        group = var1)) +
        theme_minimal() +
        geom_point(aes(color = var1)) +
        geom_line(aes(color = var1, linetype = var1)) +

        geom_errorbar(aes(ymin = lwr,
        ymax = upr,
        width = 0.1,
        color = var1,
        linetype = var1)) +
        labs(x = "var2", y = "var1", color = "var3", linetype = "var3") +
        theme(panel.grid.minor = element_blank()) +
        scale_colour_manual(labels = c("Foo", "Bar"), values = c("black", "red")) +
        scale_linetype_manual( labels = c("Foo", "Bar"),values = c("solid", "dashed") ) +
        theme(legend.position="bottom", legend.title = element_blank())


        enter image description here







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 7:31









        WimpelWimpel

        4,790321




        4,790321



























            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%2f53275734%2fchanging-legend-labels-without-changing-legend-markers-or-quantity%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

            Use pre created SQLite database for Android project in kotlin

            Darth Vader #20

            Ondo