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

这是一个Wego Web工具的结果,但我想在R中做.

我知道它在x轴下添加线段,但我没有得到每个标签的末端来绘制线段.

推荐答案

它不一定很漂亮,因为它大多是硬编码的,但它可能会给出一个大致的 idea ,并成为一个开始.其 idea 是在文本上涂上gsub个,从而在segments上涂上白色的ASCII框.

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')

R相关问答推荐

在ggplot的注释表格中突出显示最大值

更改绘图上的x轴断点,而不影响风险?

编辑文件后编辑RhandsonTable

获取列中值更改的行号

如何在观测缺失的地方添加零

以更少间隔的较小表中的聚合离散频率表

如何同时从多个列表中获取名字?

使用Facet_WRAP时更改框图中线的 colored颜色

根据类别合并(汇总)某些行

R Read.table函数无法对制表符分隔的数据正常工作

使用带有OR条件的grepl过滤字符串

如何从容器函数中提取conf并添加到ggplot2中?

根据1个变量绘制 colored颜色 发散的 map ,由另一个变量绘制饱和度,ggplot2不工作

更改STAT_VALLES/STAT_PEAKS中的箭头线宽/大小

如何将Which()函数用于管道%>;%

R仅当存在列时才发生变异

将文本批注减少到gglot的y轴上的单个值

创建两个变量组合的索引矩阵

使用nls()函数的非线性模型的半正态图

通过分析特定列中的字符串在数据框中创建新的行和列