R 图形:具有极坐标的堆叠点

R graphics: stacked dots with polar coordinates

提问人:Julien 提问时间:11/10/2023 最后编辑:Julien 更新时间:11/11/2023 访问量:55

问:

使用 RStudio,我正在尝试: (1)绘制没有重叠的点,因为我想在每个点上放置数字, (2) 考虑到 x 轴和 y 轴都是分类的,并且 (3)在极坐标/蜘蛛网坐标图上

这是我的数据:

dtgr <- data.frame(
  gbf_ttl = c("Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal A", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal C", "Goal D", "Target 1", "Target 1", "Target 2", "Target 3", "Target 3", "Target 3", "Target 3", "Target 3", "Target 3", "Target 3", "Target 3", "Target 4", "Target 4", "Target 4", "Target 4", "Target 4", "Target 4", "Target 4", "Target 4", "Target 4", "Target 5", "Target 5", "Target 5", "Target 5", "Target 5", "Target 5", "Target 5", "Target 6", "Target 7", "Target 8", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 9", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 11", "Target 13", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 16", "Target 18", "Target 18", "Target 18", "Target 19", "Target 20", "Target 20", "Target 21", "Target 21", "Goal A", "Goal A", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal B", "Goal C", "Target 1", "Target 2", "Target 3", "Target 4", "Target 4", "Target 5", "Target 5", "Target 7", "Target 8", "Target 9", "Target 9", "Target 10", "Target 10", "Target 10", "Target 10", "Target 10", "Target 11", "Target 11", "Target 11", "Target 13", "Target 14", "Target 14", "Target 14", "Target 14", "Target 14", "Target 18", "Target 21", "Target 21", "Target 22", "Target 22", "Target 23", "Goal B", "Goal B", "Target 6", "Target 12", "Target 15", "Target 17"), 
  nbt_ttll = c("1.1", "2.1", "3.3", "5.1", "5.2", "6.1", "6.2", "7.4", "7.5", "7.6", "7.7", "10.1", "12.1", "13.1", "14.1", "15.2", "17.1", "19", "2.2", "3.3", "3.4", "6.2", "6.3", "7.5", "7.7", "8.1", "12.1", "13.1", "17.1", "18.1", "19", "13.1", "20", "5.1", "7.6", "5.1", "1.1", "3.3", "5.1", "6.1", "7.6", "10.1", "15.1", "15.2", "1.1", "5.1", "5.2", "7.6", "10.1", "13.1", "14.1", "15.1", "15.2", "1.1", "6.2", "7.5", "7.6", "10.1", "11.1", "13.1", "9.1", "7.5", "6.1", "1.1", "3.4", "5.1", "6.2", "6.3", "7.5", "7.6", "7.7", "10.1", "11.1", "12.1", "13.1", "14.1", "15.1", "17.1", "1.1", "2.1", "3.4", "5.1", "6.2", "6.3", "7.3", "7.7", "10.1", "11.1", "13.1", "15.1", "15.2", "17.1", "1.1", "5.1", "6.1", "7.6", "7.7", "8.1", "10.1", "13.1", "15.1", "17.1", "19", "2.1", "1.1", "3.2", "3.3", "3.4", "5.1", "5.2", "7.5", "7.6", "10.1", "13.1", "15.1", "16.1", "17.1", "18.1", "19", "20", "1.1", "3.2", "3.4", "14.1", "20", "1.1", "19", "2.1", "19", "11.1", "15.1", "1.1", "2.1", "5.1", "5.2", "6.1", "7.6", "10.1", "14.1", "15.1", "18.2", "11.1", "15.2", "11.1", "11.1", "12.1", "6.1", "18.2", "8.1", "15.2", "6.1", "18.2", "2.2", "6.1", "7.1", "7.5", "7.6", "11.1", "14.1", "15.2", "16.1", "2.1", "2.2", "11.1", "14.1", "15.2", "3.3", "18.1", "18.2", "18.1", "18.2", "18.1", "11.1", "15.2", "9.3", "NA", "NA", "NA"), 
  catt = c("Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low10", "Low10", "Low10", "Low10", "NA", "NA", "NA", "NA", "NA", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low10", "Low10", "Low10", "Low10", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low10", "Low10", "Low10", "Low10", "NA", "NA", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low10", "Low10", "Low10", "Low10", "NA", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low10", "Low10", "Low1", "Low1", "Low1", "Low1", "Low4", "Low4", "Low4", "Low7", "Low7", "Low7", "Low10", "Low10", "Low10", "Low10", "NA", "NA", "NA", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Low1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium4", "Medium4", "Medium7", "Medium7", "Medium10", "Medium10", "Medium10", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium4", "Medium4", "Medium7", "Medium1", "Medium1", "Medium4", "Medium1", "Medium1", "Medium1", "Medium4", "Medium4", "Medium7", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "Medium1", "High1", "High2", "High1", "NA", "NA", "NA")
)

dtgr$gbf_ttl <- factor(dtgr$gbf_ttl, levels = unique(dtgr$gbf_ttl))
dtgr$nbt_ttll <- factor(dtgr$nbt_ttll, levels = unique(dtgr$nbt_ttll))
dtgr$catt <- factor(dtgr$catt, levels= c("Low12", "Low11", "Low10", "Low9", "Low8", "Low7", "Low6", "Low5", "Low4", "Low3", "Low2", "Low1", "Low0", 
                       "Medium10", "Medium9", "Medium8", "Medium7", "Medium6", "Medium5", "Medium4", "Medium3", "Medium2", "Medium1", "Medium0",  
                       "High3", "High2", "High1", "High0", "High00"))


pt1 <- c("Target 1", "Target 2", "Target 3", "Target 4", "Target 5", "Target 6", "Target 7", "Target 8", "Target 9", "Target 10", "Target 11")
dtgr1 <- dtgr %>% filter(gbf_ttl %in% pt1)

我尽了最大的努力使我使用: (1) geom_quasirandom()(而不是 geom_point() 或 geom_jitter()),它与 coord_polar() 配合得很好,但一定程度的 ovelap 仍然存在,这使得无法读取标签 - 特别是因为我需要对 69 个案例重复这一点,每个案例都有不同的点数 下面是它的样子:

sep1 <- seq(0.5, 10.5, 1)

ggplot(data = dtgr1, aes(x = gbf_ttl, y = catt)) +
  geom_rect(data = data.frame(sim = factor(c("High", "Medium", "Low"), levels = c("High", "Medium", "Low")), 
                              start = c(0.5, 5.5, 16.5), end = c(5.5, 16.5, 28.5)), inherit.aes = FALSE, 
            aes(xmin = 0.5, xmax = Inf, ymin = start, ymax = end, fill = sim), 
            colour = "black", size = 0.1, show.legend = FALSE) + 
  scale_fill_manual(values = c("#00A54A", "#59DC3D", "#AFFF57")) +
  scale_x_discrete() + 
  scale_y_discrete(limits = rev(levels(dtgr$catt))) + 
  geom_vline(xintercept = sep1, colour = "black") + 
  geom_quasirandom(size = 10, show.legend = FALSE, width = 0.8, dodge.width = 0, color = "white") + 
  geom_text(aes(label = nbt_ttll), size = 4, fontface = "bold", color = "black", 
            position = position_quasirandom(width = 0.8, dodge.width = 0)) + 
  coord_polar(theta = "x") + 
  theme(line = element_blank(), 
        panel.background = element_blank(), panel.grid.major = element_blank(), panel.grid.minor = element_blank(), 
        axis.title = element_blank(), axis.text.y = element_blank(), axis.text.x = element_text(size = 6), 
        legend.title = element_text(size=8), legend.text = element_text(size=7), legend.key = element_blank(), 
        plot.margin = unit(c(0, 0, 0, 0), "cm"))

我能够制作的重叠绘图

(2) geom_dotplot()),它完全符合我想要的点本身(堆叠,并排)......但它不适用于 coord_polar():“geom_dotplot() 不适用于非线性坐标” 以下是我希望这些点的表示方式,以便我可以将坐标调整为极坐标:

