How to write labels in barplot on x-axis with duplicated names?
I am trying to make a simple barplot but i have a problem that I have duplicated names on x-axis. So when ever I am trying to write names on x-axis it does not show complete string. I have following data
x <- c(1.8405917,0.3265986,1.5723623,464.7370299,0.0000000,3.2235716,
3.1223534, 7.0999787, 1.7122258,3.2005524,3.7531266,469.4436828)
and I am using barplot
barplot(x,xlab=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC","CC/AC",
"CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"))
But it does not work. I also used
axis()
But it does not work as well.
Thanks in advanc开发者_开发技巧e.
No, xlab
is for providing a label for the entire x-axis of the plot, not for labelling the individual bars.
barplot()
takes the labels for the bars from the names of the vector plotted (or something that can be derived into a set of names).
> names(x) <- c("AA/AA", "AA/CC", "AA/AC", "AA/NC", "CC/AA", "CC/CC", "CC/AC",
+ "CC/NC", "AC/AA", "AC/CC", "AC/AC", "AC/NC")
> barplot(x)
> ## or with labels rotated, see ?par
> barplot(x, las = 2)
Edit: As @Aaron mentions, barplot()
also has a names.arg
to supply the labels for the bars. This is what ?barplot
has to say:
names.arg:
a vector of names to be plotted below each bar or group of bars. If this argument is omitted, then the names are taken from thenames
attribute ofheight
if this is a vector, or the column names if it is a matrix.
Which explains the default behaviour if names.arg
is not supplied - which is to take the names from the object plotted. Which usage is most useful for you will mainly be a matter of taste. Not having the row/column/names might speed code up slightly, but many of R's functions will take the names
attribute (or similar, e.g. row names) directly from objects so you don't have to keep providing labels for plotting/labelling of results etc.
xlab
should be names.arg
. See ?barplot
for details.
The way to use axis()
is to capture the midpoints, which is what the barplot function returns. See ?barplot
:
mids <- barplot(x, xlab="")
axis(1, at=mids, labels=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC",
"CC/AC","CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"),
las=3)
Try this:
barplot(x, cex.names=0.7,
names.arg=c("AA/AA","AA/CC","AA/AC","AA/NC","CC/AA","CC/CC","CC/AC",
"CC/NC","AC/AA","AC/CC","AC/AC","AC/NC"))
精彩评论