r - adding manual labels in ggplot2 when using geom_line grouping
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
Using multiple functionalities of the ggplot2
package in order to make a plot with geom_line
, strange output is produced when several of them are combined like in the following:
Necessary libraries:
library(ggplot2)
library(dplyr)
library(plotly)
Sample dataset:
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
b = rnorm(10, mean = 2, sd = 5),
c = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
The plot:
ggplot(df, aes(x = a)) +
geom_line(aes(y = b, group = 1, color = "line_one"), size = 2, alpha = 0.6) +
geom_line(aes(y = c, group = 1, color = "line_two"), size = 2, alpha = 0.6) +
scale_y_continuous(sec.axis = sec_axis(~. + 10)) +
labs(x = "My x axis",
y = "My y axis") +
theme(axis.text = element_text(angle = 90, hjust = 0.4, vjust = -0.5)) +
geom_hline(yintercept = df$b %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6) +
scale_color_manual(
name = "",
values = c("line_one" = "red", "line_two" = "blue")
) +
theme_light()
The output produced does not show the color of the lines in the legend box:
r ggplot2
add a comment |
Using multiple functionalities of the ggplot2
package in order to make a plot with geom_line
, strange output is produced when several of them are combined like in the following:
Necessary libraries:
library(ggplot2)
library(dplyr)
library(plotly)
Sample dataset:
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
b = rnorm(10, mean = 2, sd = 5),
c = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
The plot:
ggplot(df, aes(x = a)) +
geom_line(aes(y = b, group = 1, color = "line_one"), size = 2, alpha = 0.6) +
geom_line(aes(y = c, group = 1, color = "line_two"), size = 2, alpha = 0.6) +
scale_y_continuous(sec.axis = sec_axis(~. + 10)) +
labs(x = "My x axis",
y = "My y axis") +
theme(axis.text = element_text(angle = 90, hjust = 0.4, vjust = -0.5)) +
geom_hline(yintercept = df$b %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6) +
scale_color_manual(
name = "",
values = c("line_one" = "red", "line_two" = "blue")
) +
theme_light()
The output produced does not show the color of the lines in the legend box:
r ggplot2
Can't reproduce (I get corresponding colors in the legend) . You might want to update your packages.
– PoGibas
Jan 3 at 14:38
add a comment |
Using multiple functionalities of the ggplot2
package in order to make a plot with geom_line
, strange output is produced when several of them are combined like in the following:
Necessary libraries:
library(ggplot2)
library(dplyr)
library(plotly)
Sample dataset:
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
b = rnorm(10, mean = 2, sd = 5),
c = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
The plot:
ggplot(df, aes(x = a)) +
geom_line(aes(y = b, group = 1, color = "line_one"), size = 2, alpha = 0.6) +
geom_line(aes(y = c, group = 1, color = "line_two"), size = 2, alpha = 0.6) +
scale_y_continuous(sec.axis = sec_axis(~. + 10)) +
labs(x = "My x axis",
y = "My y axis") +
theme(axis.text = element_text(angle = 90, hjust = 0.4, vjust = -0.5)) +
geom_hline(yintercept = df$b %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6) +
scale_color_manual(
name = "",
values = c("line_one" = "red", "line_two" = "blue")
) +
theme_light()
The output produced does not show the color of the lines in the legend box:
r ggplot2
Using multiple functionalities of the ggplot2
package in order to make a plot with geom_line
, strange output is produced when several of them are combined like in the following:
Necessary libraries:
library(ggplot2)
library(dplyr)
library(plotly)
Sample dataset:
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
b = rnorm(10, mean = 2, sd = 5),
c = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
The plot:
ggplot(df, aes(x = a)) +
geom_line(aes(y = b, group = 1, color = "line_one"), size = 2, alpha = 0.6) +
geom_line(aes(y = c, group = 1, color = "line_two"), size = 2, alpha = 0.6) +
scale_y_continuous(sec.axis = sec_axis(~. + 10)) +
labs(x = "My x axis",
y = "My y axis") +
theme(axis.text = element_text(angle = 90, hjust = 0.4, vjust = -0.5)) +
geom_hline(yintercept = df$b %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6) +
scale_color_manual(
name = "",
values = c("line_one" = "red", "line_two" = "blue")
) +
theme_light()
The output produced does not show the color of the lines in the legend box:
r ggplot2
r ggplot2
asked Jan 3 at 13:55


