MonJeanJean
MonJeanJean

Reputation: 2906

Is there a way to have the y-axis in the middle of a mirror plot?

I currently have this graph:

My graph

I'd like to know if it is possible to have the y-axis in between the blue part and the red part. I've tried to use facet_grid in a first place, but couldn't get any result.

Does anyone have a solution or an idea please?

Here's my graph code:

ggplot(test,
       aes(x = age,
           y = agriculteurs_exploitants,
           fill = sexe)) +
  geom_area() +
  geom_line(aes(y = ensemble_des_personnes_en_emploi)) +
  scale_x_continuous(breaks = seq(15,75,5),
                     labels = c(seq(15,70,5),"75 ou +")) +
  scale_y_continuous(limits = c(-3,3),
                     breaks = c(-3,-2,-1,0,1,2,3),
                     labels = c("3","2","1","0","1","2","3")) +
  coord_flip()

Because of the coord_flip, the variable I would like to have in between the two sides is the x axis, the variable age.

Here's the data:

structure(list(age = c(15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 
25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 
41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 
57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 
73, 74, 75, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 
28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 
44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75
), sexe = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L
), .Label = c("Femme", "Homme"), class = "factor"), agriculteurs_exploitants = c(0, 
0, 0.01, 0.01, 0.01, 0.03, 0.03, 0.05, 0.06, 0.1, 0.1, 0.13, 
0.18, 0.2, 0.24, 0.22, 0.24, 0.27, 0.3, 0.3, 0.37, 0.36, 0.39, 
0.37, 0.4, 0.45, 0.45, 0.5, 0.6, 0.65, 0.61, 0.72, 0.73, 0.7, 
0.81, 0.82, 0.92, 1, 1.03, 1.03, 1.16, 1.1, 1.1, 1.12, 1.12, 
0.92, 0.78, 0.52, 0.43, 0.33, 0.31, 0.25, 0.21, 0.16, 0.17, 0.12, 
0.07, 0.07, 0.06, 0.05, 0.49, -0.01, -0.02, -0.03, -0.06, -0.11, 
-0.18, -0.24, -0.33, -0.4, -0.46, -0.6, -0.64, -0.75, -0.87, 
-0.89, -1.07, -1.14, -1.21, -1.25, -1.32, -1.32, -1.32, -1.37, 
-1.44, -1.47, -1.49, -1.62, -1.67, -1.77, -1.89, -2.01, -2.11, 
-2.19, -2.23, -2.35, -2.48, -2.7, -2.72, -2.84, -2.82, -2.84, 
-2.75, -2.76, -2.72, -2.56, -1.99, -1.58, -1.08, -0.83, -0.66, 
-0.51, -0.45, -0.35, -0.29, -0.22, -0.18, -0.11, -0.1, -0.08, 
-0.06, -0.56), ensemble_des_personnes_en_emploi = c(0.02, 0.05, 
0.07, 0.17, 0.29, 0.42, 0.52, 0.65, 0.75, 0.85, 0.94, 0.99, 1.03, 
1.06, 1.09, 1.1, 1.11, 1.13, 1.16, 1.18, 1.19, 1.21, 1.2, 1.19, 
1.19, 1.2, 1.23, 1.27, 1.32, 1.34, 1.36, 1.35, 1.32, 1.33, 1.33, 
1.32, 1.32, 1.31, 1.28, 1.26, 1.23, 1.18, 1.12, 1.06, 0.97, 0.74, 
0.57, 0.34, 0.26, 0.2, 0.12, 0.08, 0.06, 0.05, 0.04, 0.03, 0.03, 
0.02, 0.02, 0.01, 0.14, -0.06, -0.13, -0.17, -0.29, -0.4, -0.54, 
-0.63, -0.72, -0.81, -0.9, -0.99, -1.04, -1.09, -1.12, -1.15, 
-1.18, -1.19, -1.21, -1.24, -1.26, -1.28, -1.29, -1.29, -1.28, 
-1.28, -1.28, -1.32, -1.35, -1.4, -1.42, -1.44, -1.43, -1.41, 
-1.39, -1.39, -1.39, -1.38, -1.37, -1.35, -1.32, -1.27, -1.22, 
-1.16, -1.08, -0.97, -0.64, -0.49, -0.35, -0.27, -0.21, -0.15, 
-0.12, -0.09, -0.08, -0.06, -0.05, -0.04, -0.03, -0.02, -0.02, 
-0.14)), row.names = c(NA, -122L), class = c("tbl_df", "tbl", 
"data.frame"))

Thanks!

Upvotes: 1

Views: 309

Answers (2)

stefan
stefan

Reputation: 125607

One option to get a shared axis which I borrowed from here would be to make use of ggpol::facet_share:

test$sexe <- factor(test$sexe, levels = c("Homme", "Femme"))

library(ggpol)
#> Loading required package: ggplot2

ggplot(test,
       aes(x = age,
           y = agriculteurs_exploitants,
           fill = sexe)) +
  geom_area() +
  geom_line(aes(y = ensemble_des_personnes_en_emploi)) +
  geom_point(aes(y = ifelse(sexe == "Homme", -3, 3)), 
             color = "transparent") +
  scale_x_continuous(breaks = seq(15,75,5),
                     labels = c(seq(15,70,5),"75 ou +")) +
  scale_y_continuous(expand = expansion(mult = c(0, 0)), breaks = -3:3,
                     labels = abs(-3:3)) +
  facet_share(~sexe, scales = "free_x") +
  coord_flip() +
  labs(x = NULL)

Upvotes: 2

Hansel Palencia
Hansel Palencia

Reputation: 1046

Maybe something like this if you're willing to convert to discrete v continuous y axis.

dat <- test

dat$cut_age <- cut(dat$age, 11, right = F)

dat %>% 
  mutate(agriculteurs_exploitants = ifelse(dat$agriculteurs_exploitants >= 0, dat$agriculteurs_exploitants+.2, dat$agriculteurs_exploitants-.2)) %>% 
  mutate(ensemble_des_personnes_en_emploi = ifelse(dat$ensemble_des_personnes_en_emploi >= 0, dat$ensemble_des_personnes_en_emploi+.2, dat$ensemble_des_personnes_en_emploi-.2)) %>% 
  ggplot() +
  #geom_area(aes(x = age, y = agriculteurs_exploitants, fill = sexe)) +
  geom_boxplot(aes(x = cut_age, y = agriculteurs_exploitants, fill = sexe), alpha = .3) +
  geom_point(aes(x = cut_age, y = ensemble_des_personnes_en_emploi, color = sexe)) +
  geom_text(aes(x = cut_age, y = 0, label = cut_age), size = 2.5) +
  coord_flip() +
  scale_y_continuous(limits = c(-3,3),
                     breaks = c(-3,-2,-1,0,1,2,3),
                     labels = c("3","2","1","0","1","2","3")) +
  scale_color_manual(values = c("firebrick", "steelblue3")) +
  theme_classic() +
  theme(axis.text.y = element_blank()) +
  theme(axis.ticks.y.left = element_blank()) +
  theme(axis.line.y = element_blank()) +
  theme(axis.title.y = element_blank())

Also tells a bit more of the story...

enter image description here

Upvotes: 2

Related Questions