重塑坐标列表(在 Y 上堆叠 X)

Reshaping coordinate list (stack X over Y)

我有一个数据框,其中包含一些坐标 X、Y 和一个类型。 数据框如下所示:

structure(list(Typ = c("Blue", "Blue", "Blue", 
"Blue", "Green", "Red", "Blue", "Blue", "Blue", 
"Red", "Red", "Red", "Cyan", "Cyan", "Cyan", "Blue", "Green", 
"Red", "Green", "Cyan", "Cyan", "Green", "Cyan", "Red", "Red", 
"Blue", "Red", "Blue", "Yellow", "Green"), X = c(429L, 
432L, 451L, 324L, 442L, 577L, 597L, 411L, 598L, 371L, 455L, 469L, 
380L, 437L, 316L, 387L, 414L, 482L, 506L, 478L, 496L, 574L, 395L, 
554L, 344L, 496L, 516L, 579L, 586L, 412L), Y = c(310L, 311L, 
311L, 312L, 312L, 312L, 312L, 314L, 314L, 315L, 315L, 315L, 316L, 
316L, 317L, 317L, 318L, 318L, 318L, 320L, 320L, 320L, 321L, 321L, 
322L, 322L, 322L, 322L, 323L, 324L)), .Names = c("Typ", "X", 
"Y"), row.names = c(NA, 30L), class = "data.frame")

并会得到如下所示的输出。 此外,最好将 Typ 作为 X 坐标的颜色包含在内。

Y:   X:
310: 429
311: 432 451
312: 324 442 577 597
314: 411 598
315: 371 455 469
316: 380 437
317: 316 387
....

我们可以使用 base R 中的 aggregate 为每个 'Y'

创建 list 个 'X' 值
aggregate(X~Y, df, I)
#    Y                  X
#1  310                429
#2  311           432, 451
#3  312 324, 442, 577, 597
#4  314           411, 598
#5  315      371, 455, 469
#6  316           380, 437
#7  317           316, 387
#8  318      414, 482, 506
#9  320      478, 496, 574
#10 321           395, 554
#11 322 344, 496, 516, 579
#12 323                586
#13 324                412