Maintain ggplot panel size while axis labels change length
How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.
library(dplyr)
library(ggplot2)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_short
plot_long
For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?
r ggplot2
add a comment |
How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.
library(dplyr)
library(ggplot2)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_short
plot_long
For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?
r ggplot2
Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54
@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02
Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32
add a comment |
How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.
library(dplyr)
library(ggplot2)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_short
plot_long
For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?
r ggplot2
How can I make the panels of separate ggplots align when the y-axis labels change in length across plots? Below I've saved two subsets of mtcars with longer and shorter model names. Although the overall plots are the same size, the panels are smaller in the mt_long plot because the y-axis labels take up more of the plot.
library(dplyr)
library(ggplot2)
ds_mt <- mtcars %>% rownames_to_column("model")
mt_short <- ds_mt %>% arrange(nchar(model)) %>% slice(1:4)
mt_long <- ds_mt %>% arrange(-nchar(model)) %>% slice(1:4)
plot_short <-
mt_short %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_long <-
mt_long %>%
ggplot(aes(x = model, y = mpg)) +
geom_col() +
coord_flip()
plot_short
plot_long
For this reprex, it is important that the plots be separate. Is there any way to set just the panel dimensions of the plot rather than the overall size of the plot?
r ggplot2
r ggplot2
asked Nov 13 at 5:37
Joe
683514
683514
Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54
@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02
Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32
add a comment |
Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54
@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02
Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32
Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54
Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54
@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02
@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02
Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32
Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32
add a comment |
2 Answers
2
active
oldest
votes
We can use gridarrange
from the egg
package
library(egg)
ggarrange(plot_short, plot_long, ncol = 1)
To save, use
gg <- ggarrange(plot_short, plot_long, ncol = 1)
ggsave("file.png", gg)
add a comment |
try egg::set_panel_size(plot_short)
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
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%2f53274483%2fmaintain-ggplot-panel-size-while-axis-labels-change-length%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
We can use gridarrange
from the egg
package
library(egg)
ggarrange(plot_short, plot_long, ncol = 1)
To save, use
gg <- ggarrange(plot_short, plot_long, ncol = 1)
ggsave("file.png", gg)
add a comment |
We can use gridarrange
from the egg
package
library(egg)
ggarrange(plot_short, plot_long, ncol = 1)
To save, use
gg <- ggarrange(plot_short, plot_long, ncol = 1)
ggsave("file.png", gg)
add a comment |
We can use gridarrange
from the egg
package
library(egg)
ggarrange(plot_short, plot_long, ncol = 1)
To save, use
gg <- ggarrange(plot_short, plot_long, ncol = 1)
ggsave("file.png", gg)
We can use gridarrange
from the egg
package
library(egg)
ggarrange(plot_short, plot_long, ncol = 1)
To save, use
gg <- ggarrange(plot_short, plot_long, ncol = 1)
ggsave("file.png", gg)
answered Nov 13 at 6:16
Maurits Evers
25.8k41532
25.8k41532
add a comment |
add a comment |
try egg::set_panel_size(plot_short)
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
add a comment |
try egg::set_panel_size(plot_short)
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
add a comment |
try egg::set_panel_size(plot_short)
try egg::set_panel_size(plot_short)
answered Nov 13 at 5:45
user10644264
111
111
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
add a comment |
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
Please improve your question little bit more and also demonstrate it
– Ved Prakash
Nov 13 at 5:54
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
@user10644264, your code is outputting a table rather than a plot. Is there a way to use set_panel_size with explicit measurements for width and length?
– Joe
Nov 13 at 16:33
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%2f53274483%2fmaintain-ggplot-panel-size-while-axis-labels-change-length%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
Is using facets not an option?
– Maurits Evers
Nov 13 at 5:54
@MauritsEvers no facets are out. This is just a reprex. In reality I need to produce a lot of plots from different datasets and want to control panel size explicitly if that is possible.
– Joe
Nov 13 at 6:02
Likely duplicate: How can I make consistent-width plots in ggplot (with legends)?
– Henrik
Nov 13 at 7:32