Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to write labels in barplot on x-axis with duplicated names?

Tags:

plot

r

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 advance.

like image 897
Iftikhar Avatar asked Sep 28 '11 18:09

Iftikhar


People also ask

Which function is used to add label of the X-axis?

Description. xlabel( txt ) labels the x-axis of the current axes or standalone visualization. Reissuing the xlabel command replaces the old label with the new label. xlabel( target , txt ) adds the label to the specified target object.

How do you label the axes?

In charts, axis labels are shown below the horizontal (also known as category) axis, next to the vertical (also known as value) axis, and, in a 3-D chart, next to the depth axis. The chart uses text from your source data for axis labels. To change the label, you can change the text in the source data.

How do I add a data label to a Barplot in R?

To add labels on top of each bar in Barplot in R we use the geom_text() function of the ggplot2 package. Parameters: value: value field of which labels have to display. nudge_y: distance shift in the vertical direction for the label.

How do I change axis labels in Matplotlib?

Matplotlib x-axis label size To change the size, the fontsize parameter is passed to the xlabel() method. The label text is set by xlabel, while the font size is specified by fontsize.


1 Answers

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 the names attribute of height 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.

like image 83
Gavin Simpson Avatar answered Sep 25 '22 10:09

Gavin Simpson