Transform y axis in bar plot using scale_y_log10()

前端 未结 3 2177
野性不改
野性不改 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:26

    If you need bars flipped, maybe calculate your own log10(y), see example:

    library(ggplot2)
    library(dplyr)
    
    # make your own log10
    dfPlot <- df %>% 
      mutate(ymin = -log10(ymin),
             ymax = -log10(ymax),
             ymean = -log10(ymean))
    
    # then plot
    ggplot(dfPlot, aes(x = id, y = ymean, fill = var, group = var)) +
      geom_bar(position = "dodge", stat = "identity",
               width = 0.7,
               size = 0.9)+
      geom_errorbar(aes(ymin = ymin, ymax = ymax),
                    size = 0.25,   
                    width = 0.07,
                    position = position_dodge(0.7)) +
      scale_y_continuous(name = expression(-log[10](italic(ymean)))) + 
      theme_bw() 
    

提交回复
热议问题