adladl
362213
362213
Can't reproduce (I get corresponding colors in the legend) . You might want to update your packages.
– PoGibas
Jan 3 at 14:38
add a comment |
Can't reproduce (I get corresponding colors in the legend) . You might want to update your packages.
– PoGibas
Jan 3 at 14:38
Can't reproduce (I get corresponding colors in the legend) . You might want to update your packages.
– PoGibas
Jan 3 at 14:38
Can't reproduce (I get corresponding colors in the legend) . You might want to update your packages.
– PoGibas
Jan 3 at 14:38
add a comment |
1 Answer
1
active
oldest
votes
You should always use long data when using ggplot2
. By doing so, you don't need to add each and every single line as a layer and you also don't need to manually adjust legend etc. Here an example based on yours (I used tidyr::gather()
to transform to long):
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
line_one = rnorm(10, mean = 2, sd = 5),
line_two = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
df %>%
tidyr::gather(key = line, value = value, line_one, line_two) %>%
ggplot(aes(x = a, y = value, color = line, group = line)) +
geom_line() +
scale_color_manual(
name = NULL,
values = c("line_one" = "red", "line_two" = "blue")
) +
geom_hline(yintercept = df$line_one %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6)
Edit:
Another example showing the flexibility of long data with different layers:
library(ggplot2)
library(tidyr)
set.seed(1)
df_long <- data.frame(
x = 1:10,
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
) %>%
gather(key = key, value = y, -x)
ggplot(mapping = aes(x = x, y = y, fill = key, color = key)) +
geom_col(data = subset(df_long, key %in% "a")) +
geom_line(data = subset(df_long, key %in% c("b", "c"))) +
geom_point(data = subset(df_long, key %in% "d"))
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. Yougather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).
– Tino
Jan 3 at 14:42
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%2f54023716%2fr-adding-manual-labels-in-ggplot2-when-using-geom-line-grouping%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 should always use long data when using ggplot2
. By doing so, you don't need to add each and every single line as a layer and you also don't need to manually adjust legend etc. Here an example based on yours (I used tidyr::gather()
to transform to long):
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
line_one = rnorm(10, mean = 2, sd = 5),
line_two = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
df %>%
tidyr::gather(key = line, value = value, line_one, line_two) %>%
ggplot(aes(x = a, y = value, color = line, group = line)) +
geom_line() +
scale_color_manual(
name = NULL,
values = c("line_one" = "red", "line_two" = "blue")
) +
geom_hline(yintercept = df$line_one %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6)
Edit:
Another example showing the flexibility of long data with different layers:
library(ggplot2)
library(tidyr)
set.seed(1)
df_long <- data.frame(
x = 1:10,
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
) %>%
gather(key = key, value = y, -x)
ggplot(mapping = aes(x = x, y = y, fill = key, color = key)) +
geom_col(data = subset(df_long, key %in% "a")) +
geom_line(data = subset(df_long, key %in% c("b", "c"))) +
geom_point(data = subset(df_long, key %in% "d"))
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. Yougather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).
– Tino
Jan 3 at 14:42
add a comment |
You should always use long data when using ggplot2
. By doing so, you don't need to add each and every single line as a layer and you also don't need to manually adjust legend etc. Here an example based on yours (I used tidyr::gather()
to transform to long):
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
line_one = rnorm(10, mean = 2, sd = 5),
line_two = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
df %>%
tidyr::gather(key = line, value = value, line_one, line_two) %>%
ggplot(aes(x = a, y = value, color = line, group = line)) +
geom_line() +
scale_color_manual(
name = NULL,
values = c("line_one" = "red", "line_two" = "blue")
) +
geom_hline(yintercept = df$line_one %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6)
Edit:
Another example showing the flexibility of long data with different layers:
library(ggplot2)
library(tidyr)
set.seed(1)
df_long <- data.frame(
x = 1:10,
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
) %>%
gather(key = key, value = y, -x)
ggplot(mapping = aes(x = x, y = y, fill = key, color = key)) +
geom_col(data = subset(df_long, key %in% "a")) +
geom_line(data = subset(df_long, key %in% c("b", "c"))) +
geom_point(data = subset(df_long, key %in% "d"))
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. Yougather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).
– Tino
Jan 3 at 14:42
add a comment |
You should always use long data when using ggplot2
. By doing so, you don't need to add each and every single line as a layer and you also don't need to manually adjust legend etc. Here an example based on yours (I used tidyr::gather()
to transform to long):
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
line_one = rnorm(10, mean = 2, sd = 5),
line_two = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
df %>%
tidyr::gather(key = line, value = value, line_one, line_two) %>%
ggplot(aes(x = a, y = value, color = line, group = line)) +
geom_line() +
scale_color_manual(
name = NULL,
values = c("line_one" = "red", "line_two" = "blue")
) +
geom_hline(yintercept = df$line_one %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6)
Edit:
Another example showing the flexibility of long data with different layers:
library(ggplot2)
library(tidyr)
set.seed(1)
df_long <- data.frame(
x = 1:10,
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
) %>%
gather(key = key, value = y, -x)
ggplot(mapping = aes(x = x, y = y, fill = key, color = key)) +
geom_col(data = subset(df_long, key %in% "a")) +
geom_line(data = subset(df_long, key %in% c("b", "c"))) +
geom_point(data = subset(df_long, key %in% "d"))
You should always use long data when using ggplot2
. By doing so, you don't need to add each and every single line as a layer and you also don't need to manually adjust legend etc. Here an example based on yours (I used tidyr::gather()
to transform to long):
df <- data.frame(a = sample(LETTERS, 10, replace = FALSE),
line_one = rnorm(10, mean = 2, sd = 5),
line_two = rnorm(10, mean = 15, sd = 5),
d = sample(letters, 10, replace = FALSE))
df %>%
tidyr::gather(key = line, value = value, line_one, line_two) %>%
ggplot(aes(x = a, y = value, color = line, group = line)) +
geom_line() +
scale_color_manual(
name = NULL,
values = c("line_one" = "red", "line_two" = "blue")
) +
geom_hline(yintercept = df$line_one %>% quantile(.99),
size = 2,
color = "tomato",
linetype = "dashed",
alpha = 0.6)
Edit:
Another example showing the flexibility of long data with different layers:
library(ggplot2)
library(tidyr)
set.seed(1)
df_long <- data.frame(
x = 1:10,
a = rnorm(10),
b = rnorm(10),
c = rnorm(10),
d = rnorm(10)
) %>%
gather(key = key, value = y, -x)
ggplot(mapping = aes(x = x, y = y, fill = key, color = key)) +
geom_col(data = subset(df_long, key %in% "a")) +
geom_line(data = subset(df_long, key %in% c("b", "c"))) +
geom_point(data = subset(df_long, key %in% "d"))
edited Jan 3 at 14:50
answered Jan 3 at 14:10
TinoTino
1,707813
1,707813
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. Yougather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).
– Tino
Jan 3 at 14:42
add a comment |
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. Yougather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).
– Tino
Jan 3 at 14:42
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
yes, but sometimes we might want to add a bar layer or a point layer instead of having all of them being lines, like for example in this post: stackoverflow.com/questions/47023781/…
– adl
Jan 3 at 14:35
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. You
gather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).– Tino
Jan 3 at 14:42
Long still works best for that. Imagine you have 3 different values for each x (hence 3 colors) you want to represent as bars and only one line. You
gather
all and use subsets in the 2 layers with the desired keys instead of adding more and more layers for each line or bar you want to print (special case with bars: they will be printed on top of the previous, not stacked but behind).– Tino
Jan 3 at 14:42
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%2f54023716%2fr-adding-manual-labels-in-ggplot2-when-using-geom-line-grouping%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't reproduce (I get corresponding colors in the legend) . You might want to update your packages.
– PoGibas
Jan 3 at 14:38