0

I created a bar plot to show differences in water accumulation on different sites and layers. Because one value is way higher than the other ones I want to set the y-axis on log10 scale. It all works but the result looks rather unintuitive. is it possible to set the limit of the y-axis to 0 so the bar with the value 0.2 is not going downwards?

Here is the code I used:

p2 <- ggplot(data_summary2, aes(x= Site, y= small_mean, fill= Depth, Color= Depth))+
  geom_bar(stat = "identity", position = "dodge", alpha=1)+
  geom_errorbar(aes(ymin= small_mean - sd, ymax= small_mean + sd),
                position = position_dodge(0.9),width=0.25,   alpha= 0.6)+
  scale_fill_brewer(palette = "Greens")+
  geom_text(aes(label=small_mean),position=position_dodge(width=0.9), vjust=-0.25, hjust= -0.1, size= 3)+
  #geom_text(aes(label= Tukey), position= position_dodge(0.9), size=3, vjust=-0.8, hjust= -0.5, color= "gray25")+
  theme_bw()+
  theme(legend.position = c(0.2, 0.9),legend.direction = "horizontal")+
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())+
  labs(fill="Depth [cm]")+
  theme(axis.text.x = element_text(angle = 25, size =9, vjust = 1, hjust=1))+
  scale_x_discrete(labels= c( "Site 1\n(Hibiscus tillaceus)","Site 2 \n(Ceiba pentandra)","Site 3 \n(Clitoria fairchildiana)","Site 4 \n(Pachira aquatica)"))+
  #theme(legend.position = c(0.85, 0.7))+
  labs(x= "Sites\n(Type of Tree)", y= "µg Deep-Water/ g rhizosphere soil", title = "Average microbial Deep-water incorporation per Site", subtitle = "Changes over Time and Depth")+
  facet_grid(.~Time, scale = "free")
p2 + scale_y_continuous(trans = "log10")

This is what the plot looks like:

enter image description here

user438383
  • 5,716
  • 8
  • 28
  • 43
  • 2
    Welcome to SO, Umberto_Leckerbaer! Please make this question *reproducible*. This includes sample code you've attempted (including listing non-base R packages, and any errors/warnings received), sample *unambiguous* data (e.g., `data.frame(x=...,y=...)` or the output from `dput(head(x))`), and intended output given that input. Refs: https://stackoverflow.com/q/5963269, [mcve], and https://stackoverflow.com/tags/r/info. – r2evans Dec 30 '21 at 19:43
  • Try: `scale_y_continuous(trans = "log", breaks = c(0, 1, 10, 100), limits = c(NA, 100))` – TarJae Dec 30 '21 at 19:44
  • 3
    What are fractions below 1 in the log scale? These *are* negative values, so I find this actually more intuitive than when trying to show them as positive values. Also, you show paired data (measurement at days 3 and 10), maybe consider a scatter plot instead. Will show the change much better. Would love to show how, but impossible without some fake data. – tjebo Dec 31 '21 at 11:52
  • Please provide enough code so others can better understand or reproduce the problem. – Community Jan 07 '22 at 13:48

1 Answers1

1

On a log scale there is no 0, therefore the only sensible place for bars to start from is y = 10^0 or 1.

However you can create a pseudolog scale using scales::pseudo_log_trans to get 0 included on the axis so all the bars go the same direciton. I'm borrowing from this answer. NOTE it's important to add 0 to the breaks to make it clear that this is a pseudolog scale. Compare the two plots below:

library(tidyverse)
library(scales)

# make up data with distribution positive values above and below 1
d <- tibble(grp = LETTERS[1:5],
            val = 10^(-2:2))

# normal plot with true log scale doesn't contain 0
d %>%
  ggplot(aes(x = grp, y = val, fill = grp)) +
  geom_col() +
  ggtitle("On True Log Scale Bars Start at y = 1") +
  scale_y_log10() # or if you prefer: scale_y_continuous(trans = "log10")

# set range of 'linear' portion of pseudolog scale
sigma <- min(d$val)
  
# plot on pseudolog to get all bars to extend to 0
d %>%
  ggplot(aes(x = grp, y = val, fill = grp)) +
  geom_col() +
  ggtitle("On Pseudolog Scale Bars Start at y = 0") +
  scale_y_continuous(
    trans = pseudo_log_trans(base = 10, sigma = sigma),
    breaks = c(0, 10^(-2:2)),
    labels = label_number(accuracy = 0.01)
  )

Created on 2021-12-30 by the reprex package (v2.0.1)

Dan Adams
  • 4,971
  • 9
  • 28