geom_tile 使用 ggplotly 时移动的图块
geom_tile tiles displaced when using ggplotly
问题:
当 ggplotly
与 geom_tile
结合使用时,在以下 reprex 中(见下文),瓷砖被置换。
ggplot 输出:
ggplotly 输出:
如您所见,y 值部分偏移 1,x 值部分偏移很多。
plotly 发出以下警告:
版本: 我在使用 R 版本 3.5.3 和 4.0.1、使用 ggplot2 版本 3.1.1/3.3.3 和 plotly 版本 3.9.0 时遇到了同样的问题/4.9.3 分别。
问题:如何解决此问题,使 ggplotly 图表与原始 ggplot2 图表具有相同、正确的图块位置?
代表:
library(ggplot2)
library(plotly)
library(data.table)
dt <- structure(list(x = c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 7, 8, 9, 11, 12, 13, 14, 15,
16, 17, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), y = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L), Color = c(0.105263157894737,
0.0736842105263158, -0.136842105263158, -0.136842105263158, -0.221052631578947,
0.105263157894737, 0.0421052631578947, -0.031578947368421, -0.136842105263158,
-0.178947368421053, -0.210526315789474, -0.242105263157895, -0.263157894736842,
NA, NA, 0.761904761904762, 0.619047619047619, 0.523809523809524,
0.476190476190476, 0.380952380952381, 1.19047619047619, 1.0952380952381,
1.04761904761905, 0.952380952380952, 0.904761904761905, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.436923076923077,
0.313846153846154, 0.704615384615385, 1.56615384615385, 1.44307692307692,
1.19692307692308, 1.13230769230769, 1.04, 0.947692307692308,
0.916923076923077, 0.898461538461538, 0.876923076923077, 1.76923076923077,
0.569105691056911, 0.447154471544715, 0.894308943089431, 0.853658536585366,
0.731707317073171, 0.634146341463415, 0.58130081300813, 0.33739837398374,
0.313008130081301, 0.272357723577236, 0.252032520325203, 0.235772357723577,
1.4390243902439, NA, 1.09195402298851, 1.07471264367816, 1.47701149425287,
1.4367816091954, 1.83908045977012, 2.35632183908046, 2.18390804597701,
1.95402298850575, 1.94252873563218, 1.92528735632184, 1.91379310344828,
1.89080459770115, 3.59770114942529, NA, 0.457227138643068, 0.752212389380531,
0.604719764011799, 0.471976401179941, 0.678466076696165, 0.575221238938053,
0.457227138643068, 0.56047197640118, 0.530973451327434, 0.497050147492625,
0.463126843657817, 0.548672566371681, NA, 0.273665320771646,
0.165993719156572, 0.122625990728279, 0.272169881860326, 0.182443547181098,
0.0373859727830118, -0.091969493046209, -0.153282488410348, -0.108419321070734,
-0.129355465829221, -0.145057574398086, -0.157918349035442, -0.0354419021982952,
NA, 0.336239103362391, 0.62266500622665, 0.460772104607721, 0.361145703611457,
0.24906600249066, 0.161892901618929, 0.386052303860523, 0.311332503113325,
0.261519302615193, 0.23412204234122, 0.207970112079701, 0.183063511830635,
NA, 0.39348710990502, 0.251017639077341, 0.33921302578019, 0.617367706919946,
0.597014925373134, 0.556309362279512, 0.461329715061058, 0.345997286295794,
0.440976933514247, 0.400271370420624, 0.37449118046133, 0.347354138398915,
0.831750339213026, 0.820189274447949, 0.725552050473186, 0.630914826498423,
0.599369085173502, 0.378548895899054, 0.157728706624606, 0.126182965299685,
0.0630914826498423, 0.517350157728707, 0.495268138801262, 0.476340694006309,
0.451104100946372, 0.892744479495268, 0.632743362831858, 0.411504424778761,
0.942477876106195, 0.853982300884956, 0.809734513274336, 0.721238938053097,
0.676991150442478, 0.455752212389381, 0.438053097345133, 0.411504424778761,
0.389380530973451, 0.36283185840708, 0.327433628318584, 0.746268656716418,
0.54726368159204, 0.462686567164179, 0.412935323383085, 0.114427860696517,
0.796019900497512, 0.746268656716418, 0.681592039800995, 0.6318407960199,
0.592039800995025, 0.552238805970149, 0.527363184079602, 0.492537313432836,
NA, 0.402097902097902, 0.340909090909091, 0.236013986013986,
0.131118881118881, 0.236013986013986, 0.0262237762237762, -0.0174825174825175,
-0.0699300699300699, 0.13986013986014, 0.106643356643357, 0.0769230769230769,
0.0629370629370629, 0.048951048951049, NA, 0.476495726495726,
0.391025641025641, 0.252136752136752, 0.273504273504273, 0.412393162393162,
0.391025641025641, 0.358974358974359, 0.273504273504273, 0.145299145299145,
0.123931623931624, 0.0737179487179487, 0.0416666666666667, 0.111111111111111,
NA, 0.777385159010601, 0.600706713780919, 0.353356890459364,
0.49469964664311, 0.459363957597173, 0.653710247349823, 0.618374558303887,
0.547703180212014, 0.512367491166078, 0.484098939929329, 0.45583038869258,
0.431095406360424, 0.696113074204947, NA, 1.06481481481481, 0.925925925925926,
0.694444444444445, 1.01851851851852, 0.694444444444445, 0.648148148148148,
0.601851851851852, 0.569444444444445, 0.541666666666667, 0.523148148148148,
0.50462962962963, 0.490740740740741, 0.851851851851852, NA, 0.574712643678161,
0.498084291187739, 0.383141762452107, 0.651340996168582, 0.53639846743295,
0.762452107279693, 0.67816091954023, 0.448275862068965, 0.409961685823755,
0.333333333333333, 0.283524904214559, 0.245210727969349, 0.532567049808429,
1.04166666666667, 0.925925925925926, 0.740740740740741, 0.717592592592593,
0.625, 0.671296296296296, 0.625, 0.578703703703704, 0.532407407407407,
0.643518518518518, 0.578703703703704, 0.518518518518518, 0.851851851851852,
NA, 0.775193798449612, 0.682170542635659, 0.578811369509044,
0.527131782945736, 1.25064599483204, 1.19896640826873, 0.96640826873385,
0.837209302325581, 0.75968992248062, 0.689922480620155, 0.674418604651163,
0.664082687338501, 0.86046511627907, 0.783132530120482, 0.642570281124498,
0.582329317269076, 0.522088353413655, 0.642570281124498, 0.522088353413655,
0.421686746987952, 0.281124497991968, 0.200803212851406, 0.180722891566265,
0.112449799196787, 0.0562248995983936, 0.305220883534137, 1.89473684210526,
1.78947368421053, 1.73684210526316, 1.47368421052632, 1.47368421052632,
1.36842105263158, 1.26315789473684, 1.21052631578947, 1.10526315789474,
1.10526315789474, NA, NA, NA, NA, NA, NA, NA, NA, 2.25, 2, 2,
2, 1.53153153153153, 1.35135135135135, 1.21621621621622, 1.08108108108108,
0.990990990990991, 0.986486486486486, 0.981981981981982, 0.711711711711712,
0.621621621621622, 0.576576576576577, 0.576576576576577, 0.576576576576577,
1.02702702702703, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.129740518962076, 0.0449101796407186, -0.0998003992015968,
-0.229540918163673, -0.384231536926148, -0.484031936127745, -0.62874251497006,
-0.738522954091816, -0.828343313373254, -0.838323353293413, -0.839321357285429,
-0.839820359281437, -0.800399201596807, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -355L), class = c("data.table",
"data.frame"))
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
p
g <- ggplotly(p)
g
问题是您使用不同的 Color
变量值多次重复相同的坐标。实际上,如果您检查 x
和 y
有多少独特的组合,您的 data.table.
中的行数只有 346 而不是 355
nrow(unique(dt[, .(x, y)]))
# [1] 346
如果您尝试更改示例的这一行:
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
到
p <- ggplot(unique(dt, by = c('x', 'y')), aes(x, y, fill = Color)) + geom_tile()
你将在 ggplot2 和 plotly 中得到相同的图。
最好!
问题:
当 ggplotly
与 geom_tile
结合使用时,在以下 reprex 中(见下文),瓷砖被置换。
ggplot 输出:
如您所见,y 值部分偏移 1,x 值部分偏移很多。
plotly 发出以下警告:
版本: 我在使用 R 版本 3.5.3 和 4.0.1、使用 ggplot2 版本 3.1.1/3.3.3 和 plotly 版本 3.9.0 时遇到了同样的问题/4.9.3 分别。
问题:如何解决此问题,使 ggplotly 图表与原始 ggplot2 图表具有相同、正确的图块位置?
代表:
library(ggplot2)
library(plotly)
library(data.table)
dt <- structure(list(x = c(5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 5, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13,
14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8,
9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5,
6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10,
11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17,
5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5,
5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9,
10, 11, 12, 13, 14, 15, 16, 17, 7, 8, 9, 11, 12, 13, 14, 15,
16, 17, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7,
8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11,
12, 13, 14, 15, 16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
16, 17, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17), y = c(1L,
1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L,
7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L,
8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L,
9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 15L, 15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 17L, 17L, 17L, 17L, 17L,
17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 18L, 18L, 18L, 18L,
18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 19L, 19L, 19L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L, 20L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L,
22L, 22L, 22L, 22L, 22L, 22L, 22L, 22L, 23L, 23L, 23L, 23L, 23L,
23L, 23L, 23L, 23L, 23L, 23L, 23L, 24L, 24L, 24L, 24L, 24L, 24L,
24L, 24L, 24L, 24L, 24L, 24L, 24L, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L), Color = c(0.105263157894737,
0.0736842105263158, -0.136842105263158, -0.136842105263158, -0.221052631578947,
0.105263157894737, 0.0421052631578947, -0.031578947368421, -0.136842105263158,
-0.178947368421053, -0.210526315789474, -0.242105263157895, -0.263157894736842,
NA, NA, 0.761904761904762, 0.619047619047619, 0.523809523809524,
0.476190476190476, 0.380952380952381, 1.19047619047619, 1.0952380952381,
1.04761904761905, 0.952380952380952, 0.904761904761905, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 0.436923076923077,
0.313846153846154, 0.704615384615385, 1.56615384615385, 1.44307692307692,
1.19692307692308, 1.13230769230769, 1.04, 0.947692307692308,
0.916923076923077, 0.898461538461538, 0.876923076923077, 1.76923076923077,
0.569105691056911, 0.447154471544715, 0.894308943089431, 0.853658536585366,
0.731707317073171, 0.634146341463415, 0.58130081300813, 0.33739837398374,
0.313008130081301, 0.272357723577236, 0.252032520325203, 0.235772357723577,
1.4390243902439, NA, 1.09195402298851, 1.07471264367816, 1.47701149425287,
1.4367816091954, 1.83908045977012, 2.35632183908046, 2.18390804597701,
1.95402298850575, 1.94252873563218, 1.92528735632184, 1.91379310344828,
1.89080459770115, 3.59770114942529, NA, 0.457227138643068, 0.752212389380531,
0.604719764011799, 0.471976401179941, 0.678466076696165, 0.575221238938053,
0.457227138643068, 0.56047197640118, 0.530973451327434, 0.497050147492625,
0.463126843657817, 0.548672566371681, NA, 0.273665320771646,
0.165993719156572, 0.122625990728279, 0.272169881860326, 0.182443547181098,
0.0373859727830118, -0.091969493046209, -0.153282488410348, -0.108419321070734,
-0.129355465829221, -0.145057574398086, -0.157918349035442, -0.0354419021982952,
NA, 0.336239103362391, 0.62266500622665, 0.460772104607721, 0.361145703611457,
0.24906600249066, 0.161892901618929, 0.386052303860523, 0.311332503113325,
0.261519302615193, 0.23412204234122, 0.207970112079701, 0.183063511830635,
NA, 0.39348710990502, 0.251017639077341, 0.33921302578019, 0.617367706919946,
0.597014925373134, 0.556309362279512, 0.461329715061058, 0.345997286295794,
0.440976933514247, 0.400271370420624, 0.37449118046133, 0.347354138398915,
0.831750339213026, 0.820189274447949, 0.725552050473186, 0.630914826498423,
0.599369085173502, 0.378548895899054, 0.157728706624606, 0.126182965299685,
0.0630914826498423, 0.517350157728707, 0.495268138801262, 0.476340694006309,
0.451104100946372, 0.892744479495268, 0.632743362831858, 0.411504424778761,
0.942477876106195, 0.853982300884956, 0.809734513274336, 0.721238938053097,
0.676991150442478, 0.455752212389381, 0.438053097345133, 0.411504424778761,
0.389380530973451, 0.36283185840708, 0.327433628318584, 0.746268656716418,
0.54726368159204, 0.462686567164179, 0.412935323383085, 0.114427860696517,
0.796019900497512, 0.746268656716418, 0.681592039800995, 0.6318407960199,
0.592039800995025, 0.552238805970149, 0.527363184079602, 0.492537313432836,
NA, 0.402097902097902, 0.340909090909091, 0.236013986013986,
0.131118881118881, 0.236013986013986, 0.0262237762237762, -0.0174825174825175,
-0.0699300699300699, 0.13986013986014, 0.106643356643357, 0.0769230769230769,
0.0629370629370629, 0.048951048951049, NA, 0.476495726495726,
0.391025641025641, 0.252136752136752, 0.273504273504273, 0.412393162393162,
0.391025641025641, 0.358974358974359, 0.273504273504273, 0.145299145299145,
0.123931623931624, 0.0737179487179487, 0.0416666666666667, 0.111111111111111,
NA, 0.777385159010601, 0.600706713780919, 0.353356890459364,
0.49469964664311, 0.459363957597173, 0.653710247349823, 0.618374558303887,
0.547703180212014, 0.512367491166078, 0.484098939929329, 0.45583038869258,
0.431095406360424, 0.696113074204947, NA, 1.06481481481481, 0.925925925925926,
0.694444444444445, 1.01851851851852, 0.694444444444445, 0.648148148148148,
0.601851851851852, 0.569444444444445, 0.541666666666667, 0.523148148148148,
0.50462962962963, 0.490740740740741, 0.851851851851852, NA, 0.574712643678161,
0.498084291187739, 0.383141762452107, 0.651340996168582, 0.53639846743295,
0.762452107279693, 0.67816091954023, 0.448275862068965, 0.409961685823755,
0.333333333333333, 0.283524904214559, 0.245210727969349, 0.532567049808429,
1.04166666666667, 0.925925925925926, 0.740740740740741, 0.717592592592593,
0.625, 0.671296296296296, 0.625, 0.578703703703704, 0.532407407407407,
0.643518518518518, 0.578703703703704, 0.518518518518518, 0.851851851851852,
NA, 0.775193798449612, 0.682170542635659, 0.578811369509044,
0.527131782945736, 1.25064599483204, 1.19896640826873, 0.96640826873385,
0.837209302325581, 0.75968992248062, 0.689922480620155, 0.674418604651163,
0.664082687338501, 0.86046511627907, 0.783132530120482, 0.642570281124498,
0.582329317269076, 0.522088353413655, 0.642570281124498, 0.522088353413655,
0.421686746987952, 0.281124497991968, 0.200803212851406, 0.180722891566265,
0.112449799196787, 0.0562248995983936, 0.305220883534137, 1.89473684210526,
1.78947368421053, 1.73684210526316, 1.47368421052632, 1.47368421052632,
1.36842105263158, 1.26315789473684, 1.21052631578947, 1.10526315789474,
1.10526315789474, NA, NA, NA, NA, NA, NA, NA, NA, 2.25, 2, 2,
2, 1.53153153153153, 1.35135135135135, 1.21621621621622, 1.08108108108108,
0.990990990990991, 0.986486486486486, 0.981981981981982, 0.711711711711712,
0.621621621621622, 0.576576576576577, 0.576576576576577, 0.576576576576577,
1.02702702702703, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 0.129740518962076, 0.0449101796407186, -0.0998003992015968,
-0.229540918163673, -0.384231536926148, -0.484031936127745, -0.62874251497006,
-0.738522954091816, -0.828343313373254, -0.838323353293413, -0.839321357285429,
-0.839820359281437, -0.800399201596807, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA)), row.names = c(NA, -355L), class = c("data.table",
"data.frame"))
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
p
g <- ggplotly(p)
g
问题是您使用不同的 Color
变量值多次重复相同的坐标。实际上,如果您检查 x
和 y
有多少独特的组合,您的 data.table.
nrow(unique(dt[, .(x, y)]))
# [1] 346
如果您尝试更改示例的这一行:
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
到
p <- ggplot(unique(dt, by = c('x', 'y')), aes(x, y, fill = Color)) + geom_tile()
你将在 ggplot2 和 plotly 中得到相同的图。
最好!