首页 > 解决方案 > 如何在 R 的罚球区外画一个角和半圆?

问题描述

这是我在 RStudio 中绘制的代码,但我不知道如何将弧线放在角落和区域外。我是一个初学者,我不知道是否需要使用 geom_circle ,还找出两个向量 x 和 y 的正确位置来绘制罚球区外的角和半圆。

  geom_point(data_player, mapping = aes(x = x1 - 58.015,
                                        y = y1,
                                        fill = factor(Resultado)),
             shape = 21,
             size = 5) +
  #scale_fill_manual(values = realmadrid_colors) +
  guides(fill = guide_legend(title = NULL)) +
  
  geom_rect(mapping = aes(xmin = 0.0, xmax = 596.97, 
                          ymin = 50.5, ymax = 446.5),
            color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 0.0, xmax = 91.935, 
                          ymin = 128.975, ymax = 368.025), 
            color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 505.035, xmax = 596.97, 
                          ymin = 128.975, ymax = 368.025), 
            color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 0.0, xmax = 29.858, 
                          ymin = 195, ymax = 302.0), 
            color ="#00529f", fill = NA, alpha = 0.1) +
  geom_rect(mapping = aes(xmin = 567.112, xmax = 596.97, 
                          ymin = 195, ymax = 302.0), 
            color ="#00529f", fill = NA, alpha = 0.1) +
 
  geom_linerange(aes(x = 298.485, ymin = 50.5, 
                     ymax = 446.5), 
                 color = "#00529f") +
  geom_point(mapping = aes(x = 66.33, y = 248.5), 
             size = 1, color = "#00529f") +
  geom_point(mapping = aes(x = 530.64, y = 248.5), 
             size = 1, color = "#00529f") +
  geom_circle(mapping = aes(x0 = 298.485, 
                            y0 = 248.5, r = 52), 
              color = "#00529f") +
  coord_fixed() +
 theme_no_axes(base.theme = theme_bw()) +
  theme(legend.position = c(0.5, 0.04),
        legend.box = "horizontal",
        legend.direction = "horizontal",
        legend.box.background = element_rect(fill = "transparent",
                                             colour = "transparent"),
        legend.text = element_text(size = 14),
        panel.border = element_blank(),
        axis.title = element_blank(), 
        axis.text = element_blank(), 
        axis.ticks = element_blank()) +
        plot.margin=unit(c(-0.05,-0.05,-0.05,-0.1),"in")) +
  scale_x_continuous(limits = c(0,647.47), expand = c(0,0)) +
  scale_y_continuous(limits = c(0,497), expand = c(0,0))

player_scatter_plot'''

标签: rggplot2

解决方案


这可以通过以下方式实现ggforce::geom_arc

library(ggforce)
#> Loading required package: ggplot2
library(tibble)

ggplot() +
  geom_rect(data = data_box, aes(xmin = xmin, xmax = xmax, 
                          ymin = ymin, ymax = ymax), 
            color ="#00529f", alpha = 0.1) +
  geom_linerange(data = data_line, aes(x = x, ymin = ymin, ymax = ymax),
                 color = "#00529f") +
  geom_point(data = data_penalty, aes(x = x, y = y), size = 1, color = "#00529f") +
  geom_circle(mapping = aes(x0 = 298.485, y0 = 248.5, r = 52), color = "#00529f") +
  ggforce::geom_arc(data = data_corner, aes(x0 = x0, y0 = y0, start = start, end = end, r = 15), color = "#00529f") +
  ggforce::geom_arc(data = data_semi, aes(x0 = x0, y0 = y0, start = start, end = end, r = 30), color = "#00529f") +
  scale_x_continuous(expand = c(0,0)) +
  scale_y_continuous(expand = c(0,0)) +
  coord_fixed() +
  theme_no_axes(base.theme = theme_bw()) +
  theme(legend.position = c(0.5, 0.04),
        legend.box = "horizontal",
        legend.direction = "horizontal",
        legend.box.background = element_rect(fill = "transparent",
                                             colour = "transparent"),
        legend.text = element_text(size = 14),
        panel.border = element_blank(),
        axis.title = element_blank(), 
        axis.text = element_blank(), 
        axis.ticks = element_blank(),
        plot.margin=unit(c(-0.05,-0.05,-0.05,-0.1),"in"))

数据

data_box <- tribble(
  ~xmin, ~xmax, ~ymin, ~ymax,
  0, 596.97, 50.5, 446.5,
  0, 91.935, 128.975, 368.025,
  505.035, 596.97, 128.975, 368.025,
  0, 29.858, 195, 302.0,
  567.112, 596.97, 195, 302.0
)

data_line <- data.frame(x = 298.485, ymin = 50.5, ymax = 446.5)
data_penalty <- data.frame(x = c(66.33, 530.64), y = rep(248.5, 2))

data_corner <- tribble(
  ~x0, ~y0, ~start, ~end,
  0, 50.5, 0, pi / 2,
  0, 446.5, pi, pi /2,
  596.97, 50.5, 3 * pi / 2, 2 * pi,
  596.97, 446.5, pi, 3 * pi /2
)

data_semi <- tribble(
  ~x0, ~y0, ~start, ~end,
  91.935, 248.5, 0, pi,
  505.035, 248.5, pi, 2 * pi
)

推荐阅读