0
votes

I am trying to convert the y-axis of a bar-chart to a logarithmic scale (I.e. logarithmic distance between each ticks).

Some dummy data:

DF <- data.frame(num=c(1,2,3),label=c("a","b","c"))

I've tried the following examples:

p <- ggplot(data=DF,aes(x=label,y=num)) + geom_bar() +
  scale_y_continuous(trans = 'log10',
                     breaks=trans_breaks("log10",function(x) 10^x),
                     labels=trans_format("log10",math_format(10^.x)))

This only log transforms labels, but not ticks:

p <- ggplot(data=DF,aes(x=label,y=num)) + geom_bar() + coord_trans(y="log10")

This doesn't plot anything at all:

p <- ggplot(data=DF,aes(x=label,y=num),y="log")

No luck either

I have also read the 0.9 transition guide , but that does not seem to work either.

EDIT:

I forgot to include one example I tried:

p <- ggplot(data=DF,aes(x=label,y=num)) + geom_bar() + scale_y_log10()

Which produces the following warning:

Warning message: In pretty(trans(x), n, ...) : NaNs produced

1
Using a log-scale for a bar chart is a bad idea, since bar measure the distance from zero. Use a dot plot instead. perceptualedge.com/articles/b-eye/dot_plots.pdfRichie Cotton
I tried using a dot plot, and it worked with the dummy data. But when I switched to the actual data, which ranges from 20865 to 127135230, the y-axis label only displays 10^5demodw
Did you try a dot plot with a logarithmic y scale? It should give you more axis label with that sort of data range. If not, you can manually specify the labels arg in scale_y_log10.Richie Cotton

1 Answers

8
votes

if you want to do a log 10 scale use

p + scale_y_log10()