0

This is my data (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

And this is my code:

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)

Unfortunately, I'm not able to group x-axis labels correctly.

I've try numbers of solution and this is what I get so far. Can anyone guide me on how to group the result on every 6 bars (example: S1 for the first 6 bars). Please bear in my that I'm not very good in R and I only able to use pre-installed R package as I cannot install any new package due to unknown reason. Thank you.

UPDATED

I have change little bit my code so that it might be easy to reproduce. My new code:

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)
irwan
  • 51
  • 6

1 Answers1

0

If i read you correctly, replacing your label with:

label = rep(column,each=6)

Should provide the desired result.

Edit;

If only one label per group is desired

label = as.vector(rbind("",paste("S",1:5,sep=""),""))
Pewi
  • 1,134
  • 13
  • 14
  • Thanks for the solution but it display same label in every bar char. Can it be displaying only one label per group? (example: S1 for first 3 bars, S2 for second 3 bars. Not S1,S1,S1 for first 3 bars and S2,S2,S2 for second 3 bars). Sorry if I can't visualise it good enough. – irwan Mar 19 '15 at 04:23
  • Ok i think i get it. Think of the label as a sequence of 15 character strings (one per bar) that needs to be filled with something. If we create this sequence so that every other element is an empty string `""` no label will be printed for these bars and we will end up with one label per bar. Answer above is edited to reflect this. – Pewi Mar 19 '15 at 08:11