简体   繁体   English

在另一个 plot 顶部绘制时如何避免极坐标图周围有太多空间

[英]How to avoid too much space around polar chart when drawing it on top of another plot

This is my dataframe:这是我的 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:有了这个 dataframe 我创建了这个极坐标图:

        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:然后我想在下面的base图表中包含这个极坐标图:

#######################
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.我用蓝色着色以查看 plot 区域和完全为空的图表“开始”之间的空间。

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?有什么帮助吗?

There seems to be a default 10% gutter on the radial scale径向刻度上似乎有一个默认的 10% 装订线

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

after scale_x_reverse seems to fix it.在 scale_x_reverse 似乎修复它之后。
(My first thought was scale_y_continuous(expand = c(0, 0)) , but that didn't work.) (我的第一个想法是scale_y_continuous(expand = c(0, 0)) ,但这不起作用。)

If you can live with using awesome extension packages, I'd advise to use {cowplot} for drawing a plot on top of another plot.如果您可以使用很棒的扩展包,我建议使用 {cowplot} 在另一个 plot 之上绘制 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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