ggplot2 0.9.1: Logarithmic y-axis scale with barch

2019-08-19 08:10发布

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

标签: r ggplot2
1条回答
孤傲高冷的网名
2楼-- · 2019-08-19 08:35

if you want to do a log 10 scale use

p + scale_y_log10()
查看更多
登录 后发表回答