Avijit Mallick
Avijit Mallick

Reputation: 37

Removing the white spaces between the plots resulting from timetk library in R

I am trying to have very neat plots. So, trying to removes the whites spaces between the plots. I am using the timetk library. I have seen some of the examples related to the same questions, but those queries are mostly from grid.arrange or command +theme(plot.margin = unit(c(-0.25,0.5,-0.25,0.5), "lines")) from ggplot library. These concepts do not work on some of the plots commands with timetk library.
When there will be many plots, I want to have very neet plots.

My codes are

library(tidyverse)
library(timetk)

    ## Case 1

walmart_sales_weekly %>%
    group_by(Store, Dept) %>%
    plot_anomaly_diagnostics(Date, Weekly_Sales,
                             .facet_ncol = 3, 
                             .interactive = TRUE)

anomaly

## Case 2

walmart_sales_weekly %>%
  select(Date, Weekly_Sales,Size, Temperature,Fuel_Price, CPI, Unemployment )%>%
  pivot_longer(-Date)%>%
    group_by(name) %>%
  plot_seasonal_diagnostics(.date_var =  Date, 
                              .value =  value, 
                              .feature_set = 'month.lbl', 
                              .facet_vars = name
                              )

seasonal

## Case 3

walmart_sales_weekly %>%
  select(Date, Weekly_Sales,Size, Temperature,Fuel_Price, CPI, Unemployment )%>%
  pivot_longer(-Date)%>%
    group_by(name) %>%
  plot_time_series(.date_var =  Date, 
                              .value =  value, 
                             .facet_ncol = 2, .smooth = FALSE
                              )

timeseries_plot

I required timeseries plots with tiketk library. I do not like to use any other library

My requirement plots

Upvotes: 0

Views: 361

Answers (1)

Matt Dancho
Matt Dancho

Reputation: 7298

This is related to how plotly produces facets. If you are trying to make neat plots, I suggest using ggplot by setting .interactive = FALSE. This will ensure the whitespace can be managed with ggplot2.

Once you have a ggplot2 object, you can control the theme() function to adjust the spacing. See this Stack Overflow for more information. enter link description here

# Change spacing between facets on both axis
p + theme(panel.spacing = unit(2, "lines"))

# Change horizontal spacing between facets
p + theme(panel.spacing.x = unit(2, "lines"))

# Change vertical spacing between facets
p + theme(panel.spacing.y = unit(2, "lines"))

Upvotes: 1

Related Questions