2

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..."

Harnarday
  • 21
  • 7
  • It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-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
  • 1
    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](https://stackoverflow.com/questions/20123147/add-line-break-to-axis-labels-and-ticks-in-ggplot) – iod Nov 21 '18 at 20:20

0 Answers0