I’m trying to create a stacked Barplot where one of the Bars is way shorter than the other ones. To make the small bar more understandable I tried to add in a “zoomed In” Plot of the smaller bar, but I can’t get it to align with my bar, because my Y-Axis is categorial and I don’t know how I can place the second plot in the space between Axis and Plot. The second Plot would have to sit/start “on the X-Axis” to perfectly align both bars
I need to move the second Plot further down, but I don’t know how
This is an example code for my problem.
library(ggplot2)
library(dplyr)
Depth <- c("10-20m","10-20m","10-20m","20-40m","20-40m","20-40m","30-60m","30-60m","30-60m")
Animal <- c("Fish", "Turtle", "Shark", "Fish", "Turtle", "Shark", "Fish", "Turtle", "Shark")
Abundance <- c(10,20, 15, 500, 500, 500, 1000, 1000, 1000)
BigPlotdata <- data.frame(Depth, Animal, Abundance)
Bigplot <- ggplot(BigPlotdata, aes(fill = Animal, x = Abundance, y = Depth))+
geom_bar(position = "stack" , stat = "identity")
Zoomed <- filter(BigPlotdata, Depth == "10-20m")
ZoomIn <- ggplot(Zoomed, aes(fill = Animal, x = Abundance, y = Depth)) +
geom_bar(position = "stack", stat= "identity")+
theme(legend.position = "none",
axis.title.y = element_blank(),
axis.text.y=element_blank(),
axis.ticks.y=element_blank(),
axis.title.x = element_blank())
PlotwithZoom <- Bigplot + annotation_custom(ggplotGrob(ZoomIn), xmin = 500, xmax = 3000, ymin = "10-20m" , ymax = "20-40m")
I added the second Plot via annotations_custom()
. Is there a way to put something into the ymin argument so the second plot will sit on my x-Axis and align with the smallest bar from the bigger Plot?
Olchecker is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.