Changing legend labels without changing legend markers or quantity
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
add a comment |
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
Add ascale_linetype_discrete
and use thelabels
argument there and inscale_color
. Related: How do I manually change the key labels in a legend in ggplot2
– Henrik
Nov 13 '18 at 7:29
add a comment |
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
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
r ggplot2 plot
asked Nov 13 '18 at 7:16
user3614648user3614648
531619
531619
Add ascale_linetype_discrete
and use thelabels
argument there and inscale_color
. Related: How do I manually change the key labels in a legend in ggplot2
– Henrik
Nov 13 '18 at 7:29
add a comment |
Add ascale_linetype_discrete
and use thelabels
argument there and inscale_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 comment |
1 Answer
1
active
oldest
votes
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())
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%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
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())
add a comment |
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())
add a comment |
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())
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())
answered Nov 13 '18 at 7:31
WimpelWimpel
4,790321
4,790321
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%2f53275734%2fchanging-legend-labels-without-changing-legend-markers-or-quantity%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
Add a
scale_linetype_discrete
and use thelabels
argument there and inscale_color
. Related: How do I manually change the key labels in a legend in ggplot2– Henrik
Nov 13 '18 at 7:29