小编For*_*415的帖子

使用ggplot将线段和点一起绘制

我想要完成的任务:手动添加形状(足球场上的线条和圆圈,在我的实际项目中)到散点图.这是我实际项目的缩小示例,但说明了我需要帮助的问题.

以下是我为此示例使用的数据:

data <- data.frame("name" = c("A", "A", "B", "B", "C", "C"),
                     "x" = c(.13, .64, .82, .39, .51, .03),
                     "y" = c(.62, .94, .10, .24, .20, .84))
Run Code Online (Sandbox Code Playgroud)

我将提供示例代码,首先是一种工作方式(繁琐,太长),然后是一种我无法弄清楚的方法(这似乎更有效/简洁,可能...... 更快?).

library(ggplot2)

ggplot(data, aes(x, y)) +
  geom_point() +
  geom_segment(aes(x = 0,xend = 1,y = 1,yend = 1)) +
  geom_segment(aes(x = 0, xend = 1, y=0,yend=0)) +
  geom_segment(aes(x=1, xend=1, y=0, yend=1)) +
  geom_segment(aes(x=0, xend=0, y=0, yend=1))
Run Code Online (Sandbox Code Playgroud)

1x1足球场上球员位置的示例图

这让我有一个很好的1x1音高,有6个数据点(不要以为我可以嵌入剧情,因为我没有足够的声望).由于我的实际项目有更多的数据点,还有更多的"形状"(线段,圆和圆弧)构成音高,我认为使用向量来定义geom_segment美学会更好 - 因为完整的数据集非常缓慢.这就是我所拥有的:

ggplot(data, aes(x, y)) +
  geom_point() +
  geom_segment(aes(x = c(0,0,1,0),
               xend …
Run Code Online (Sandbox Code Playgroud)

r data-visualization ggplot2

8
推荐指数
1
解决办法
612
查看次数

如何使用 geom_circle 函数绘制圆

我的目标是使用 ggplot2 和 ggforce 包的组合绘制 NBA 篮球场的尺寸/线条。我已经使用 + geom_segment() 图层成功绘制了线段(边线、罚球线等),但是我很难使用 + geom_circle() 和 + geom_arc() 函数来绘制圆圈和圆弧(三点线、半场圆等)

我的代码如下,其中对象 'sample' 只是一个带有 x 和 y 坐标的镜头数据框:

ggplot(sample, aes(shot_x, shot_y)) +
geom_point(color = "red", alpha = .2) +
geom_segment(aes(x = 0, xend = 94, y = 0, yend = 0)) +
geom_segment(aes(x = 0, xend = 94, y = 50, yend = 50)) +
geom_segment(aes(x = 0, xend = 0, y = 0, yend = 50)) +
geom_segment(aes(x = 94, xend = 94, y …
Run Code Online (Sandbox Code Playgroud)

r ggplot2 ggforce

3
推荐指数
1
解决办法
4052
查看次数

标签 统计

ggplot2 ×2

r ×2

data-visualization ×1

ggforce ×1