Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get x-axis labels to show in R Barplot? [duplicate]

Tags:

r

Probably pretty simple but I am a newb to R (and to stack)...

Can't seem to get the car names to show on the x-axis of my barplot.

I tried pasting in the example given in the "How to display all x labels in R barplot?" Question but that didn't work

My code is below. Does that code work for anybody else?

#plot of efficiency of 4 cylinder cars
#get 4cylinder cars seperate
fourcyl <- subset(mtcars, cyl == "4")
#barplot in descending order... need to add in car names.
barplot(fourcyl$mpg[order(fourcyl$mpg, decreasing = TRUE)], 
        horiz=FALSE,
        ylab = "Miles per Gallon",
        main = "Efficiency for 4 cylinder vehicles",
        ylim = c(0,35))
like image 458
Julian Tagell Avatar asked Oct 31 '22 21:10

Julian Tagell


1 Answers

@Pascal's comment links to two possible solutions, but the bottom line is that you need to add the car names manually.

To know which car names to use takes a first step: if you look at mtcars, you'll see that they don't appear under a column header, meaning they are the row names. To get at them, simply:

carnames <- rownames(fourcyl)[ order(fourcyl$mpg, decreasing=TRUE) ]

From here, you need to know how and where to add them. Perhaps the first place many people look is to axis, where you'd do something like:

axis(side=1, at=1:length(carnames), labels=carnames)

But you'd be disappointed on at least two accounts: first, you don't see all of the names, since axis courteously ensures they don't overlap by omitting some; second, the ones that do show are not aligned properly under the corresponding vertical bar.

To fix the first, you can try rotating the text. You could use las (see help(par)) and do something like:

axis(side=1, at=1:length(carnames), labels=carnames, las=2)

But again you'll be a little disappointed in that many of the names will run over the default bottom margin (and disappear). You can fix with this a preceding par(mar=...) (again, see the help there and play with it some to find the right parameters), but there are solutions that provide slightly better methods (aesthetically), two of which are mentioned in @Pascal's link (really, go there).

The other problem -- where to put the labels -- is resolved by reading help(barplot) and noticing that the return value from barplot(...) is a matrix providing the mid-point of each of the bars. Odd, perhaps, but it is what it is (and it has a good reason, somewhere). So, capture this and you'll be nearly home-free:

bp <- barplot(fourcyl$mpg[order(fourcyl$mpg, decreasing = TRUE)], 
              horiz=FALSE, ylab = "Miles per Gallon",
              main = "Efficiency for 4 cylinder vehicles",
              ylim = c(0,35))

Now, to copy one of the link's suggestions, try:

text(x=bp[,1], y=-1, adj=c(1, 1), carnames, cex=0.8, srt=45, xpd=TRUE)

(No need for the axis command, just bp <- barplot(...), carnames <- ..., and text(...).)

like image 193
r2evans Avatar answered Nov 14 '22 04:11

r2evans