How to keep original x-axis after utilizing a custom scaling for the geom_rect in ggplot?

41 Views Asked by At

I am attempting to make a depth-distribution chart of abundance and biomass. The chart is centered around zero with abundance essentially acting as the 'negative' values and biomass essentially acting as the 'positive' values. Similar to what is happening here. I have this for two seasons and used a facet wrap to have the two charts all on one page. However, I am running into issues when I want to center the graph around zero while still having the bottom x-axis match up.

Essentially, my main issue is this is all happening in a for loop as I am doing this for over 200 different species and am unable to just individually indicate what the x-axis is. In order to have the graphs centered around zero, I utilized a scale per line item to have the biomass or abundance values scale up to the maximum value. That worked great and I was even able to get the original values put next geom_rect so it's got the right values. However, since I was scaling up the x-axis at the bottom has now changed.

I was wondering if there was a way to get the x-axis to show the original (an therefore actual) values while maintaining the scaling I did?

Below is my code. One thing to note is that I have it set as one individual species right now just so I could view the ggplot but the for loop is working fine otherwise!

 value <- subset(df_combined, LTU == c("Genus sp."))
  
  xmin <- abs(min(value$value))
  xmax <- abs(max(value$value))
  scale <- ifelse(xmax>xmin,
                  xmax/xmin,
                  xmin/xmax)
  scale2=xmax/xmin

  
  value <- value %>%
    mutate(value2 = case_when(
      scale2 > 1 & strip == "Abundance" ~ value * scale,
      scale2 < 1 & strip == "Biomass" ~ value * scale,
      TRUE ~ value
    ))
  
  
  
  p<- ggplot(data = value) +
    geom_rect(mapping = aes(xmin = 0, xmax = value2,
                            ymin = Min.Depth, ymax = Max.Depth, 
                            fill = factor(side))) +
    geom_text(mapping = aes(x = value2, 
                            y = (Min.Depth + Max.Depth)/2,
                            hjust = 1 - (side > 0),
                            label = paste0('(', round(abs(value), digits=2), ')'))) +
    geom_vline(xintercept = 0, linewidth = 2) +
    scale_y_continuous(breaks = c(0, 70, 450, 700, 1000, 1500),
                       labels = c('0', '70', '450', '700', '1000', '1500'),
                       expand = expansion(mult = c(0, 0)),
                       trans = 'reverse') +
    scale_x_continuous(labels=abs, position="bottom", expand = c(0.2,0))+
    coord_cartesian(ylim=c(1500,0)) +
    facet_wrap(strip ~ ., scales = "free_x",
               labeller = label_parsed) +
    facet_grid(cols = vars(Season)) +
    labs(y = 'Depth (m)') +

    scale_fill_manual(values = c('black', 'grey'),
                      breaks=c(-1, 1),
                      labels=c("Abundance (n/10,000 m^3)", "Biomass (g/10,000 m^3)")) +
    theme(panel.grid.minor = element_blank(),
          panel.grid.major.x = element_blank(),
          panel.grid.major.y= element_line(colour = "#AAAAAA", linewidth = 1,
                                           linetype = "dashed"),
          panel.background = element_blank(),
          panel.border=element_rect(color="black", fill=NA),
          # panel.spacing = unit(0, "lines"),
          plot.tag = element_text(hjust = 0, vjust = 0, face = 'bold'),
          plot.tag.position = c(0, 0),
          axis.title.x.bottom = element_blank(),
          axis.ticks.y = element_blank(),
          axis.ticks.length.x = unit(10, 'pt'),
          axis.line.x = element_line(),
          #strip.placement = 'outside',
          #strip.background = element_blank(),
          legend.position = 'right',
          text = element_text(size = 10)) +
    guides(fill= guide_legend(title = "Legend")) + 
    labs(y = "Depth (m)")

And here is the output I am getting! Note that the values are stated on the chart but the x-axis at the bottom doesn't match up for the 'Biomass' side of the graph. Values are noted so you can remake the dataframe but just let me know if you need anything else!

Thank you for any and all help!

0

There are 0 best solutions below