ggplot(dtgr1, aes(x = gbf_ttl, y = catt)) + 
  geom_dotplot(binaxis = "y", stackdir = "center", stackratio = 1.2) +
  scale_y_discrete(limits = rev(levels(dtgr1$catt))) #+ coord_polar(theta = "x")

我希望生成的非重叠非极性图

关于如何克服任何一种方法的局限性以实现“两全其美”的任何想法?

先谢谢你!

r ggplot2 坐标

评论

2赞 Allan Cameron 11/10/2023
嗨,朱利安。如果没有你试图绘制的数据,任何帮助你的尝试都是毫无意义的(请原谅双关语)。您能否编辑您的问题以包含您已经尝试过的格式和代码的数据?谢谢。dput
1赞 MrFlick 11/10/2023
如果您包含一个简单的可重现示例,其中包含可用于测试和验证可能的解决方案的示例输入和所需输出,则更容易为您提供帮助。如果您不是在问特定的编程问题,而是在寻求一般的数据可视化建议,那么这对 Stack Overflow 来说就不是主题了。
0赞 r2evans 11/10/2023
coord_polar()似乎适用于离散变量(角度和大小),因此这是特定于 的。至于“没有重叠”,有两种选择,并且非常努力地防止事情重叠。这几乎是我所能做到的,没有更实质性的东西(即样本代表性数据)。添加数据时,请使用(您的数据在哪里,请不要太大,只需要列),并确保样本包含会导致重叠问题的观测值。谢谢!geom_dotplotjitter(.)ggrepel::geom_text_repel()dput(x)x
0赞 r2evans 11/10/2023
我在制作离散变量极坐标图方面不是很有经验,所以至少对某些人来说,知道你期望的结果是什么样子可能会很有趣,即使是一张餐巾纸素描的图片或某种形式的模型。如果您能够制作准系统 ggplot 极坐标图,但它无法满足您的需求,那么查看代码/图像以及确切的问题和需要修复的地方也很好。谢谢!

答: 暂无答案