How to make R barplot with y axis scale?

It should be a simple question ... I'm just trying to make a barcode from a vector to R, but I want the values โ€‹โ€‹displayed in a logarithmic scale with marks and labels on the y axis. I can make a regular barcode just fine, but when I try to use a magazine or label, everything goes 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, that works. Then I try to use the log="" function defined in the barplot manual and it never works. Here are some dumb attempts I tried:

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

Can someone please help me here? In addition, the marking will be excellent. Thanks!

+8
r label axis bar-chart
source share
2 answers

The log argument requires a one or two character string specifying which axes should be logarithmic. No, for the x-axis of the line drawing there is no sense logarithmically, but this is the general mechanism used by all the "basic" graphs - for more details see ?plot.default .

So you want

 barplot(samples, log="y") 

I can not help you with marks and tags, I'm afraid I threw the basic graphics for ggplot years ago and never looked back.

+10
source share

This should start with ggplot2 .

 d<-data.frame(samples) ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) + geom_bar(stat="identity") + scale_y_log10() 

Inside the scale_y_log10() function, you can define breaks, labels, etc. Similarly, you can mark the x axis. for example

 ggplot(data=d, aes(x=factor(1:length(samples)),y=samples)) + geom_bar(stat="identity") + scale_y_log10(breaks=c(1,5,10,50,100,500,1000), labels=c(rep("label",7))) + scale_x_discrete(labels=samples) 
+2
source share

All Articles