coord_polar中的设计面区域

1yjd4xko  于 2022-12-20  发布在  其他
关注(0)|答案(2)|浏览(206)

碰巧我正在进行一些调查,这些调查的数据可以直接显示(就像Map一样),因为极坐标是由相对于中心的距离和Angular 定义的。但是,有些数据不应该用简单的点来表示,而应该用表面来表示。由一个共同变量所确定的四个或更多点的位置和Angular 定义的(就像在正常聚类中发生的那样)。在下面的例子中,我希望所有用变量“Qu”标识的点都被分组在同一个表面上。计算表面的问题可以很容易地单独解决,为了图形的目的,目前我只对表示感兴趣。

library(ggplot2)

Azimut<-c(30,60,90,270,275, 45, 135, 45, 180)
Distance<-c(3,6,12,16,22, 4, 16, 18, 18)
CHR<-c(12,19,55,7,39,34,32,34,32)
Spe1<-c("Eu","Pi","Qu","Qu","Ol", "Qu", "Qu", "Qu", "Qu")
Tr1<-data.frame(Azimut,Distance, CHR, Spe1)
Tr1$Theta<-2*pi*Azimut/360

# With polar coordinates
ggplot(aes(x = (Azimut), y = Distance), data = Tr1) + 
  geom_hline(aes(yintercept = 6)) +
  geom_hline(aes(yintercept = 18)) +
  scale_x_continuous(limits = c(0, 360),
                     breaks = seq(0, 360, 45)) +
  scale_y_continuous(limits = (c(0, 18)),
                     breaks = (seq(0, 6, 18))) +
  geom_point(aes(size=CHR, fill=factor(Spe1)),shape =21,alpha = 0.7)+
  coord_polar(theta = "x")

这给出了以下结果:

guicsvcw

guicsvcw1#

这里有两种选择。一种是计算曲线,当转换为极坐标时,这些曲线会给予直线。第二种是将数据转换为极坐标,并绘制正态直线多边形。出于多种原因,我认为第二种选择更容易。甚至可以使用注解层来伪造极轴:

library(ggplot2)

ggplot(aes(x = Distance * cos(Theta), y = Distance * sin(Theta)), data = Tr1) +
  geom_abline(slope = c(1000, 0, tan(pi/6), tan(pi/3), tan(-pi/3), tan(-pi/6)),
              color = 'white', linewidth = 0.5) +
  geom_path(aes(x = x, y = y), 
            data = data.frame(x = 6 * sin(seq(0, 2 * pi, length = 200)),
                              y = 6 * cos(seq(0, 2 * pi, length = 200)))) +
  geom_path(aes(x = x, y = y), 
            data = data.frame(x = 18 * sin(seq(0, 2 * pi, length = 200)),
                              y = 18 * cos(seq(0, 2 * pi, length = 200)))) +
  geom_path(aes(x = x, y = y), 
            data = data.frame(x = 20 * sin(seq(0, 2 * pi, length = 200)),
                              y = 20 * cos(seq(0, 2 * pi, length = 200))),
            col = 'white') +
  annotate("text", label = c("360/0", seq(45, 315, 45)),
           x = 20 * sin(seq(pi/2, -5*pi/4, -pi/4)),
           y = 20 * cos(seq(pi/2, -5*pi/4, -pi/4))) +
  geom_point(aes(size = CHR, fill = factor(Spe1)), shape = 21, alpha = 0.7) +
  lapply(split(Tr1, Tr1$Spe1), function(x) {
    if(nrow(x) < 4) return(NULL)
    geom_polygon(data = x[chull(x$Distance * cos(x$Theta), 
                                x$Distance * sin(x$Theta)),],
                 aes(fill = factor(Spe1), 
                     color = after_scale(scales::alpha(fill, 1))), alpha = 0.2)
  }) +
  coord_flip() +
  theme_void() +
  theme(panel.background = element_rect(fill = 'gray92', color = NA),
        plot.margin = margin(20, 20, 20, 20))

xqkwcwgp

xqkwcwgp2#

你可以用chull找到形成一个包围船体的点的行索引,然后用geom_polygon使用它们的坐标。对于笛卡尔坐标,这是可行的-但极坐标可能会给予意想不到的结果。

library(ggplot2)
library(dplyr)

df <- 
  data.frame(
    Azimut = c(30,60,90,270,275, 45, 135, 45, 180),
    Distance = c(3,6,12,16,22, 4, 16, 18, 18),
    CHR = c(12,19,55,7,39,34,32,34,32),
    Spel = c("Eu","Pi","Qu","Qu","Ol", "Qu", "Qu", "Qu", "Qu")
  ) |>
  mutate(Theta =  pi * Azimut / 180)
  
## which rows of subset Spel === "Qu" form the convex hull for this subset
hull_rows <- 
  df |>
  filter(Spel == "Qu") |>
  select(Azimut, Distance) |>
  chull()

df |>
  ggplot() +
  geom_point(aes(Azimut, Distance)) +
  geom_polygon(data = df[hull_rows,],
               aes(Azimut, Distance),
               fill = 'red', alpha = .5
               ) + coord_polar()

x1c 0d1x(Debian标志的种类...)

相关问题