Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make an R barplot with a log y-axis scale?

This should be a simple question... I'm just trying to make a barplot from a vector in R, but want the values to be shown on a log scale, with y-axis tick marks and labelling. I can make the normal barplot just fine, but when I try to use log or labelling, things go south.

Here is my current code:

samples <- c(10,2,5,1,2,2,10,20,150,23,250,2,1,500)
barplot(samples)

Ok, this works. Then I try to use the log="" function defined in the barplot manual, and it never works. Here are some stupid attempts I have tried:

barplot(samples, log="yes")
barplot(samples, log="TRUE")
barplot(log=samples)

Can someone please help me out here? Also, the labelling would be great too. Thanks!

like image 288
jake9115 Avatar asked Dec 19 '13 14:12

jake9115


People also ask

How do I change the Y axis scale in a Barplot in R?

To change the axis scales on a plot in base R Language, we can use the xlim() and ylim() functions. The xlim() and ylim() functions are convenience functions that set the limit of the x-axis and y-axis respectively.

How do I change the Y axis to log scales ggplot2?

This can be done easily using the ggplot2 functions scale_x_continuous() and scale_y_continuous(), which make it possible to set log2 or log10 axis scale. An other possibility is the function scale_x_log10() and scale_y_log10(), which transform, respectively, the x and y axis scales into a log scale: base 10.

How do I change the axis on a Barplot in R?

Barplots in R programming language can be created using the barplot() method. It takes as input a matrix or vector of values. The bar heights are equivalent to the values contained in the vector.


1 Answers

The log argument wants a one- or two-character string specifying which axes should be logarithmic. No, it doesn't make any sense for the x-axis of a barplot to be logarithmic, but this is a generic mechanism used by all of "base" graphics - see ?plot.default for details.

So what you want is

barplot(samples, log="y")

I can't help you with tick marks and labeling, I'm afraid, I threw over base graphics for ggplot years ago and never looked back.

like image 50
zwol Avatar answered Oct 24 '22 08:10

zwol