ggplot2 data labels outside margins
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}
I'm making many figures in ggplot2 using a for loop, but my data labels are extending beyond the plot margin. I've tried using expand, but it only works for some figures. When I try to use par(mar) I get this error message:
Error: Don't know how to add o to a plot.
I also tried just using ggsave to save as a really wide file, but 1) that looks odd and 2) that won't work for making so many different figures.
Does anyone know of any other workarounds? Ideally a way to have the inner plot margins automatically set per figure based on the length of the bars + data labels. Below is the code I'm using and an example figure (you can see the bar for 'x' is outside the margin). Thank you in advance!
for (i in each) {
temp_plot = ggplot(data= subset(Data, Each == i)) +
geom_bar(stat = "identity",
aes(x = reorder(Letter, +Number), y = Number, fill = factor(Category))) +
xlab("Letters") +
ggtitle(paste0("Title"), subtitle = "Subtitle") +
coord_flip() +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size=16),
plot.subtitle = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#00358E", "#00AFD7"),
name= "Category",
labels=c("This","That")) +
geom_text(family="Verdana", size=3,
aes(label=Number2, x=reorder(Letter, +Number), y=Number),
position=position_dodge(width=0.8), hjust=-0.001) +
scale_y_continuous(labels = comma, expand = c(0.01,0)) +
scale_x_discrete(labels = letters)
ggsave(temp_plot, file=paste0("Example", i,".jpeg"))
}

r ggplot2
add a comment |
I'm making many figures in ggplot2 using a for loop, but my data labels are extending beyond the plot margin. I've tried using expand, but it only works for some figures. When I try to use par(mar) I get this error message:
Error: Don't know how to add o to a plot.
I also tried just using ggsave to save as a really wide file, but 1) that looks odd and 2) that won't work for making so many different figures.
Does anyone know of any other workarounds? Ideally a way to have the inner plot margins automatically set per figure based on the length of the bars + data labels. Below is the code I'm using and an example figure (you can see the bar for 'x' is outside the margin). Thank you in advance!
for (i in each) {
temp_plot = ggplot(data= subset(Data, Each == i)) +
geom_bar(stat = "identity",
aes(x = reorder(Letter, +Number), y = Number, fill = factor(Category))) +
xlab("Letters") +
ggtitle(paste0("Title"), subtitle = "Subtitle") +
coord_flip() +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size=16),
plot.subtitle = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#00358E", "#00AFD7"),
name= "Category",
labels=c("This","That")) +
geom_text(family="Verdana", size=3,
aes(label=Number2, x=reorder(Letter, +Number), y=Number),
position=position_dodge(width=0.8), hjust=-0.001) +
scale_y_continuous(labels = comma, expand = c(0.01,0)) +
scale_x_discrete(labels = letters)
ggsave(temp_plot, file=paste0("Example", i,".jpeg"))
}

r ggplot2
2
My general idiom for this is to explicitly setlimits=c(0,b)forscale_y_continuous()and ensurebis an a % out from that. Said % is something you'll need to figure out as we all have aesthetic preferences. If it's a one-off plot I just hardcode a # but if it's a series of similar ones (like this) I use that % computation method.
– hrbrmstr
Nov 16 '18 at 17:27
2
You can addclip = offinsidecoord_flip()similar to this
– Tung
Nov 16 '18 at 17:33
It's hard to say without a reproducible example. I'm not sure what exactly is going on with yourexpandargument in your y scale, but it looks like you're setting very very small padding or no padding around the range of your data. Look at the docs forscale_*_continuousand try out usingexpand_scale
– camille
Nov 16 '18 at 18:42
I don't think you can change the plot margins automatically to take into account the length of the text label in this example, because stretching & compressing the plot would affect the amount of buffer required, in x-axis terms. Perhaps you can orient the label inwards instead? Check out the accepted answer here for a discussion. In your case, that would be something likehjust = "inward"forgeom_text().
– Z.Lin
Dec 3 '18 at 9:29
add a comment |
I'm making many figures in ggplot2 using a for loop, but my data labels are extending beyond the plot margin. I've tried using expand, but it only works for some figures. When I try to use par(mar) I get this error message:
Error: Don't know how to add o to a plot.
I also tried just using ggsave to save as a really wide file, but 1) that looks odd and 2) that won't work for making so many different figures.
Does anyone know of any other workarounds? Ideally a way to have the inner plot margins automatically set per figure based on the length of the bars + data labels. Below is the code I'm using and an example figure (you can see the bar for 'x' is outside the margin). Thank you in advance!
for (i in each) {
temp_plot = ggplot(data= subset(Data, Each == i)) +
geom_bar(stat = "identity",
aes(x = reorder(Letter, +Number), y = Number, fill = factor(Category))) +
xlab("Letters") +
ggtitle(paste0("Title"), subtitle = "Subtitle") +
coord_flip() +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size=16),
plot.subtitle = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#00358E", "#00AFD7"),
name= "Category",
labels=c("This","That")) +
geom_text(family="Verdana", size=3,
aes(label=Number2, x=reorder(Letter, +Number), y=Number),
position=position_dodge(width=0.8), hjust=-0.001) +
scale_y_continuous(labels = comma, expand = c(0.01,0)) +
scale_x_discrete(labels = letters)
ggsave(temp_plot, file=paste0("Example", i,".jpeg"))
}

