r - 如何在 r 中绘制多个图,每个图对应一个需要与另一个向量创建坐标的向量?

标签 r ggplot2 vector coordinates permutation

所以,我有一个有序列表,例如:

(1、2、3、4、5、6、7、8)

然后我有多种排列,例如:

(3,2,5,4,1,6,7,8),

(7,2,4,1,3,5,8,6),

...

现在,我需要使用相同的有序列表单独绘制每个排列(在新图中),以便形成坐标散点图。

  • 因此,对于第一个排列,我需要绘制 (1,3)、(2,2)、(3,5)、(4,4)、...

  • 对于第二个排列,我需要绘制 (1,7), (2,2), (3,4), (4,1), ...

类似这样的事情:

Example, where 'a' is the ordered list and 'b' is the first permutation

我为一对+排列对创建了这个,但我不知道如何通过多个排列来扩展它。我只是每次手动替换排列,但这样需要很长时间:

library(tidyverse)

ordered <- c(1, 2, 3, 4, 5, 6, 7, 8, 9)
permutation <- c(5, 7, 8, 6, 9, 4, 1, 3, 2)

coord.permutations <- data.frame(ordered,permutation)

ggplot(data=coord.permutations)+
  geom_point(aes(ordered,permutation,size=3))+
  scale_x_continuous(breaks = seq(0,(length(ordered)+1)), minor_breaks = seq(0,(length(ordered)+1)), expand = c(0,0), limits= c(0,(length(ordered)+1)))+
  scale_y_continuous(breaks = seq(0,(length(ordered)+1)), minor_breaks = seq(0,(length(ordered)+1)), expand = c(0,0), limits= c(0,(length(ordered)+1)))+
  coord_fixed()

预先感谢您的帮助!

最佳答案

假设您已将排列保存在列表 coord.permutations 中,那么您可以使用 map 循环访问该列表,并将每个图也保存在列表中。

library(tidyverse)
coord.permutations <- list(c(5, 7, 8, 6, 9, 4, 1, 3, 2), c(2, 9, 1, 6, 5, 4, 7, 3, 8))
plots <- map(coord.permutations, ~ggplot(tibble(ordered, permutation=.))+
        geom_point(aes(ordered,permutation,size=3))+
        coord_fixed())
plots[[1]]

enter image description here

使用 10 种排列的动画

library(gganimate)
ggplot(data.frame(ordered = rep(ordered, 10), permutation = unlist(map(1:10, ~sample(1:10, 9))), n_per = rep(1:10, each=9))) + 
  geom_point(aes(ordered,permutation)) +
  transition_states(n_per, transition_length = 2, state_length = 1) +
  ggtitle('Permutation {closest_state}')

enter image description here

关于r - 如何在 r 中绘制多个图,每个图对应一个需要与另一个向量创建坐标的向量?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66465097/

相关文章:

r - 需要在R中跳过不同数量的行

r - 变量与 R 独立性的卡方检验

r - 将有图例的图与没有图例的图结合起来

r - 在 ggplot2 中增加图例项之间的空白

r - 分别绘制从 grob 中提取的图例

r - 如何使用 R mongolite 更新子文档数组?

c++ - 为什么不能使用带范围的for循环打印对象 vector 的内容?

c++ - vector 迭代器不兼容......但为什么呢?

c++ - 不能在 C++ 中使用类头中定义的 vector

r - 使用列索引而不是 group_by_at 中的名称显示子组的加权平均值