I am actually trying to build a graph showing the fiscal space of a country, so that it looks like But for now it looks like
I don't know how to add the transparent triangle like in the model. I tried with several functions but I didn't manage to succeed.
I used:
geom_area(x = c(-15, 15, 15), c(-15, -15, 15))
geom_polygon(aes(x = c(-15, 15, 15),y = c(-15, -15, 15), col = 'grey'))
as well as
triangle <- tibble(x = -15:15)
geom_ribbon(data = triangle, aes(x, x, ymin=-15, ymax=15), alpha = 0.4)
But none of these functions worked. Here is a preview of my code:
df %>%
ggplot(aes(CAB, gvtbal, xmin=-15, xmax=15, ymin=-15, ymax=15, colour=year)) +
geom_point() +
geom_label_repel(aes(label= year),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50',
max.overlaps = 50,
force= 85) +
# Add lines 45° line for private sector, add x and y axis for CAB and gvtbal
geom_abline(color='grey') +
geom_vline(xintercept = 0, color='grey')+
geom_hline(yintercept = 0, color='grey')
I would like the shaded triangle to cover the area below the geom_abline
function.
Thank you.
You could try with annotate
.
In the absence of a reproducible data I've mocked up a dataset.
I'm assuming you are OK with the labelling of the axis and title as the question is specifically about the shaded triangle.
updated to reflect @KSkoczek's suggestion to place the shaded triangle in an earlier layer so labels are placed on top of the shading.
library(ggplot2)
library(ggrepel)
set.seed(123)
df <- data.frame(CAB = runif(10, 0, 15),
gvtbal = runif(10, -3, 1),
year = 2001:2010)
df |>
ggplot(aes(CAB, gvtbal, xmin=-15, xmax=15, ymin=-15, ymax=15, colour=year)) +
geom_point() +
annotate(geom = "polygon", x = c(-Inf, Inf, Inf), y = c(-Inf, Inf, -Inf), fill = "blue", alpha = 0.1 )+
geom_label_repel(aes(label= year),
box.padding = 0.35,
point.padding = 0.5,
segment.color = 'grey50',
max.overlaps = 50,
force= 85) +
# Add lines 45° line for private sector, add x and y axis for CAB and gvtbal
geom_abline(color='grey') +
geom_vline(xintercept = 0, color='grey')+
geom_hline(yintercept = 0, color='grey')+
theme(legend.position = "none")+
coord_fixed()
Created on 2022-10-10 with reprex v2.0.2