Barplot labels too long, is it possible to set a “label width”












1















I am trying to create a stacked barplot where beside = TRUE. Here is the data used to generate this figure;



significant <- c(27, 44, 25, 54, 40, 31, 25,  9, 57, 59)
annotated <- c(119, 267, 109, 373, 250, 173, 124, 20, 452, 478)
names <- c("mitochondrial gene expression","ncRNA metabolic process",
"mitochondrial translation", "translation",
"ribonucleoprotein complex biogenesis", "ribosome biogenesis",
"rRNA metabolic process", "transcription preinitiation complex asse...",
"peptide biosynthetic process", "amide biosynthetic process")
data = rbind(significant, annotated)
colnames(data) <- names
rownames(data) <- c("significant", "annotated")


My plotting code is;



printBarPlots <- function(input, main){
data = rbind(input[,4], input[,3])

colnames(data)= input[,2]
rownames(data)=c("Significant", "Annotated")
par(mar=c(5,9,4,2))
mybar = barplot(data, width = 3, xlab = "Number of genes", main = main,
horiz = T, cex.axis = 0.8, beside = TRUE, las = 1,
cex.names = 0.8, legend = T, args.legend = list(x="right"))
}


barplot showing long margins on the left handside



Using this code, the bar labels extend far to the left of my plot. My question is unlike this questionbecause splitting the bar names at each space would still require having to have a small cex.names. Is it possible to specify that rather than having something like "transcription preinitiation complex asse..." written on one line, it can be spread out over two lines, such as below, to make better use of space? Or perhaps, some sort of code to split names onto different lines following a certain amount of letters (e.g. start new line after 13 characters).



"transcription preinitiation



complex asse..."










share|improve this question

























  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.

    – MrFlick
    Nov 21 '18 at 20:04











  • You might look into str_wrap from the stringr package, or something similar can be done in base R using strwrap, paste and maybe sapply.

    – joran
    Nov 21 '18 at 20:19






  • 1





    Possible duplicate of Add line break to axis labels and ticks in ggplot

    – iod
    Nov 21 '18 at 20:20
















1















I am trying to create a stacked barplot where beside = TRUE. Here is the data used to generate this figure;



significant <- c(27, 44, 25, 54, 40, 31, 25,  9, 57, 59)
annotated <- c(119, 267, 109, 373, 250, 173, 124, 20, 452, 478)
names <- c("mitochondrial gene expression","ncRNA metabolic process",
"mitochondrial translation", "translation",
"ribonucleoprotein complex biogenesis", "ribosome biogenesis",
"rRNA metabolic process", "transcription preinitiation complex asse...",
"peptide biosynthetic process", "amide biosynthetic process")
data = rbind(significant, annotated)
colnames(data) <- names
rownames(data) <- c("significant", "annotated")


My plotting code is;



printBarPlots <- function(input, main){
data = rbind(input[,4], input[,3])

colnames(data)= input[,2]
rownames(data)=c("Significant", "Annotated")
par(mar=c(5,9,4,2))
mybar = barplot(data, width = 3, xlab = "Number of genes", main = main,
horiz = T, cex.axis = 0.8, beside = TRUE, las = 1,
cex.names = 0.8, legend = T, args.legend = list(x="right"))
}


barplot showing long margins on the left handside



Using this code, the bar labels extend far to the left of my plot. My question is unlike this questionbecause splitting the bar names at each space would still require having to have a small cex.names. Is it possible to specify that rather than having something like "transcription preinitiation complex asse..." written on one line, it can be spread out over two lines, such as below, to make better use of space? Or perhaps, some sort of code to split names onto different lines following a certain amount of letters (e.g. start new line after 13 characters).



"transcription preinitiation



complex asse..."










share|improve this question

























  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.

    – MrFlick
    Nov 21 '18 at 20:04











  • You might look into str_wrap from the stringr package, or something similar can be done in base R using strwrap, paste and maybe sapply.

    – joran
    Nov 21 '18 at 20:19






  • 1





    Possible duplicate of Add line break to axis labels and ticks in ggplot

    – iod
    Nov 21 '18 at 20:20














1












1








1








I am trying to create a stacked barplot where beside = TRUE. Here is the data used to generate this figure;



significant <- c(27, 44, 25, 54, 40, 31, 25,  9, 57, 59)
annotated <- c(119, 267, 109, 373, 250, 173, 124, 20, 452, 478)
names <- c("mitochondrial gene expression","ncRNA metabolic process",
"mitochondrial translation", "translation",
"ribonucleoprotein complex biogenesis", "ribosome biogenesis",
"rRNA metabolic process", "transcription preinitiation complex asse...",
"peptide biosynthetic process", "amide biosynthetic process")
data = rbind(significant, annotated)
colnames(data) <- names
rownames(data) <- c("significant", "annotated")


