Search code examples
rlabelbar-chart

R: How can I make a barplot with labels parallel (horizontal) to bars


In barplot, is it possible to make the labels parallel to the bars?

Suppose we have the data frame called "data".

              Page   PV UniquePgv
1 /photos/upcoming 5295      2733
2                / 4821      2996
3          /search 1201       605
4       /my_photos  827       340
5   /photos/circle  732       482

I want to make a barplot of PV with the Page column as the label.

names <-data$Page
barplot(data$PV,main="Page Views", horiz=TRUE,names.arg=names)

Which Produces:

enter image description here

The name of each bar is vertical, while the bars are horizontal.

How can I make the labels display horizontally and parallel with the bars? If it is not possible, I am open to suggestions for other ways to plot this information.


Solution

  • You can use the las graphics parameter. However, if you do that, the names will run off the end of the window, so you need to change the margins. For example:

    par(mai=c(1,2,1,1))
    barplot(data$PV,main="Page Views", horiz=TRUE,names.arg=names,las=1)
    

    enter image description here