使用条形图分组x轴

时间:2015-03-18 10:59:49

标签: r bar-chart

这是我的数据(data.txt):

    S1    S2   S3    S4   S5   S6    S7   S8    S9   S10  S11
T1  1.6   17   2.2   1    4.4  23.8  0.2  2.8   3.2  0.4  18.2
T2  0     3.5  1.5   0.5  4    3     6.5  21.6  0.5  1    18.6
T3  0     0    0     4.2  4.2  41.7  4.2  0     8.3  4.2  4.2
T4  4     0    0     0    0    12    4    4     4    16   20
T5  31.3  6.3  12.5  0    0    12.5  0    0     6.3  0    0
T6  12.5  0    0     18.8 0    12.5  0    0     6.3  6.3  0

这是我的代码:

data =  read.table("C:/Users/Irwan/Desktop/graph_plot/data.txt", header = TRUE, sep = "\t", row.names = 1)

attach(data)

column = colnames(data)
rows = rownames(data)

x = barplot(as.matrix(data), xaxt = "n", beside = TRUE, legend = rows, ylim = c(0, 50), density = c(30,30,30,30,70,100), angle = c(0,45,90,135,0,0))

label = as.vector(column)

text(x = x + 1.20, y = -1.25, xpd = TRUE, cex = 0.6, srt = 90, label, pos = 2)

不幸的是,我无法正确分组x轴标签。

我尝试了很多解决方案,这是我到目前为止所得到的。任何人都可以指导我如何在每6个条形图上对结果进行分组(例如:前6个条形的S1)。请注意我在R中不是很好,我只能使用预先安装的R包,因为我不能安装任何新的包,原因不明。谢谢。

已更新

我对我的代码进行了一些改动,以便重现它。我的新代码:

y = matrix (1:15, ncol = 5)

x = barplot(as.matrix(y), xaxt = "n", beside = TRUE, legend = c("T1","T2","T3"), ylim = c(0,30), density = c(30,30,30), angle = c(0,45,90))

label = rep(c("S1","S2","S3","S4","S5"), each = 3)

text(x = x + 0.4, y = -1.25, xpd = TRUE, cex = 0.6, srt = 90, label, pos = 2)

1 个答案:

答案 0 :(得分:0)

如果我读了你的正确,用以下代码替换你的标签:

label = rep(column,each=6)

应该提供所需的结果。

编辑;

如果每组只需要一个标签

label = as.vector(rbind("",paste("S",1:5,sep=""),""))