简体   繁体   中英

How to define common y-axis limits when using facet_grid() in ggplot2

My third post here. I am leading the plot design using lines and areas in ggplot2 with facet_grid() . The code works well. Here is the data I used:

#My data
df <- structure(list(Var = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 
8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 
11L, 12L), .Label = c("1", "2", "3", "4", "5", "6", "7", "8", 
"9", "10", "11", "12"), class = c("ordered", "factor")), Val = c(46.0233614780009, 
85.6471698498353, 83.8100037071854, 98.6977939726785, 94.0682111307979, 
92.1834012959152, 79.1579962009564, 62.9422475816682, 2.36891501117498, 
25.3718703053892, 87.2779565863311, 32.0944497128949, 444.363995105959, 
337.84707041923, 93.2718054391444, 171.342949266545, 81.6757546272129, 
135.7353850035, 286.496924120001, 450.293861329556, 339.913251576945, 
80.7274857070297, 122.17661133036, 370.043645612895), Group = c("Up", 
"Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", "Up", 
"Down", "Down", "Down", "Down", "Down", "Down", "Down", "Down", 
"Down", "Down", "Down", "Down")), row.names = c(NA, -24L), class = "data.frame")

Now this is my plot:

library(ggplot2)
library(dplyr)
#Code for the plot
df %>%
  mutate(Val=ifelse(Group=='Down',-Val,Val),
         Group=factor(Group,levels = c('Up','Down'),ordered = T)) %>%
  ggplot(aes(x=Var,y=Val,color=Group,fill=Group,group=Group))+
  geom_line(size=1)+geom_area(alpha=0.75)+
  facet_grid(Group~.,scales = 'free')+
  scale_y_continuous(labels = function(x) abs(x),
                     expand = c(0,0.1))+
  scale_fill_manual(values=c('cyan','tomato'))+
  scale_color_manual(values=c('cyan','tomato'))+
  theme_bw()

Which produces this baby:

在此处输入图像描述

Until here everything is fine. I would like if it is possible to modify two elements:

  1. How can I reduce the vertical space between both facets so that visually there would be only one x-axis (align in the same axis both zeroes and reduce that space).

  2. I would like to have the same scale in both y-axis, so the code is designed to have a mirror effect. My issue is that when I set a scale in y-axis like this:

scale_y_continuous(labels = function(x) abs(x),expand = c(0,0.1),limits = c(NA,800))

Everything changes:

在此处输入图像描述

I would like if possible that both y-axis have the same scale starting in 0 to 800 but respecting the magnitude. In this case upper would go from 0 to 800 and down from 0 to -800 but masked according to labels to have the mirror effect.

Many thanks for the help.

This looks like an ideal job for the old "invisible dots" trick to set the facet limits where you want them:

library(ggplot2)
library(dplyr)

df %>%
  mutate(Val = ifelse(Group == 'Down', -Val, Val),
         Group = factor(Group, levels = c('Up', 'Down'), ordered = TRUE)) %>%
  ggplot(aes(Var, Val, color = Group, fill = Group, group = Group)) +
  geom_line(size = 1) +
  geom_area(alpha = 0.75) +
  geom_point(data = data.frame(Group = factor(c("Down", "Up"), c('Up', 'Down')),
                               Var = c(1, 1),
                               Val = c(-800, 800)), alpha = 0) +
  facet_grid(Group~., scales = 'free') +
  scale_y_continuous(labels = function(x) abs(x),
                     expand = c(0, 0.1)) +
  scale_fill_manual(values = c('cyan', 'tomato')) +
  scale_color_manual(values = c('cyan', 'tomato')) +
  theme_bw() +
  theme(panel.spacing = unit(0, "points"))

在此处输入图像描述

Though, it's only fair to point out you don't really need facets here and you get a very similar effect by doing:

df %>%
  mutate(Val = ifelse(Group == 'Down', -Val, Val),
         Group = factor(Group, levels = c('Up', 'Down'), ordered = TRUE)) %>%
  ggplot(aes(Var, Val, color = Group, fill = Group, group = Group)) +
  geom_line(size = 1) +
  geom_area(alpha = 0.75) +
  geom_hline(yintercept = 0) +
  scale_y_continuous(labels = function(x) abs(x),
                     expand = c(0, 0.1),
                     limits = c(-800, 800)) +
  scale_fill_manual(values = c('cyan', 'tomato')) +
  scale_color_manual(values = c('cyan', 'tomato')) +
  theme_bw() +
  theme(panel.spacing = unit(0, "points"))

在此处输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM