Home > front end >  Is it possible to have a continuous line with geom_line across facets with facet_nested?
Is it possible to have a continuous line with geom_line across facets with facet_nested?

Time:04-20

I'm learning how to use geom_line with facet_nested and I cannot produce a continuous line across facets and between each pair of facets there is a gap. What I try to do is to obtain a clean and continuous line as if I produced the very same graphic using Microsoft Excel. Before asking this question here, I searched and I found the following thread in StackOverflow :

enter image description here

Is there any solution available in ggplot2 for the two problems indicated in the above screenshot in particular for having a continuous line across facets?

CodePudding user response:

It's possible to add annotations that "connect" multiple facets using grid or patchwork, but I think a simpler approach would be to avoid facets altogether and add the year boxes as a separate plot with the same x range.

I think this could also be done using annotations outside the plot range (e.g. with coord_cartesian(clip = "off")), but this method below worked for me faster.

enter image description here

library(patchwork); library(ggplot2); library(scales)

main_plot <- ggplot(data = df_graph_data)  
  geom_line(mapping = aes(
    x = rownum,
    y = number_of_queries
  ),
  size = 1,
  colour = "blue",
  linetype = "solid"
  )   
  
  scale_x_continuous(
    breaks = seq(
      min(df_graph_data$rownum),
      max(df_graph_data$rownum),
      by = 1L
    ),   # let's define x range in coord_cartesian instead 
    labels = df_graph_data$month_number,
    name = NULL
  )  
  scale_y_continuous(
    limits = c(0, max(df_graph_data$number_of_queries)   1000000L),
    breaks = seq(0, max(df_graph_data$number_of_queries)   1000000L,
                 by = 250000L
    ),
    labels = comma,
    expand = expansion(mult = 0, add = 0)
  )  
  geom_point(mapping = aes(
    x = rownum,
    y = number_of_queries),
    shape = 15
  )  
  coord_cartesian(xlim = c(0,27), expand = FALSE)   # range should align between plots
  labs(
    x = "Month",
    y = "Number of clients queries"
  )  
  theme(
    strip.placement = "outside",
    axis.text.x = element_text(angle = 0, vjust = 0.5, hjust=0.5),
    axis.title.y = element_text(margin = margin(t = 0, r = 15, b = 0, l = 0)),
    axis.title.x = element_text(margin = margin(t = 15, r = 0, b = 0, l = 0)),
    plot.title = element_text(hjust = 0.5, size = 12),
    plot.caption = element_text(size = 8, hjust = 1),
    plot.caption.position = "plot"
  ) 


# data for year labels
year_lab <- data.frame(
  from  = c(0,  11.5, 23.5),
  to    = c(11.5, 23.5, 27),
  lab   = 2020:2022,
  y_top = 2E5,  
  y_bot = 0E5
)

year_plot <- ggplot(year_lab)  
  geom_rect(fill = "gray80", color = "gray70",
            aes(xmin = from, xmax = to, ymin = y_bot, ymax = y_top))  
  geom_text(aes((from to)/2, y = (y_bot y_top)/2, label = lab),
            vjust = 0.5, hjust = 0.5)  
  theme_void()  
  coord_cartesian(clip = "off", xlim = c(0,27), expand = FALSE)  
  labs(caption = "Statistics on queries")  
  theme(plot.margin = unit(c(0,0,0,0), "lines"))

main_plot / year_plot  
  plot_layout(ncol = 1, heights = c(10,0.5))
  • Related