Group clause in ggplot loop
I would like to put a ggplot in a loop. The loop is to go over multiple columns. I managed this without the group clause in the aes command with the help of an udf I found on another thread:
How do I combine aes() and aes_string() options
# Necessary for use of BOTH aes and aes_string
`+.uneval` <- function(a,b) {
`class<-`(modifyList(a,b), "uneval")
}
# Create list of column names
columns = as.character(c("quantity_actual", "price_sale", "quantity_corrected"))
# Loop through the columns
for (i in columns)
{
print(
ggplot(demand, aes(x = date) + aes_string(y = i)) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
However, in another plot where I do need the group clause R is failing to recognise the existence of it
for (i in columns)
{
print(
ggplot(demand_year_month, aes(x = year_month) + aes_string(y = i), group = 1) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
because it is throwing the error:
'geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?'
My guess is modification of the udf is needed. If so, how? or is there a neater solution? Thank you.
r ggplot2
add a comment |
I would like to put a ggplot in a loop. The loop is to go over multiple columns. I managed this without the group clause in the aes command with the help of an udf I found on another thread:
How do I combine aes() and aes_string() options
# Necessary for use of BOTH aes and aes_string
`+.uneval` <- function(a,b) {
`class<-`(modifyList(a,b), "uneval")
}
# Create list of column names
columns = as.character(c("quantity_actual", "price_sale", "quantity_corrected"))
# Loop through the columns
for (i in columns)
{
print(
ggplot(demand, aes(x = date) + aes_string(y = i)) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
However, in another plot where I do need the group clause R is failing to recognise the existence of it
for (i in columns)
{
print(
ggplot(demand_year_month, aes(x = year_month) + aes_string(y = i), group = 1) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
because it is throwing the error:
'geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?'
My guess is modification of the udf is needed. If so, how? or is there a neater solution? Thank you.
r ggplot2
1
Try puttinggroup = 1
in thegeom_line()
layer or inside the globalaes()
.
– aosmith
Nov 21 '18 at 16:11
Just useaes(x = year_month, group=1)
or also add+aes(group = 1)
(rather than putting it after the comma). Right now you are not passing that as an aesthetic. Right now theggplot
function just ignores that additional parameter.
– MrFlick
Nov 21 '18 at 16:14
Something as simple as that. I can't believe it. Very grateful for your prompt reply @aosmith May I ask how you had this idea? Experience?
– T.Fung
Nov 21 '18 at 16:14
@ MrFlick Tried both. Both threw the error: 'Error: geom_line requires the following missing aesthetics: y' Seems y axis has to be mentioned. But thank you for your udf solution.
– T.Fung
Nov 21 '18 at 16:22
add a comment |
I would like to put a ggplot in a loop. The loop is to go over multiple columns. I managed this without the group clause in the aes command with the help of an udf I found on another thread:
How do I combine aes() and aes_string() options
# Necessary for use of BOTH aes and aes_string
`+.uneval` <- function(a,b) {
`class<-`(modifyList(a,b), "uneval")
}
# Create list of column names
columns = as.character(c("quantity_actual", "price_sale", "quantity_corrected"))
# Loop through the columns
for (i in columns)
{
print(
ggplot(demand, aes(x = date) + aes_string(y = i)) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
However, in another plot where I do need the group clause R is failing to recognise the existence of it
for (i in columns)
{
print(
ggplot(demand_year_month, aes(x = year_month) + aes_string(y = i), group = 1) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
because it is throwing the error:
'geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?'
My guess is modification of the udf is needed. If so, how? or is there a neater solution? Thank you.
r ggplot2
I would like to put a ggplot in a loop. The loop is to go over multiple columns. I managed this without the group clause in the aes command with the help of an udf I found on another thread:
How do I combine aes() and aes_string() options
# Necessary for use of BOTH aes and aes_string
`+.uneval` <- function(a,b) {
`class<-`(modifyList(a,b), "uneval")
}
# Create list of column names
columns = as.character(c("quantity_actual", "price_sale", "quantity_corrected"))
# Loop through the columns
for (i in columns)
{
print(
ggplot(demand, aes(x = date) + aes_string(y = i)) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
However, in another plot where I do need the group clause R is failing to recognise the existence of it
for (i in columns)
{
print(
ggplot(demand_year_month, aes(x = year_month) + aes_string(y = i), group = 1) +
geom_line() +
scale_y_continuous(labels = comma)
)
}
because it is throwing the error:
'geom_path: Each group consists of only one observation. Do you need to adjust the group aesthetic?'
My guess is modification of the udf is needed. If so, how? or is there a neater solution? Thank you.
r ggplot2
r ggplot2
asked Nov 21 '18 at 16:07
T.FungT.Fung
616
616
1
Try puttinggroup = 1
in thegeom_line()
layer or inside the globalaes()
.
– aosmith
Nov 21 '18 at 16:11
Just useaes(x = year_month, group=1)
or also add+aes(group = 1)
(rather than putting it after the comma). Right now you are not passing that as an aesthetic. Right now theggplot
function just ignores that additional parameter.
– MrFlick
Nov 21 '18 at 16:14
Something as simple as that. I can't believe it. Very grateful for your prompt reply @aosmith May I ask how you had this idea? Experience?
– T.Fung
Nov 21 '18 at 16:14
@ MrFlick Tried both. Both threw the error: 'Error: geom_line requires the following missing aesthetics: y' Seems y axis has to be mentioned. But thank you for your udf solution.
– T.Fung
Nov 21 '18 at 16:22
add a comment |
1
Try puttinggroup = 1
in thegeom_line()
layer or inside the globalaes()
.
– aosmith
Nov 21 '18 at 16:11
Just useaes(x = year_month, group=1)
or also add+aes(group = 1)
(rather than putting it after the comma). Right now you are not passing that as an aesthetic. Right now theggplot
function just ignores that additional parameter.
– MrFlick
Nov 21 '18 at 16:14
Something as simple as that. I can't believe it. Very grateful for your prompt reply @aosmith May I ask how you had this idea? Experience?
– T.Fung
Nov 21 '18 at 16:14
@ MrFlick Tried both. Both threw the error: 'Error: geom_line requires the following missing aesthetics: y' Seems y axis has to be mentioned. But thank you for your udf solution.
– T.Fung
Nov 21 '18 at 16:22
1
1
Try putting
group = 1
in the geom_line()
layer or inside the global aes()
.– aosmith
Nov 21 '18 at 16:11
Try putting
group = 1
in the geom_line()
layer or inside the global aes()
.– aosmith
Nov 21 '18 at 16:11
Just use
aes(x = year_month, group=1)
or also add +aes(group = 1)
(rather than putting it after the comma). Right now you are not passing that as an aesthetic. Right now the ggplot
function just ignores that additional parameter.– MrFlick
Nov 21 '18 at 16:14
Just use
aes(x = year_month, group=1)
or also add +aes(group = 1)
(rather than putting it after the comma). Right now you are not passing that as an aesthetic. Right now the ggplot
function just ignores that additional parameter.– MrFlick
Nov 21 '18 at 16:14
Something as simple as that. I can't believe it. Very grateful for your prompt reply @aosmith May I ask how you had this idea? Experience?
– T.Fung
Nov 21 '18 at 16:14
Something as simple as that. I can't believe it. Very grateful for your prompt reply @aosmith May I ask how you had this idea? Experience?
– T.Fung
Nov 21 '18 at 16:14
@ MrFlick Tried both. Both threw the error: 'Error: geom_line requires the following missing aesthetics: y' Seems y axis has to be mentioned. But thank you for your udf solution.
– T.Fung
Nov 21 '18 at 16:22
@ MrFlick Tried both. Both threw the error: 'Error: geom_line requires the following missing aesthetics: y' Seems y axis has to be mentioned. But thank you for your udf solution.
– T.Fung
Nov 21 '18 at 16:22
add a comment |
0
active
oldest
votes
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%2f53416095%2fgroup-clause-in-ggplot-loop%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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%2f53416095%2fgroup-clause-in-ggplot-loop%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
1
Try putting
group = 1
in thegeom_line()
layer or inside the globalaes()
.– aosmith
Nov 21 '18 at 16:11
Just use
aes(x = year_month, group=1)
or also add+aes(group = 1)
(rather than putting it after the comma). Right now you are not passing that as an aesthetic. Right now theggplot
function just ignores that additional parameter.– MrFlick
Nov 21 '18 at 16:14
Something as simple as that. I can't believe it. Very grateful for your prompt reply @aosmith May I ask how you had this idea? Experience?
– T.Fung
Nov 21 '18 at 16:14
@ MrFlick Tried both. Both threw the error: 'Error: geom_line requires the following missing aesthetics: y' Seems y axis has to be mentioned. But thank you for your udf solution.
– T.Fung
Nov 21 '18 at 16:22