I have a data set with a big negative value, so I used gg.gap
package to make a discontinuous y-axis. The code worked well but after adding the function gg.gap
the legend was deleted.
I tried adding a legend using the add.legend
argument but it didn't work.
Am I doing something wrong?
Here is a sample code
df<- data.frame(
var1 = c(3.2,5.1,-25000,3.9,4.6,3.1),
var2 = c(2020,2021,2022,2020,2021,2022),
var3 = c("c","c","c","t","t","t")
)
test<-ggplot(df, aes(x = as.factor(var2), y = var1, fill = var3)) +
geom_col(position = position_dodge(width = 0.9)) +
scale_fill_viridis_d() + # Use the color-blind-friendly viridis palette
labs(
title = "Grouped Bar Plot",
x = "Year",
y = "Variable 1",
fill = "Group"
) +
theme_minimal() +
theme(legend.position = "bottom")
gg.gap(plot=test,segments=c(-24900,0),ylim= c(-25000,10))