-1

I'm new using R and ggplot library, and I'm trying to build a bar chart with an error bar, like this:

pd <- position_dodge(0.80)
ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd)
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1) 
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)") 

enter image description here

The only thing is that I would like logscale the y-axis, so I tried using the scale_y_log10 function:

ggplot(aqp1) + geom_bar( aes(x=Type, y=Error, fill=Set, colour=Set, group=Set), width=0.75, stat="identity", alpha=1, position=pd) 
+ geom_errorbar( aes(x=Type, ymin=Error,ymax=Max.Error, colour=NULL, group=Set), position=pd, width=0.1, colour="black", alpha=1, size=0.1) 
+ theme_light() + labs(title="", x="Sizes", y="Relative error (%)") 
+ scale_y_log10(breaks = trans_breaks("log10", function(x) 10^x), labels = trans_format("log10", math_format(10^.x)))

But I have a bizarre result, where the errorbar has another scale, different from the bar chart.

enter image description here

How can I fix this? I tried using + ylim(10^-2, 10^1) but it doesn't work.

fingerprints
  • 2,751
  • 1
  • 25
  • 45
  • Does this help: https://stackoverflow.com/a/9507037/8583393 You might try `scale_y_sqrt()` – markus Jul 22 '18 at 20:25
  • thanks, but I have the same problem, the errorbar get overlapped and it has its own scale. – fingerprints Jul 22 '18 at 20:50
  • 6
    A bar chart doesn't work well on a log scale, because the bars' baseline is set to 1 (10^0) instead of zero. You're probably better off plotting the values as points, rather than bars. Also, to set the limits, `+ ylim(10^-2, 10^1)` will override the previous `scale_y_log10()`. Instead, add the argument `limits=c(10^-2, 10^1)` inside `scale_y_log10`. Note however, that if you set the limits to be less than the range of the data, data outside the limits will be excluded. To avoid this, don't use the `limits` argument in `scale_y_log10`. Instead, add `+ coord_cartesian(ylim=c(10^-2, 10^1))` – eipi10 Jul 22 '18 at 21:02
  • 1
    Please include some or all of the data in `aqp1` to help people answer the question. – neilfws Jul 23 '18 at 00:34

1 Answers1

0

I just want to quote @eipi10

A bar chart doesn't work well on a log scale, because the bars' baseline is set to 1 (10^0) instead of zero

This explains, why the images go wrong.

fingerprints
  • 2,751
  • 1
  • 25
  • 45