2

I am trying to create a stacked bargraph with a logged y-axis. I don't want to transform my entire dataset, just the y-axis so that I can spread out some of my smaller bars. I am trying to use coord_trans. Whenever I try a log transformation, I get an error message for every single individual bar saying "Transformation introduced infinite values in y-axis". If I use the same code with a sqrt transformation, everything works fine. So it must be something with the log transformation. Have you encountered this problem before? I have included the code for the plot I am using below.

library(ggplot2)    
dput(Bef_bio)
structure(list(Treat = structure(c(1L, 2L, 3L, 1L, 2L, 3L, 1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("Con", 
"N", "P"), class = "factor"), Functional_Group = structure(c(5L, 
5L, 5L, 4L, 4L, 4L, 1L, 1L, 1L, 7L, 7L, 7L, 6L, 6L, 6L, 3L, 3L, 
3L, 2L, 2L, 2L), .Label = c("One", "Two", "Three", "Four", "Five", 
"Six", "Seven"), class = "factor"), BV_mm3L = c(0.284504136, 
0.414444186666667, 3.75290346275, 0, 0, 0, 0.118510033666667, 
0.0218816683333333, 17.942747818875, 0, 0, 0, 0.668984741333333, 
0.436479357333333, 1.310577009625, 3.16931740733333, 1.5116585285, 
2.07553807775, 0.188271552, 0.165778386333333, 10.2304435985)), row.names = 
c(NA, 
-21L), class = "data.frame")

plot<-ggplot()+geom_bar(aes(y=BV_mm3L, x=Treat, fill=Functional_Group), colour="black", data=Bef_bio, stat="identity"))
plot
plot<-plot+coord_trans(y="log10")
plot

I apologize that I do not know how to make a reproducible dataset here.

TylerH
  • 20,799
  • 66
  • 75
  • 101
Jacob
  • 21
  • 3
  • Here the link to know how to make a reproducible example: https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example. Have you try `scale_y_log10` function ? – dc37 Feb 15 '20 at 16:44
  • There's guidance [here](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) on making reproducible examples. The issue is probably going to depend on your data—are there NAs or 0s? Here's a hint from high school math class: what's log10(0)? – camille Feb 15 '20 at 16:44
  • As others hinted, bars that start at 0 and logs don't tend to work well. Nice discussion here: https://stackoverflow.com/a/9507037/2461552 – aosmith Feb 15 '20 at 16:46
  • Thank you for your help! I have tried using scale_y_log10, but that transforms my data and from what have read, I should not do this to a stacked barplot. If I set limits to my y-axis starting at 1, my scale looks correct but then none of my bars are plotted. I have got this to work in Excel with bars that start at 0. I will try posting a reproducible example momentarily. Thank you for the help! – Jacob Feb 15 '20 at 17:25
  • 3
    The `pseudolog` transform might be better here. It's roughly linear in the area around -1 to +1, but then shifts to a log scale. Try `scale_y_continuous(trans = "pseudo_log")` – Jon Spring Feb 15 '20 at 17:51
  • Thanks Jon, that worked perfectly when I set position_dodge(). For whatever reason though, when I tried to stack the bars, the y-axis shifted to the thousands when it should have been in double digits. Is there any reason why it wouldn't work for a stacked bar graph? – Jacob Feb 15 '20 at 18:26

0 Answers0