Simpler way to order facet_wrap plot by year
I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.
I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.
Is there an easier way ? Here's an example to illustrate what I'm talking about:
sub.data$year = factor(sub.data$year,
levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))
ggplot(data = sub.data, aes(x = sub.data$weight)) +
geom_histogram() + facet_wrap(~ year, ncol = 2)
Thank you
r ggplot2 facet-wrap
add a comment |
I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.
I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.
Is there an easier way ? Here's an example to illustrate what I'm talking about:
sub.data$year = factor(sub.data$year,
levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))
ggplot(data = sub.data, aes(x = sub.data$weight)) +
geom_histogram() + facet_wrap(~ year, ncol = 2)
Thank you
r ggplot2 facet-wrap
add a comment |
I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.
I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.
Is there an easier way ? Here's an example to illustrate what I'm talking about:
sub.data$year = factor(sub.data$year,
levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))
ggplot(data = sub.data, aes(x = sub.data$weight)) +
geom_histogram() + facet_wrap(~ year, ncol = 2)
Thank you
r ggplot2 facet-wrap
I have 20 years of weight data and I want to make a 10 rows by 2 columns facet_wrap figure with ggplot2. The problem is that I want to re-order the facets in such a way that I have the first ten years in the left column and the last 10 years in the right column.
I could re-order the levels manually as suggested in other posts but this is rather painful. Also, I'll have to reorder the levels again if I want to do the same thing but with different years.
Is there an easier way ? Here's an example to illustrate what I'm talking about:
sub.data$year = factor(sub.data$year,
levels = c(2000,2005,2001,2006,2002,2007,2003,2008,2004,2009...))
ggplot(data = sub.data, aes(x = sub.data$weight)) +
geom_histogram() + facet_wrap(~ year, ncol = 2)
Thank you
r ggplot2 facet-wrap
r ggplot2 facet-wrap
edited Nov 11 at 17:40
Julius Vainora
31.8k75978
31.8k75978
asked Nov 11 at 16:57
Blue
213
213
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
I think you need dir = "v".
library(tidyverse)
df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~year, ncol = 2, dir = "v")

Created on 2018-11-11 by the reprex package (v0.2.1)
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
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%2f53251046%2fsimpler-way-to-order-facet-wrap-plot-by-year%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
I think you need dir = "v".
library(tidyverse)
df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~year, ncol = 2, dir = "v")

Created on 2018-11-11 by the reprex package (v0.2.1)
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
add a comment |
I think you need dir = "v".
library(tidyverse)
df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~year, ncol = 2, dir = "v")

Created on 2018-11-11 by the reprex package (v0.2.1)
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
add a comment |
I think you need dir = "v".
library(tidyverse)
df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~year, ncol = 2, dir = "v")

Created on 2018-11-11 by the reprex package (v0.2.1)
I think you need dir = "v".
library(tidyverse)
df <- data.frame(year = rep(2001:2020, 5), x = runif(100), y = runif(100))
ggplot(df, aes(x, y)) +
geom_point() +
facet_wrap(~year, ncol = 2, dir = "v")

Created on 2018-11-11 by the reprex package (v0.2.1)
answered Nov 11 at 17:16
Clemens Hug
1765
1765
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
add a comment |
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
Ah! How could I miss this ?
– Blue
Nov 12 at 18:43
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53251046%2fsimpler-way-to-order-facet-wrap-plot-by-year%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