r ggplot2
I'm making many figures in ggplot2 using a for loop, but my data labels are extending beyond the plot margin. I've tried using expand, but it only works for some figures. When I try to use par(mar) I get this error message:
Error: Don't know how to add o to a plot.
I also tried just using ggsave to save as a really wide file, but 1) that looks odd and 2) that won't work for making so many different figures.
Does anyone know of any other workarounds? Ideally a way to have the inner plot margins automatically set per figure based on the length of the bars + data labels. Below is the code I'm using and an example figure (you can see the bar for 'x' is outside the margin). Thank you in advance!
for (i in each) {
temp_plot = ggplot(data= subset(Data, Each == i)) +
geom_bar(stat = "identity",
aes(x = reorder(Letter, +Number), y = Number, fill = factor(Category))) +
xlab("Letters") +
ggtitle(paste0("Title"), subtitle = "Subtitle") +
coord_flip() +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5, size=16),
plot.subtitle = element_text(hjust = 0.5)) +
scale_fill_manual(values = c("#00358E", "#00AFD7"),
name= "Category",
labels=c("This","That")) +
geom_text(family="Verdana", size=3,
aes(label=Number2, x=reorder(Letter, +Number), y=Number),
position=position_dodge(width=0.8), hjust=-0.001) +
scale_y_continuous(labels = comma, expand = c(0.01,0)) +
scale_x_discrete(labels = letters)
ggsave(temp_plot, file=paste0("Example", i,".jpeg"))
}

