Transform y axis in bar plot using scale_y_log10()

前端 未结 3 2174
野性不改
野性不改 2020-12-22 10:13

Using the data.frame below, I want to have a bar plot with y axis log transformed.

I got this plot

using this code

ggplot(df, aes(x=         


        
3条回答
  •  独厮守ぢ
    2020-12-22 10:38

    As @Miff has written bars are generally not useful on a log scale. With barplots, we compare the height of the bars to one another. To do this, we need a fixed point from which to compare, usually 0, but log(0) is negative infinity.

    So, I would strongly suggest that you consider using geom_point() instead of geom_bar(). I.e.,

    ggplot(df, aes(x=id, y=ymean , color=var)) +
      geom_point(position=position_dodge(.7))+
      scale_y_log10("y",
                    breaks = trans_breaks("log10", function(x) 10^x),
                    labels = trans_format("log10", math_format(10^.x)))+
      geom_errorbar(aes(ymin=ymin,ymax=ymax),
                    size=.25,   
                    width=.07,
                    position=position_dodge(.7))+
      theme_bw()
    

    If you really, really want bars, then you should use geom_rect instead of geom_bar and set your own baseline. That is, the baseline for geom_bar is zero but you will have to invent a new baseline in a log scale. Your Plot 1 seems to use 10^-7.

    This can be accomplished with the following, but again, I consider this a really bad idea.

    ggplot(df, aes(xmin=as.numeric(id)-.4,xmax=as.numeric(id)+.4, x=id, ymin=10E-7, ymax=ymean, fill=var)) +
      geom_rect(position=position_dodge(.8))+
      scale_y_log10("y",
                    breaks = trans_breaks("log10", function(x) 10^x),
                    labels = trans_format("log10", math_format(10^.x)))+
      geom_errorbar(aes(ymin=ymin,ymax=ymax),
                    size=.25,   
                    width=.07,
                    position=position_dodge(.8))+
      theme_bw()
    

提交回复
热议问题