Home > Back-end >  How to avoid too much space around polar chart when drawing it on top of another plot
How to avoid too much space around polar chart when drawing it on top of another plot

Time:12-12

This is my dataframe:

my_df <-structure(list(Statistic = c("Shots on target %", "Shots on target %", 
"% of dribblers tackled", "% of dribblers tackled", "Ground passes", 
"Ground passes", "Passes Completed", "Passes Completed", "Live-ball passes", 
"Live-ball passes", "Passes Attempted (Right)", "Passes Attempted (Right)", 
"Passes Attempted", "Passes Attempted", "Successful Pressure %", 
"Successful Pressure %", "Pressures (Mid 3rd)", "Pressures (Mid 3rd)", 
"Switches", "Switches"), level = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 
4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 9L, 10L, 10L), satistics_fct = structure(c(1L, 
1L, 2L, 2L, 3L, 3L, 4L, 4L, 5L, 5L, 6L, 6L, 7L, 7L, 8L, 8L, 9L, 
9L, 10L, 10L), .Label = c("Shots on target %", "% of dribblers tackled", 
"Ground passes", "Passes Completed", "Live-ball passes", "Passes Attempted (Right)", 
"Passes Attempted", "Successful Pressure %", "Pressures (Mid 3rd)", 
"Switches"), class = "factor"), color = c("black", "#A2AED2", 
"black", "#A2AED2", "black", "#8292C4", "black", "#647BB8", "black", 
"#3F68AD", "black", "#F5F5F5", "black", "#F5DDC8", "black", " #F5DDC8", 
"black", "#F3C8A2", "black", "#F3C8A2 "), y = c("flowers", "base", 
"flowers", "base", "flowers", "base", "flowers", "base", "flowers", 
"base", "flowers", "base", "flowers", "base", "flowers", "base", 
"flowers", "base", "flowers", "base"), statistics_n = c(31.3, 
10, 24.4, 10, 20.58, 10, 20.33, 10, 18.4, 10, 18.22, 10, 18.21, 
10, 13.5, 10, 10.92, 10, 10.82, 10)), class = c("tbl_df", "tbl", 
"data.frame"), row.names = c(NA, -20L))

With this dataframe I create this polar chart:

        palette_10 <- c('#EFFBF9','#EBF8F9', '#E7F3F8', '#E3ECF6', '#DFE5F4', '#EFFBF9',
                '#E7F3F8', '#EEDDD8', '#FCE1F3','black')
polar_chart <- ggplot(my_df %>%
         filter(y == 'flowers') %>%
         mutate(color = palette_10),
       aes(x = level , y =  statistics_n, group = y))  
  geom_col(aes(color = color, fill = color),show.legend = F, width = .95,
           position = 'stack')   
  
  scale_x_reverse()  
  
  theme(
    panel.grid = element_line(color  ='white'),
    plot.background = element_rect(color = 'blue', size = 5),
    plot.title = element_blank(),
    plot.margin = margin(0,0,0,0,'pt'),
    axis.text =  element_blank(),
    axis.title = element_blank(),
    axis.ticks.y =  element_blank()
  ) 
  coord_polar(theta = 'x',direction = -1) 
  geom_point(x = 6, y = 0, size = 20, color = 'black', fill = 'white', stroke = 15,
             shape = 21)

And then I want to include this polar chart in my base chart bellow:

#######################
df <- tibble(x = 0:1, y = 0:1)

base <- ggplot(df, aes(x,y))  
  scale_x_continuous(breaks = seq(0,2,.05)) 
  scale_y_continuous(breaks = seq(0,3,.05)) 
  
  coord_fixed(expand = c(0,0), xlim = c(0,2), ylim = c(0,3))  
  
  theme(panel.grid = element_line(color  ='red'))
  
base    annotation_custom(grob = ggplotGrob(polar_chart),
                    xmin = .9, xmax = 2,ymin = .9)

My question is: How can I avoid this empty space around my radial chart? I coloured with blue color to see the space between the plot area and the the "begining" of the chart which is completely empty.

I think I need to solve this before I make the bar chart becomes polar chart. But I dont know how to do this.

Any help?

CodePudding user response:

There seems to be a default 10% gutter on the radial scale

scale_y_continuous(expand = expansion(mult = c(0, -0.1)))

after scale_x_reverse seems to fix it.
(My first thought was scale_y_continuous(expand = c(0, 0)), but that didn't work.)

CodePudding user response:

If you can live with using awesome extension packages, I'd advise to use {cowplot} for drawing a plot on top of another plot.

library(tidyverse)
library(cowplot)

polar_chart <- ggplot(my_df %>%
                        filter(y == 'flowers') %>%
                        mutate(color = palette_10),
                      aes(x = level , y =  statistics_n, group = y))  
  geom_col(aes(color = color, fill = color),show.legend = F, width = .95,
           position = 'stack')   
  
  scale_x_reverse()  
## using a cowplot theme which has a transparent background 
  theme_minimal_grid()   
  theme(
    panel.grid = element_line(color  ='white'),
    plot.background = element_rect(color = 'blue', size = 5),
    plot.title = element_blank(),
    plot.margin = margin(0,0,0,0,'pt'),
    axis.text =  element_blank(),
    axis.title = element_blank(),
    axis.ticks.y =  element_blank()
  ) 
  coord_polar(theta = 'x',direction = -1) 
  geom_point(x = 6, y = 0, size = 20, color = 'black', fill = 'white', stroke = 15,
             shape = 21) 

df <- tibble(x = 0:1, y = 0:1)

base <-
  ggplot(df, aes(x,y))  
  scale_x_continuous(breaks = seq(0,2,.05)) 
  scale_y_continuous(breaks = seq(0,3,.05)) 
## I've removed the expand argument as this gave a warning
  coord_fixed(xlim = c(0,2), ylim = c(0,3))  
  theme(panel.grid = element_line(color  ='red'))

## Now here's the main trick 
ggdraw(base)  
  draw_plot(polar_chart, scale = .5)

  • Related