根据它的密度(频率)用颜色画一个圆圈

Draw a circle with color according to it's density (frequency)

我扔过球,每次都记录球离我的距离。我可以根据球与我的距离绘制密度图。但是,我想画一个圆,其半径为最大投掷距离。

此外,我想用颜色渐变来表示球数不同频率的区域(如密度图)。

如何使用 R 实现此目的?

使用mtcars作为示例数据,绘制密度,然后转换为极坐标:

library(ggplot2)

ggplot(mtcars, aes(mpg)) +
  geom_density() +
  coord_polar()

您可以将 geom_segmentcoord_polar 一起使用(为此您必须自己设置 pre-calculate 密度)。

library(ggplot2)
# Lets calculate frequency of how far is the ball
d <- density(chickwts$weight)
# Here x is weight (or distance for OP) and y is frequency
pd <- data.frame(distance = d$x, frequency = d$y)

ggplot(pd, aes(x = 1, xend = 2, y = distance, yend = distance, color = frequency)) +
  geom_segment(stat = "identity") +
  coord_polar() +
  scale_color_viridis_c() +
  labs(
    title = "How far is the ball",
    x = "Distance",
    y = "Distance",
    color = "Frequency"
  ) +
  theme_classic() +
  theme(axis.text.x = element_blank())

如果需要分类分组,可以使用这个:

# Check if frequency is within wanted range
pd$color <- pd$frequency > 0.002 & pd$frequency < 0.003    
ggplot(
  pd,
  aes(x = 1, xend = 2, y = distance, yend = distance, color = color)
) +
  geom_segment(stat = "identity") +
  coord_polar() +
  scale_color_viridis_d() +
  labs(
    title = "How far is the ball",
    x = "Distance",
    y = "Distance",
    color = "Frequency"
  ) +
  theme_classic() +
  theme(axis.text.x = element_blank())