ggplot facet_wrap_paginate pages grouped by variable
I'm working on creating some harvest plots for a paper and am stumbling a bit with the code. I have recreated the important bits using 'diamonds' so that it's easier for people to recreate
Part 1
The aim is to create a bar chart that will facet by multiple variables, e.g. 'carat' and 'color', as these will act as the titles for the plots. I've used ggforce's paginate to allow me to spread it over multiple pages, as I'd like each page to show results by a group - here I've added values '1', '2', or '3' to each row of the dataframe. Whilst I could subset the dataframe and create the plots individually, the issue is that the widths of the bars aren't consistent between pages, even when I add width = x to geom_bar (though the widths are the same within each page).
Does anyone have an idea of how I can accomplish this? I was wondering if aes_string would help, but wasn't sure it'd work with the multiple facets I need.
Part 2
When I try and add in some code to save the images it overrides the grid.arrange ... command to specify plot size (so they are all consistent) and adjusts to fill the white space. Is this easily fixed?
Thanks,
Cal
library(ggplot2)
library(ggforce)
library(plyr)
library(dplyr)
library(grid)
library(egg)
df = diamonds
df$Group<- rep(1:3,length.out=nrow(df))
for (i in df$Group) {
p <- ggplot(data=df, aes(x=cut, y=clarity, fill=price)) +
# preserve = single keeps all bars same width, rather than adjusting to
# the space
geom_bar(position=position_dodge2(preserve = 'single'),
stat="identity", color = "black", size = 0.2) +
# paginate allows the chart to be printed on multiple pages
# strip position adds facet title to top of page
facet_wrap_paginate(c("carat","color"), ncol = 3, nrow = 3,
scales = "fixed", strip.position = "top", page = i)
# manually adjust the size of the plot panels
grid.arrange(grobs = lapply(
list(p),
set_panel_size,
width = unit(8,"cm"),
height = unit(5,"cm")
))
}
r ggplot2 facet-wrap ggforce
add a comment |
I'm working on creating some harvest plots for a paper and am stumbling a bit with the code. I have recreated the important bits using 'diamonds' so that it's easier for people to recreate
Part 1
The aim is to create a bar chart that will facet by multiple variables, e.g. 'carat' and 'color', as these will act as the titles for the plots. I've used ggforce's paginate to allow me to spread it over multiple pages, as I'd like each page to show results by a group - here I've added values '1', '2', or '3' to each row of the dataframe. Whilst I could subset the dataframe and create the plots individually, the issue is that the widths of the bars aren't consistent between pages, even when I add width = x to geom_bar (though the widths are the same within each page).
Does anyone have an idea of how I can accomplish this? I was wondering if aes_string would help, but wasn't sure it'd work with the multiple facets I need.
Part 2
When I try and add in some code to save the images it overrides the grid.arrange ... command to specify plot size (so they are all consistent) and adjusts to fill the white space. Is this easily fixed?
Thanks,
Cal
library(ggplot2)
library(ggforce)
library(plyr)
library(dplyr)
library(grid)
library(egg)
df = diamonds
df$Group<- rep(1:3,length.out=nrow(df))
for (i in df$Group) {
p <- ggplot(data=df, aes(x=cut, y=clarity, fill=price)) +
# preserve = single keeps all bars same width, rather than adjusting to
# the space
geom_bar(position=position_dodge2(preserve = 'single'),
stat="identity", color = "black", size = 0.2) +
# paginate allows the chart to be printed on multiple pages
# strip position adds facet title to top of page
facet_wrap_paginate(c("carat","color"), ncol = 3, nrow = 3,
scales = "fixed", strip.position = "top", page = i)
# manually adjust the size of the plot panels
grid.arrange(grobs = lapply(
list(p),
set_panel_size,
width = unit(8,"cm"),
height = unit(5,"cm")
))
}
r ggplot2 facet-wrap ggforce
add a comment |
I'm working on creating some harvest plots for a paper and am stumbling a bit with the code. I have recreated the important bits using 'diamonds' so that it's easier for people to recreate
Part 1
The aim is to create a bar chart that will facet by multiple variables, e.g. 'carat' and 'color', as these will act as the titles for the plots. I've used ggforce's paginate to allow me to spread it over multiple pages, as I'd like each page to show results by a group - here I've added values '1', '2', or '3' to each row of the dataframe. Whilst I could subset the dataframe and create the plots individually, the issue is that the widths of the bars aren't consistent between pages, even when I add width = x to geom_bar (though the widths are the same within each page).
Does anyone have an idea of how I can accomplish this? I was wondering if aes_string would help, but wasn't sure it'd work with the multiple facets I need.
Part 2
When I try and add in some code to save the images it overrides the grid.arrange ... command to specify plot size (so they are all consistent) and adjusts to fill the white space. Is this easily fixed?
Thanks,
Cal
library(ggplot2)
library(ggforce)
library(plyr)
library(dplyr)
library(grid)
library(egg)
df = diamonds
df$Group<- rep(1:3,length.out=nrow(df))
for (i in df$Group) {
p <- ggplot(data=df, aes(x=cut, y=clarity, fill=price)) +
# preserve = single keeps all bars same width, rather than adjusting to
# the space
geom_bar(position=position_dodge2(preserve = 'single'),
stat="identity", color = "black", size = 0.2) +
# paginate allows the chart to be printed on multiple pages
# strip position adds facet title to top of page
facet_wrap_paginate(c("carat","color"), ncol = 3, nrow = 3,
scales = "fixed", strip.position = "top", page = i)
# manually adjust the size of the plot panels
grid.arrange(grobs = lapply(
list(p),
set_panel_size,
width = unit(8,"cm"),
height = unit(5,"cm")
))
}
r ggplot2 facet-wrap ggforce
I'm working on creating some harvest plots for a paper and am stumbling a bit with the code. I have recreated the important bits using 'diamonds' so that it's easier for people to recreate
Part 1
The aim is to create a bar chart that will facet by multiple variables, e.g. 'carat' and 'color', as these will act as the titles for the plots. I've used ggforce's paginate to allow me to spread it over multiple pages, as I'd like each page to show results by a group - here I've added values '1', '2', or '3' to each row of the dataframe. Whilst I could subset the dataframe and create the plots individually, the issue is that the widths of the bars aren't consistent between pages, even when I add width = x to geom_bar (though the widths are the same within each page).
Does anyone have an idea of how I can accomplish this? I was wondering if aes_string would help, but wasn't sure it'd work with the multiple facets I need.
Part 2
When I try and add in some code to save the images it overrides the grid.arrange ... command to specify plot size (so they are all consistent) and adjusts to fill the white space. Is this easily fixed?
Thanks,
Cal
library(ggplot2)
library(ggforce)
library(plyr)
library(dplyr)
library(grid)
library(egg)
df = diamonds
df$Group<- rep(1:3,length.out=nrow(df))
for (i in df$Group) {
p <- ggplot(data=df, aes(x=cut, y=clarity, fill=price)) +
# preserve = single keeps all bars same width, rather than adjusting to
# the space
geom_bar(position=position_dodge2(preserve = 'single'),
stat="identity", color = "black", size = 0.2) +
# paginate allows the chart to be printed on multiple pages
# strip position adds facet title to top of page
facet_wrap_paginate(c("carat","color"), ncol = 3, nrow = 3,
scales = "fixed", strip.position = "top", page = i)
# manually adjust the size of the plot panels
grid.arrange(grobs = lapply(
list(p),
set_panel_size,
width = unit(8,"cm"),
height = unit(5,"cm")
))
}
r ggplot2 facet-wrap ggforce
r ggplot2 facet-wrap ggforce
edited Nov 20 '18 at 19:57
Cal
asked Nov 20 '18 at 16:11
CalCal
519
519
add a comment |
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%2f53397084%2fggplot-facet-wrap-paginate-pages-grouped-by-variable%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%2f53397084%2fggplot-facet-wrap-paginate-pages-grouped-by-variable%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