Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to put groups of rotated x-labels in labeled brackets?

Tags:

r

bar-chart

How to put groups of rotated x-labels in labeled brackets, here is an example:

This is a Wego web tools results, but I want to do in R.

I know it add segments under the x-axis, but I do not get the end each labels to draw segments.

like image 400
Akira Takenaka Avatar asked Dec 09 '25 08:12

Akira Takenaka


1 Answers

It's not necessarily pretty, since it's mostly hard-coded, but it might give a general idea and be a start. The idea is to gsub the the text and thus coat the segments with white ASCII boxes.

cls <- c('#8E83C0', '#7F004B')

png('foo.png', 600, 500)

par(mar=c(17, 5, 4, 6))

## barplot
b <- barplot(M, beside=TRUE, col=cls, log='y', ylim=c(0.1, 100), yaxt='n', border=cls, ylab='Percent of genes')
cb <- colMeans(b)
axis(side=1, at=cb, labels=FALSE, srt=45, tck=0)
axis(side=2, at=c(.1, 1, 10, 100), las=2)
axis(side=4, at=c(.1, 1, 10, 100), labels=c(.1, 1, 10, 100)*200, las=2)
mtext(side=4, line=3, 'Number of genes')
box()
legend('topleft', pch=15, col=cls, legend=c('Rice', 'Arabidopsis'), bty='n')

## selections
s <- matrix(c(1, 3, 4, 15, 16, 18), 2)

segments(cb[s], .08, cb[s] - 6, .0002, cex=10, xpd=TRUE)  ## draw diagonal lines
text(x=colMeans(b), y=0.08, labels=gsub('.', '\u{2588}', labs), col='white',  ## delete text background
     xpd=TRUE, srt=75, adj=1, cex=.45, font=2)
text(x=colMeans(b), y=0.08, labels=labs, xpd=TRUE, srt=75, adj=1, cex=.8)  ## add text
segments(cb[s[seq_along(s) %% 2 == 1]] - 6, .0002, cb[s[seq_along(s) %% 2 == 0]] - 6,  ## horiz. lines
         xpd=TRUE)

text(x=cb[colMeans(s)] - 4, .00015,  ## add labels
     labels=c('Cellular location', 'Molecular function', 'Biological process'), xpd=TRUE)

dev.off()

enter image description here


Data:

M <- structure(c(2.80726227650382, 2.64256293274864, 7.0102982246191, 
7.5018148086326, 25.6501702479728, 25.0712633160012, 4.68983170461288, 
4.70906566370853, 0.884917181175275, 0.937570946155162, 20.7375441378035, 
20.1152922600914, 56.670251722557, 56.2605524666463, 3.94515839835875, 
4.01074867385038, 9.16121956424095, 8.91395744490961, 26.8659730117826, 
27.8972725231774, 14.8268139881184, 15.3903380377903, 0.25900960776686, 
0.258869902594842, 29.8213455626689, 29.4996857430132, 13.5915802573365, 
14.3385613824498, 36.6442873514434, 35.5400788324485, 0.233886747548567, 
0.22457963117021, 11.5138271223363, 12.3439712437831, 10.4035068344801, 
10.2619581657598), dim = c(2L, 18L))

labs <- c('biological regulation', 'cellular process', 'detoxification', 'developmental process',
          'growth', 'homeostatic process', 'immune system process', 'localization', 'locomotion',
          'metabolic process', 'multicellular organismal process', 'negative regulation of biological process',
          'pigmentation', 'positive regulation of biological process', 'regulation of biological process',
          'reproduction', 'reproductive process', 'response to stimulus')
like image 153
jay.sf Avatar answered Dec 11 '25 23:12

jay.sf



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!