plot - How to merge column labels in barplot in R (a stacked barplot)? -
my data in r this:
1-12 1-12 1-15 1-15 1-20 1-20 2-6 2-6 3-1-1 3-1-1 3-1 3-1 3-2 3-2 3-3 3-3 n 0 0 14 0 17 0 9 0 27 0 9 0 13 0 33 0 p 0 0 0 12 0 12 0 5 0 13 0 6 0 0 0 9 f 0 0 0 0 0 0 0 2 0 14 0 0 0 6 0 20
by dput(data) result in r is:
structure(c(0l, 0l, 0l, 0l, 0l, 0l, 14l, 0l, 0l, 0l, 12l, 0l, 17l, 0l, 0l, 0l, 12l, 0l, 9l, 0l, 0l, 0l, 5l, 2l, 27l, 0l, 0l, 0l, 13l, 14l, 9l, 0l, 0l, 0l, 6l, 0l, 13l, 0l, 0l, 0l, 0l, 6l, 33l, 0l, 0l, 0l, 9l, 20l), .dim = c(3l, 16l), .dimnames = list( c("n", "p", "f"), c("1-12", "1-12", "1-15", "1-15", "1-20", "1-20", "2-6", "2-6", "3-1-1", "3-1-1", "3-1", "3-1", "3-2", "3-2", "3-3", "3-3")))
and code is:
barplot(data,space=c(1,0.25),legend=rownames(data),col=c('white','black','grey'),las=2)
it looks normal bar plot, each column has label @ bottom...
but, there many labels in x-axis, , want merge names of 2 columns one(since have same name), i.e, in middle of first 2 columns, there 1 label "1-12" @ bottom, there 8 labels in total. how can make change?
many thanks!
if wish have centred labels under each group, like:
# suppress x-axis , save original plot's bar locations bp <- barplot(data,space=c(1,0.25),legend=rownames(data), col=c('white','black','grey'),las=2, xaxt="n") # draw new axis using these values axis(1,at=rowmeans(matrix(bp,ncol=2,byrow=true)), labels=unique(colnames(data)),lty=0)
ignoring overlapping legend of course... can fixed placing better using legend.args
in original barplot
call
Comments
Post a Comment