r - 如何在 r 中制作多个图,每个向量需要与另一个向量创建坐标?
问题描述
所以,我有一个有序列表,例如:
(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), ...
像这样的东西:
我为一对 + 排列对创建了这个,但我不知道如何使用多个排列对其进行缩放。我每次都手动替换排列,但是这样需要很长时间:
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()
提前感谢您的帮助!
解决方案
假设您已将排列保存在 list 中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]]
使用 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}')
推荐阅读
- spring-boot - STS4 如何在 Spring Tools Suite 4 中运行代码覆盖率?
- javascript - 我希望图像在我将鼠标悬停在单词上时显示并在鼠标移出时隐藏
- database - 为什么我在 Andriod Studio 的 Dart 中的 Flutter 中编写我的应用程序时收到此 DatabaseService 和 initialData 错误?
- webpack-module-federation - 模块联合不适用于 webpack.config.js 中的动态挂钩远程
- spinnaker - 卡在大三角帆操作员的 yaml 配置中
- google-apps-script - Google Sheet Scripts -> ss.getRange() -> JSON.Stringify 删除一维数组的元素级别的括号
- google-colaboratory - 无法打开新的 Google Colab 笔记本
- ios - 在 Swift 中循环对象数组
- android - 有没有办法通过没有服务器的套接字连接两个反应本机应用程序?
- javascript - 无法删除重复的 cookie