I am trying to make a ggplot that shows a difference in biomass (called Migrant Biomass) on the x-axis and depths (as a continuous variable) on the y-axis. I also want the bars and space between labels on the y-axis to be indicative of the actual depth of the layer sampled (there are 9 with width ranges from 50 to 250 m).
I originally tried a ggplot bar graph using geom_bar, but then I am forced to plot the y-value (depth) as a categorical value. It seems like it is not possible to plot two continuous variables (biomass and depth) as a geom_bar plot. geom_bar plot
So, I am now using geom_rect to make my bars, which allows me to specify the xmin, xmax, ymin, and ymax for each bar. This works great for showing depth as a continuous variable and specifying the width (ymin and ymax) of each bar, but I cannot figure out how to plot bars with a negative x-value (xmin) and xmax=0.
Would appreciate any help or other ideas of how to go about making a plot with positive and negative bars like this? Example of a plot I am trying to make with positive and negative bars, indicative of depth sampled.
This is the code that I was trying:
Note: xmin=WW_x_min is 0 for all biomass differences that are positive. It is a negative weight value for all differences that are negative. xmax= WW_x_max is a positive weight value for all biomass differences that are positive. It is 0 for all biomass differences that are negative.
ggplot() +
scale_x_continuous(name="Wet Weight (mg m-3)", limits=c(-600, 300)) +
scale_y_continuous(name="Depth (m)", trans = "reverse",
breaks = c(0,50,100,300,500,700,800,1000,1250,1500),
labels = c("0","50","100","300","500","700","800","1000","1250","1500")) +
geom_rect(data=DG5B_DG5C_SFComb_Wide_Simple,
mapping=aes(xmin= WW_x_min, xmax= WW_x_max, ymin=Depth_max, ymax=Depth_min),
fill="skyblue", color="black", alpha=1) +
facet_wrap(~Cruise+Site)+ labs(title="Migrant Biomass") +
theme_bw()+ theme(plot.title = element_text(hjust = 0.5)) +
theme(text=element_text(size=16), #change font size of all text
axis.text=element_text(size=16), #change font size of axis
text axis.title=element_text(size=16), #change font size of axis titles
plot.title=element_text(size=30), #change font size of plot title
legend.text=element_text(size=16), #change font size of legend text
legend.title=element_text(size=16)) + #change font size of legend title
theme(strip.text.x = element_text(size = 16)) #change front size of facet wrap text
but I get this error:
Error in -x : invalid argument to unary operator
Below is some code which mostly replicates the linked example plot in your question, providing an example of plotting positive and negative bars of varying widths. The key difference with this approach is using 2 facets to get the positive and negative parts of the plot looking nice (particularly when the scales are dramatically different). The code in your question already has a facet on
~Cruise+Site, so some changes to the code below may be needed to accomodate this.