My plotting code is;



printBarPlots <- function(input, main){
data = rbind(input[,4], input[,3])

colnames(data)= input[,2]
rownames(data)=c("Significant", "Annotated")
par(mar=c(5,9,4,2))
mybar = barplot(data, width = 3, xlab = "Number of genes", main = main,
horiz = T, cex.axis = 0.8, beside = TRUE, las = 1,
cex.names = 0.8, legend = T, args.legend = list(x="right"))
}


barplot showing long margins on the left handside



Using this code, the bar labels extend far to the left of my plot. My question is unlike this questionbecause splitting the bar names at each space would still require having to have a small cex.names. Is it possible to specify that rather than having something like "transcription preinitiation complex asse..." written on one line, it can be spread out over two lines, such as below, to make better use of space? Or perhaps, some sort of code to split names onto different lines following a certain amount of letters (e.g. start new line after 13 characters).



"transcription preinitiation



complex asse..."










share|improve this question
















I am trying to create a stacked barplot where beside = TRUE. Here is the data used to generate this figure;



significant <- c(27, 44, 25, 54, 40, 31, 25,  9, 57, 59)
annotated <- c(119, 267, 109, 373, 250, 173, 124, 20, 452, 478)
names <- c("mitochondrial gene expression","ncRNA metabolic process",
"mitochondrial translation", "translation",
"ribonucleoprotein complex biogenesis", "ribosome biogenesis",
"rRNA metabolic process", "transcription preinitiation complex asse...",
"peptide biosynthetic process", "amide biosynthetic process")
data = rbind(significant, annotated)
colnames(data) <- names
rownames(data) <- c("significant", "annotated")


My plotting code is;



printBarPlots <- function(input, main){
data = rbind(input[,4], input[,3])

colnames(data)= input[,2]
rownames(data)=c("Significant", "Annotated")
par(mar=c(5,9,4,2))
mybar = barplot(data, width = 3, xlab = "Number of genes", main = main,
horiz = T, cex.axis = 0.8, beside = TRUE, las = 1,
cex.names = 0.8, legend = T, args.legend = list(x="right"))
}


barplot showing long margins on the left handside



Using this code, the bar labels extend far to the left of my plot. My question is unlike this questionbecause splitting the bar names at each space would still require having to have a small cex.names. Is it possible to specify that rather than having something like "transcription preinitiation complex asse..." written on one line, it can be spread out over two lines, such as below, to make better use of space? Or perhaps, some sort of code to split names onto different lines following a certain amount of letters (e.g. start new line after 13 characters).



"transcription preinitiation



complex asse..."







r plot graph bar-chart






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 22 '18 at 11:38







Harnarday

















asked Nov 21 '18 at 20:00









HarnardayHarnarday

65




65













  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.

    – MrFlick
    Nov 21 '18 at 20:04











  • You might look into str_wrap from the stringr package, or something similar can be done in base R using strwrap, paste and maybe sapply.

    – joran
    Nov 21 '18 at 20:19






  • 1





    Possible duplicate of Add line break to axis labels and ticks in ggplot

    – iod
    Nov 21 '18 at 20:20



















  • It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.

    – MrFlick
    Nov 21 '18 at 20:04











  • You might look into str_wrap from the stringr package, or something similar can be done in base R using strwrap, paste and maybe sapply.

    – joran
    Nov 21 '18 at 20:19






  • 1





    Possible duplicate of Add line break to axis labels and ticks in ggplot

    – iod
    Nov 21 '18 at 20:20

















It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.

– MrFlick
Nov 21 '18 at 20:04





It's easier to help you if you include a simple reproducible example with sample input and desired output that can be used to test and verify possible solutions.

– MrFlick
Nov 21 '18 at 20:04













You might look into str_wrap from the stringr package, or something similar can be done in base R using strwrap, paste and maybe sapply.

– joran
Nov 21 '18 at 20:19





You might look into str_wrap from the stringr package, or something similar can be done in base R using strwrap, paste and maybe sapply.

– joran
Nov 21 '18 at 20:19




1




1





Possible duplicate of Add line break to axis labels and ticks in ggplot

– iod
Nov 21 '18 at 20:20





Possible duplicate of Add line break to axis labels and ticks in ggplot

– iod
Nov 21 '18 at 20:20












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419671%2fbarplot-labels-too-long-is-it-possible-to-set-a-label-width%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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53419671%2fbarplot-labels-too-long-is-it-possible-to-set-a-label-width%23new-answer', 'question_page');
}
);

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







這個網誌中的熱門文章

Xamarin.form Move up view when keyboard appear

Post-Redirect-Get with Spring WebFlux and Thymeleaf

Anylogic : not able to use stopDelay()