Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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.

like image 606
CompChemist Avatar asked Oct 17 '13 21:10

CompChemist


People also ask

How do I label each bar in 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.

What is a sideways Barchart called?

Horizontal Bar Chart Horizontal bar charts are often used to represent comparisons between nominal variables. With this tool, you can display long data labels using horizontal rectangles and still have enough room for textual information.

How do you make a horizontal barplot in ggplot2?

To create a horizontal bar chart using ggplot2 package, we need to use coord_flip() function along with the geom_bar and to add the labels geom_text function is used. These two functions of ggplot2 provides enough aesthetic characteristics to create the horizontal bar chart and put the labels at inside end of the bars.


1 Answers

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

like image 175
mrip Avatar answered Oct 18 '22 14:10

mrip