r ggplot2
r ggplot2
edited Dec 3 '18 at 9:26
Z.Lin
13.6k22240
13.6k22240
asked Nov 16 '18 at 17:23
user9351962user9351962
255
255
2
My general idiom for this is to explicitly setlimits=c(0,b)forscale_y_continuous()and ensurebis an a % out from that. Said % is something you'll need to figure out as we all have aesthetic preferences. If it's a one-off plot I just hardcode a # but if it's a series of similar ones (like this) I use that % computation method.
– hrbrmstr
Nov 16 '18 at 17:27
2
You can addclip = offinsidecoord_flip()similar to this
– Tung
Nov 16 '18 at 17:33
It's hard to say without a reproducible example. I'm not sure what exactly is going on with yourexpandargument in your y scale, but it looks like you're setting very very small padding or no padding around the range of your data. Look at the docs forscale_*_continuousand try out usingexpand_scale
– camille
Nov 16 '18 at 18:42
I don't think you can change the plot margins automatically to take into account the length of the text label in this example, because stretching & compressing the plot would affect the amount of buffer required, in x-axis terms. Perhaps you can orient the label inwards instead? Check out the accepted answer here for a discussion. In your case, that would be something likehjust = "inward"forgeom_text().
– Z.Lin
Dec 3 '18 at 9:29
add a comment |
2
My general idiom for this is to explicitly setlimits=c(0,b)forscale_y_continuous()and ensurebis an a % out from that. Said % is something you'll need to figure out as we all have aesthetic preferences. If it's a one-off plot I just hardcode a # but if it's a series of similar ones (like this) I use that % computation method.
– hrbrmstr
Nov 16 '18 at 17:27
2
You can addclip = offinsidecoord_flip()similar to this
– Tung
Nov 16 '18 at 17:33
It's hard to say without a reproducible example. I'm not sure what exactly is going on with yourexpandargument in your y scale, but it looks like you're setting very very small padding or no padding around the range of your data. Look at the docs forscale_*_continuousand try out usingexpand_scale
– camille
Nov 16 '18 at 18:42
I don't think you can change the plot margins automatically to take into account the length of the text label in this example, because stretching & compressing the plot would affect the amount of buffer required, in x-axis terms. Perhaps you can orient the label inwards instead? Check out the accepted answer here for a discussion. In your case, that would be something likehjust = "inward"forgeom_text().
– Z.Lin
Dec 3 '18 at 9:29
2
2
My general idiom for this is to explicitly set
limits=c(0,b) for scale_y_continuous() and ensure b is an a % out from that. Said % is something you'll need to figure out as we all have aesthetic preferences. If it's a one-off plot I just hardcode a # but if it's a series of similar ones (like this) I use that % computation method.– hrbrmstr
Nov 16 '18 at 17:27
My general idiom for this is to explicitly set
limits=c(0,b) for scale_y_continuous() and ensure b is an a % out from that. Said % is something you'll need to figure out as we all have aesthetic preferences. If it's a one-off plot I just hardcode a # but if it's a series of similar ones (like this) I use that % computation method.– hrbrmstr
Nov 16 '18 at 17:27
2
2
You can add
clip = off inside coord_flip() similar to this– Tung
Nov 16 '18 at 17:33
You can add
clip = off inside coord_flip() similar to this– Tung
Nov 16 '18 at 17:33
It's hard to say without a reproducible example. I'm not sure what exactly is going on with your
expand argument in your y scale, but it looks like you're setting very very small padding or no padding around the range of your data. Look at the docs for scale_*_continuous and try out using expand_scale– camille
Nov 16 '18 at 18:42
It's hard to say without a reproducible example. I'm not sure what exactly is going on with your
expand argument in your y scale, but it looks like you're setting very very small padding or no padding around the range of your data. Look at the docs for scale_*_continuous and try out using expand_scale– camille
Nov 16 '18 at 18:42
I don't think you can change the plot margins automatically to take into account the length of the text label in this example, because stretching & compressing the plot would affect the amount of buffer required, in x-axis terms. Perhaps you can orient the label inwards instead? Check out the accepted answer here for a discussion. In your case, that would be something like
hjust = "inward" for geom_text().– Z.Lin
Dec 3 '18 at 9:29
I don't think you can change the plot margins automatically to take into account the length of the text label in this example, because stretching & compressing the plot would affect the amount of buffer required, in x-axis terms. Perhaps you can orient the label inwards instead? Check out the accepted answer here for a discussion. In your case, that would be something like
hjust = "inward" for geom_text().– Z.Lin
Dec 3 '18 at 9:29
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%2f53342632%2fggplot2-data-labels-outside-margins%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%2f53342632%2fggplot2-data-labels-outside-margins%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
2
My general idiom for this is to explicitly set
limits=c(0,b)forscale_y_continuous()and ensurebis an a % out from that. Said % is something you'll need to figure out as we all have aesthetic preferences. If it's a one-off plot I just hardcode a # but if it's a series of similar ones (like this) I use that % computation method.– hrbrmstr
Nov 16 '18 at 17:27
2
You can add
clip = offinsidecoord_flip()similar to this– Tung
Nov 16 '18 at 17:33
It's hard to say without a reproducible example. I'm not sure what exactly is going on with your
expandargument in your y scale, but it looks like you're setting very very small padding or no padding around the range of your data. Look at the docs forscale_*_continuousand try out usingexpand_scale– camille
Nov 16 '18 at 18:42
I don't think you can change the plot margins automatically to take into account the length of the text label in this example, because stretching & compressing the plot would affect the amount of buffer required, in x-axis terms. Perhaps you can orient the label inwards instead? Check out the accepted answer here for a discussion. In your case, that would be something like
hjust = "inward"forgeom_text().– Z.Lin
Dec 3 '18 at 9:29