Bar chart with logarithmic scale and user breaks

I am trying to create a histogram in R with a logarithmic scale for y. I am currently doing:

hist(mydata$V3, breaks=c(0,1,2,3,4,5,25)) 

This gives me a histogram, but the density from 0 to 1 is so high (about a million difference in values) that you can hardly distinguish any of the other bars.

Then I tried to do:

 mydata_hist <- hist(mydata$V3, breaks=c(0,1,2,3,4,5,25), plot=FALSE) plot(rpd_hist$counts, log="xy", pch=20, col="blue") 

This gives me what I want, but below it shows me the values ​​1-6, not 0, 1, 2, 3, 4, 5, 25. It also shows the data as points, not bars. barplot works, but then I don't get any bottom axis.

+63
r logarithm histogram
Aug 07 '09 at 15:02
source share
7 answers

A histogram is an estimate of the density of a poor person. Note that when you call hist() using the default arguments, you get non-probability frequencies - add ,prob=TRUE to the call if you want probabilities.

As for the log axis issue, do not use 'x' unless you want to convert the x axis:

 plot(mydata_hist$count, log="y", type='h', lwd=10, lend=2) 

gets bars on a logarithm scale - the look is still slightly different, but it can probably be changed.

Finally, you can also do hist(log(x), ...) to get a histogram of the log of your data.

+52
Aug 07 '09 at 15:14
source share

Another option is to use the ggplot2 package.

 ggplot(mydata, aes(x = V3)) + geom_histogram() + scale_x_log10() 
+33
Aug 09 '09 at 11:50
source share

Dirk's answer is excellent. If you want to look like hist does, you can also try the following:

 buckets <- c(0,1,2,3,4,5,25) mydata_hist <- hist(mydata$V3, breaks=buckets, plot=FALSE) bp <- barplot(mydata_hist$count, log="y", col="white", names.arg=buckets) text(bp, mydata_hist$counts, labels=mydata_hist$counts, pos=1) 

The last line is optional; it only adds value labels below the top of each panel. This may be useful for log scale graphs, but may also be omitted.

I also pass the main , xlab and ylab to provide the plot title, x-axis label, and y-axis label.

+8
Apr 10 2018-11-11T00:
source share

It is not clear from your question whether you want a registered x axis or a registered y axis. The recorded y axis is not a good idea when using bars, because they are tied to zero, which becomes negative infinity during registration. You can work around this problem using a frequency polygon or a density graph.

+7
Aug 10 '09 at 23:55
source share

Run the hist () function without creating a graph, log conversion of the counters, and then draw a shape.

 hist.data = hist(my.data, plot=F) hist.data$counts = log(hist.data$counts, 2) plot(hist.data) 

It should look the same as a regular histogram, but the y axis will have a frequency of log2.

+4
Jul 18 '13 at 15:11
source share

I put together a function that behaves the same with history in the default case, but accepts a log argument. He uses several tricks from other posters, but adds some of his own. hist(x) and myhist(x) look the same.

The original problem will be solved with:

 myhist(mydata$V3, breaks=c(0,1,2,3,4,5,25), log="xy") 

Function:

 myhist <- function(x, ..., breaks="Sturges", main = paste("Histogram of", xname), xlab = xname, ylab = "Frequency") { xname = paste(deparse(substitute(x), 500), collapse="\n") h = hist(x, breaks=breaks, plot=FALSE) plot(h$breaks, c(NA,h$counts), type='S', main=main, xlab=xlab, ylab=ylab, axes=FALSE, ...) axis(1) axis(2) lines(h$breaks, c(h$counts,NA), type='s') lines(h$breaks, c(NA,h$counts), type='h') lines(h$breaks, c(h$counts,NA), type='h') lines(h$breaks, rep(0,length(h$breaks)), type='S') invisible(h) } 

Exercise for the reader: unfortunately, not everything that works with history works with my mythologist. This should be fixed with less effort.

+2
Dec 02 '11 at 13:15
source share

Here's a pretty ggplot2 solution:

 library(ggplot2) library(scales) # makes pretty labels on the x-axis breaks=c(0,1,2,3,4,5,25) ggplot(mydata,aes(x = V3)) + geom_histogram(breaks = log10(breaks)) + scale_x_log10( breaks = breaks, labels = scales::trans_format("log10", scales::math_format(10^.x)) ) 

Note that to set gaps in geom_histogram, they had to be converted to work with scale_x_log10

+1
Sep 30 '17 at 22:07
source share



All Articles