Issue:
When using ggplotly in combination with geom_tile, in the following reprex (see below), tiles are displaced.
ggplot output:
ggplotly output:
As you can see, the y-values are partially off-placed by one and the x-values are partially off-placed by a lot.
plotly raises the following warning:
Versions: I have the same issue using R version 3.5.3 and 4.0.1., using ggplot2 version 3.1.1/3.3.3 and plotly version 3.9.0/4.9.3 respectively.
Question: How can I fix this issue such that the ggplotly graph has the same, correct tile placement as the original ggplot2 graph?
Reprex:
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
The problem is that you have the same coordinates repeated more than once with different values for the Color variable. Indeed if you check how many unique combinations for x and y you have there are only 346 not 355 as the number of rows in your data.table.
nrow(unique(dt[, .(x, y)]))
# [1] 346
If you try changing this line of your example:
p <- ggplot(dt, aes(x, y, fill = Color)) + geom_tile()
to
p <- ggplot(unique(dt, by = c('x', 'y')), aes(x, y, fill = Color)) + geom_tile()
you will get the same plot in both ggplot2 and plotly.
Best!
When making a geom_point() that has both color = column_1 and size = column_2 options passed through, ggplot provides two separate legends. One for the color column and one for the size. This is great.
I would like to split the two legends so the bit which maps onto color is shown across the top horizontally and the bit that maps onto size is shown on the right-hand side of the plotregion vertically.
The data and code below reproduce the graph shown below. In that graph I would like the size shown on the right-hand size of the graph vertically and the bit that maps onto the actor's name to be shown along the top as it is.
Is this kind of thing possible? I've found ways to put both of them on the left-hand side but that's not really what I want as you read the actor's name left to right in the plot, and you read size top to bottom, so I want the legends to display in the same way the reader would naturally read the data.
df <- structure(list(count = c(1025, 360, 625, 1108, 3018, 7376, 16318,
19114, 16947, 21532, 2088, 923, 1109, 1751, 3710, 7160, 13904,
20096, 17049, 24597, 2094, 607, 817, 1340, 2909, 6667, 13870,
18657, 17502, 34533, 1132, 447, 606, 940, 2038, 4564, 12141,
19197, 18426, 31272, 1144, 387, 646, 1081, 2164, 5451, 12343,
16194, 16783, 24880, 1450, 549, 759, 1278, 2568, 5623, 11406,
15957, 16445, 22850, 1707, 788, 1023, 1594, 3292, 6852, 14749,
18550, 13815, 19754, 1977, 819, 1051, 1522, 2873, 5469, 10692,
14740, 12352, 16335, 1256, 554, 633, 946, 1780, 3301, 6260, 10608,
11575, 20720, 1365, 547, 565, 1066, 2177, 4650, 9590, 11570,
8160, 11119, 13175, 3088, 2869, 3375, 5123, 7292, 9714, 9088,
5927, 10775, 8387, 1954, 1817, 1996, 2776, 3972, 5746, 5968,
3965, 5969), doctor = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 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, 3L, 3L,
3L, 3L, 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, 4L, 4L, 4L, 4L,
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, 5L, 5L, 5L, 5L, 5L, 5L,
5L), .Label = c("Christopher Eccleston", "David Tennant", "Matt Smith",
"Peter Capaldi", "Jodie Whitaker"), class = "factor"), rating = c(1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 1,
2, 3, 4, 5, 6, 7, 8, 9, 10, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10), season_num = c(27L,
27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 28L, 28L, 28L, 28L,
28L, 28L, 28L, 28L, 28L, 28L, 29L, 29L, 29L, 29L, 29L, 29L, 29L,
29L, 29L, 29L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L, 30L,
31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 31L, 32L, 32L, 32L,
32L, 32L, 32L, 32L, 32L, 32L, 32L, 33L, 33L, 33L, 33L, 33L, 33L,
33L, 33L, 33L, 33L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L, 34L,
34L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 35L, 36L, 36L,
36L, 36L, 36L, 36L, 36L, 36L, 36L, 36L, 37L, 37L, 37L, 37L, 37L,
37L, 37L, 37L, 37L, 37L, 38L, 38L, 38L, 38L, 38L, 38L, 38L, 38L,
38L, 38L)), row.names = c(NA, -120L), groups = structure(list(
season_num = 27:38, .rows = structure(list(1:10, 11:20, 21:30,
31:40, 41:50, 51:60, 61:70, 71:80, 81:90, 91:100, 101:110,
111:120), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -12L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
df %>%
ggplot() +
geom_point(aes(x = factor(season_num), y = rating, size = count, color = doctor)) +
labs(x = "Season", y = "Rating (1-10)", title = "IMDb ratings distributions by Season") +
theme(legend.position = 'top',
legend.title = element_blank(),
plot.title = element_text(size = 10),
axis.title.x = element_text(size = 10),
axis.title.y = element_text(size = 10)) +
scale_size_continuous(range = c(1,8)) +
scale_y_continuous(limits=c(1, 10), breaks=c(seq(1, 10, by = 1))) +
scale_x_discrete(breaks=c(seq(27, 38, by = 1))) +
scale_color_brewer(palette = "Dark2")
I do not think this is possible with ggplot2-only functions. However, a common trick is:
to make a plot without the legend,
make other plots with target legends,
extract the legends from these plots,
arrange everything in a grid using packages like cowplot or gridExtra
You can find some examples of this process on SO:
ggplot - Multiple legends arrangement
How to place legends at different sides of plot (bottom and right side) with ggplot2?
How do I position two legends independently in ggplot
Here is an example with the provided data, I have not put much effort in arranging the grid because it can change a lot depending on the package you choose in the end. It is just to showcase the process.
library(cowplot)
library(ggplot2)
# plot without legend
main_plot <- ggplot(data = df) +
geom_point(aes(x = factor(season_num), y = rating, size = count, color = doctor)) +
labs(x = "Season", y = "Rating (1-10)", title = "IMDb ratings distributions by Season") +
theme(legend.position = 'none',
legend.title = element_blank(),
plot.title = element_text(size = 10),
axis.title.x = element_text(size = 10),
axis.title.y = element_text(size = 10)) +
scale_size_continuous(range = c(1,8)) +
scale_y_continuous(limits=c(1, 10), breaks=c(seq(1, 10, by = 1))) +
scale_x_discrete(breaks=c(seq(27, 38, by = 1))) +
scale_color_brewer(palette = "Dark2")
# color legend, top, horizontally
color_plot <- ggplot(data = df) +
geom_point(aes(x = factor(season_num), y = rating, color = doctor)) +
theme(legend.position = 'top',
legend.title = element_blank()) +
scale_color_brewer(palette = "Dark2")
color_legend <- cowplot::get_legend(color_plot)
# size legend, right-hand side, vertically
size_plot <- ggplot(data = df) +
geom_point(aes(x = factor(season_num), y = rating, size = count)) +
theme(legend.position = 'right',
legend.title = element_blank()) +
scale_size_continuous(range = c(1,8))
size_legend <- cowplot::get_legend(size_plot)
# combine all these elements
cowplot::plot_grid(plotlist = list(color_legend,NULL, main_plot, size_legend),
rel_heights = c(1, 5),
rel_widths = c(4, 1))
Output:
I have a huge data, please bear with me.
df <- structure(list(W = c(5216400.4123, 5399804.7349, 5595563.3087,
5792353.9932, 5993467.7466, 6189404.9279, 6380940.454, 6566630.3544,
6747453.6816, 6917820.9796, 7086201.8275, 7248213.5225, 5402700.4252,
5592654.9057, 5795404.8549, 5999223.7818, 6207520.1695, 6410455.1037,
6608831.1825, 6801152.8706, 6988434.1695, 7164886.0132, 7339280.46,
7507078.2886, 5589000.4397, 5785505.0748, 5995246.3993, 6206093.5662,
6421572.589, 6631505.2817, 6836721.9131, 7035675.3828, 7229414.6578,
7411951.0459, 7592359.0997, 7765943.0584, 5775300.4552, 5978355.2455,
6195087.9484, 6412963.3504, 6635625.0103, 6852555.4544, 7064612.6447,
7270197.897, 7470395.1463, 7659016.0859, 7845437.7356, 8024807.8293,
5961600.4694, 6171205.4145, 6394929.4952, 6619833.1358, 6849677.4283,
7073605.633, 7292503.372, 7504720.4062, 7711375.6354, 7906081.1161,
8098516.3724, 8283672.5987, 6147900.4816, 6364055.583, 6594771.0412,
6826702.9212, 7063729.8478, 7294655.8114, 7520394.1023, 7739242.9195,
7952356.1253, 8153146.1541, 8351595.0092, 8542537.3639, 6334200.5005,
6556905.7553, 6794612.5881, 7033572.7089, 7277782.2661, 7515705.9882,
7748284.8337, 7973765.4322, 8193336.6123, 8400211.1873, 8604673.6477,
8801402.1321, 6520500.5136, 6749755.9226, 6994454.1327, 7240442.494,
7491834.6868, 7736756.1617, 7976175.5621, 8208287.9459, 8434317.1037,
8647276.2228, 8857752.2827, 9060266.9029, 6706800.528, 6942606.0899,
7194295.6809, 7447312.2777, 7705887.1079, 7957806.3409, 8204066.2961,
8442810.4584, 8675297.5886, 8894341.2573, 9110830.9206, 9319131.6718,
6893100.5418, 7135456.2602, 7394137.2288, 7654182.0653, 7919939.5232,
8178856.5136, 8431957.0234, 8677332.969, 8916278.0804, 9141406.2918,
9363909.5543, 9577996.4404, 7079400.5588, 7328306.4296, 7593978.7729,
7861051.8503, 8133991.9462, 8399906.6912, 8659847.7579, 8911855.4821,
9157258.5684, 9388471.3277, 9616988.1903, 9836861.209, 7265700.5699,
7521156.5994, 7793820.3185, 8067921.639, 8348044.3652, 8620956.868,
8887738.4844, 9146377.9962, 9398239.0552, 9635536.362, 9870066.8286,
10095725.9764, 7377480.5806, 7636866.7018, 7913725.2471, 8192043.5082,
8476475.8166, 8753586.9712, 9024472.9255, 9287091.5011, 9542827.3494,
9783775.3823, 10021914.0099, 10251044.8357, 7452000.5846, 7714006.7682,
7993661.8669, 8274791.4235, 8562096.7846, 8842007.041, 9115629.2162,
9380900.5049, 9639219.5459, 9882601.398, 10123145.4642, 10354590.7474,
7526520.5952, 7791146.8356, 8073598.4841, 8357539.3348, 8647717.7534,
8930427.113, 9206785.5068, 9474709.5143, 9735611.7438, 9981427.4094,
10224376.9194, 10458136.6527, 7638300.6025, 7906856.9365, 8193503.4143,
8481661.2074, 8776149.2063, 9063057.2172, 9343519.9463, 9615423.0209,
9880200.0383, 10129666.4312, 10376224.1032, 10613455.5131, 7824600.6172,
8099707.1074, 8393344.9601, 8688530.993, 8990201.627, 9284107.395,
9571410.6772, 9849945.5329, 10121180.5245, 10376731.4677, 10629302.7393,
10872320.2844, 8010900.6306, 8292557.2766, 8593186.5077, 8895400.7788,
9204254.0428, 9505157.5709, 9799301.4089, 10084468.0455, 10362161.0109,
10623796.504, 10882381.3729, 11131185.0534, 8197200.6444, 8485407.4453,
8793028.0567, 9102270.5614, 9418306.465, 9726207.7477, 10027192.1371,
10318990.5604, 10603141.4991, 10870861.5368, 11135460.012, 11390049.8203,
8383500.6593, 8678257.6128, 8992869.6005, 9309140.3471, 9632358.8856,
9947257.9225, 10255082.8657, 10553513.0731, 10844121.988, 11117926.5692,
11388538.6484, 11648914.5899, 8569800.6735, 8871107.7799, 9192711.1487,
9516010.1349, 9846411.3046, 10168308.0987, 10482973.5998, 10788035.5824,
11085102.4738, 11364991.6051, 11641617.2821, 11907779.3576, 8756100.6891,
9063957.9534, 9392552.6936, 9722879.9204, 10060463.7195, 10389358.2746,
10710864.3279, 11022558.0956, 11326082.9668, 11612056.6477, 11894695.9209,
12166644.1258, 8942400.705, 9256808.1204, 9592394.2396, 9929749.7037,
10274516.1413, 10610408.4494, 10938755.0613, 11257080.6099, 11567063.455,
11859121.6771, 12147774.5594, 12425508.8956, 9128700.7203, 9449658.2901,
9792235.786, 10136619.4908, 10488568.5635, 10831458.6249, 11166645.792,
11491603.1215, 11808043.9427, 12106186.7106, 12400853.1962, 12684373.664,
9315000.7353, 9642508.4595, 9992077.3349, 10343489.2758, 10702620.9802,
11052508.7998, 11394536.5185, 11726125.6341, 12049024.431, 12353251.7462,
12653931.8307, 12943238.4324, 9501300.7465, 9835358.6307, 10191918.8807,
10550359.0623, 10916673.4022, 11273558.9758, 11622427.2509, 11960648.1479,
12290004.92, 12600316.7835, 12907010.4688, 13202103.2002, 9687600.7612,
10028208.7979, 10391760.4269, 10757228.8467, 11130725.8198, 11494609.1532,
11850317.9787, 12195170.6632, 12530985.4111, 12847381.8161, 13160089.1051,
13460967.9711), t = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L,
3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L,
6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 10L, 11L, 12L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L,
12L), .Label = c("t_m75_tc", "t_m5_tc", "t_m25_tc", "t_p0_tc",
"t_p25_tc", "t_p5_tc", "t_p75_tc", "t_p10_tc", "t_p125_tc", "t_p15_tc",
"t_p175_tc", "t_p20_tc"), class = "factor"), p = structure(c(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, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 5L,
5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 6L, 6L, 6L, 6L, 6L,
6L, 6L, 6L, 6L, 6L, 6L, 6L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L, 7L,
7L, 7L, 7L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 8L, 9L,
9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 9L, 10L, 10L, 10L, 10L,
10L, 10L, 10L, 10L, 10L, 10L, 10L, 10L, 11L, 11L, 11L, 11L, 11L,
11L, 11L, 11L, 11L, 11L, 11L, 11L, 12L, 12L, 12L, 12L, 12L, 12L,
12L, 12L, 12L, 12L, 12L, 12L, 13L, 13L, 13L, 13L, 13L, 13L, 13L,
13L, 13L, 13L, 13L, 13L, 14L, 14L, 14L, 14L, 14L, 14L, 14L, 14L,
14L, 14L, 14L, 14L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L, 15L,
15L, 15L, 15L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L, 16L,
16L, 16L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L, 17L,
17L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L, 18L,
19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 19L, 20L,
20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 20L, 21L, 21L,
21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 21L, 22L, 22L, 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, 25L, 25L, 25L, 25L, 25L, 25L,
25L, 25L, 25L, 25L, 25L, 25L, 26L, 26L, 26L, 26L, 26L, 26L, 26L,
26L, 26L, 26L, 26L, 26L, 27L, 27L, 27L, 27L, 27L, 27L, 27L, 27L,
27L, 27L, 27L, 27L), .Label = c("h_30", "h_27.5", "h_25", "h_22.5",
"h_20", "h_17.5", "h_15", "h_12.5", "h_10", "h_7.5", "h_5", "h_2.5",
"h_1", "h0", "h1", "h2.5", "h5", "h7.5", "h10", "h12.5", "h15",
"h17.5", "h20", "h22.5", "h25", "h27.5", "h30"), class = "factor"),
tt = c(-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20,
-7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5,
-5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5,
-2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5,
0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0,
2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5,
5, 7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5,
7.5, 10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5,
10, 12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10,
12.5, 15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5,
15, 17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15,
17.5, 20, -7.5, -5, -2.5, 0, 2.5, 5, 7.5, 10, 12.5, 15, 17.5,
20), hh = c(-30, -30, -30, -30, -30, -30, -30, -30, -30,
-30, -30, -30, -27.5, -27.5, -27.5, -27.5, -27.5, -27.5,
-27.5, -27.5, -27.5, -27.5, -27.5, -27.5, -25, -25, -25,
-25, -25, -25, -25, -25, -25, -25, -25, -25, -22.5, -22.5,
-22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5, -22.5,
-22.5, -20, -20, -20, -20, -20, -20, -20, -20, -20, -20,
-20, -20, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5, -17.5,
-17.5, -17.5, -17.5, -17.5, -17.5, -15, -15, -15, -15, -15,
-15, -15, -15, -15, -15, -15, -15, -12.5, -12.5, -12.5, -12.5,
-12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -12.5, -10,
-10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -10, -7.5,
-7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5, -7.5,
-7.5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -5, -2.5,
-2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5, -2.5,
-2.5, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1,
1, 1, 1, 1, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5, 2.5,
2.5, 2.5, 2.5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 7.5, 7.5,
7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 7.5, 10, 10,
10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 12.5, 12.5, 12.5,
12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 12.5, 15,
15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 15, 17.5, 17.5, 17.5,
17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 17.5, 20,
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 22.5, 22.5, 22.5,
22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 22.5, 25,
25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 25, 27.5, 27.5, 27.5,
27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 27.5, 30,
30, 30, 30, 30, 30, 30, 30, 30, 30, 30, 30), pChange = c(-36.9603395985828,
-34.7439172960321, -32.3781951432773, -30.0000000392759,
-27.5695610939649, -25.2016804880133, -22.8869934306931,
-20.6429501564101, -18.4577189167867, -16.3988477104845,
-14.3639825485445, -12.4060879418008, -34.7089231777299,
-32.4133428932464, -29.9631306906258, -27.5000000028702,
-24.982759663634, -22.5303119363876, -20.1329575059591, -17.8087697620382,
-15.5454946011909, -13.4130922883195, -11.3055534045631,
-9.27773397066822, -32.4575067375413, -30.082768511005, -27.5480662597272,
-25.000000017221, -22.3959582743917, -19.8589433581751, -17.3789215558468,
-14.974589416006, -12.6332702807612, -10.4273368770308, -8.24712417357041,
-6.14937995482153, -30.2060902852677, -27.7521941094277,
-25.1330017720295, -22.5000000339888, -19.8091568633966,
-17.1875748440126, -14.6248855936495, -12.1404090458039,
-9.72104595791446, -7.44158137752243, -5.18869498850033,
-3.02102592568144, -27.9546738487045, -25.4216197283948,
-22.7179373121271, -20.0000000362547, -17.2223554922816,
-14.5162062585492, -11.8708496834174, -9.30622873602633,
-6.80882162781682, -4.45582599644603, -2.13026579255384,
0.107328085331291, -25.7032574363111, -23.0910453534044,
-20.3028728618926, -17.5000000385206, -14.6355541030393,
-11.8448376755028, -9.11681373693055, -6.47204837670069,
-3.89659728805126, -1.47007052110743, 0.928163403392649,
3.23568204558746, -23.4518409429489, -20.7604709324913, -17.8878084007817,
-15.0000000129913, -12.0487527282989, -9.17346911179217,
-6.36277777715033, -3.63786802462598, -0.984372983331903,
1.51568489622367, 3.98659261988347, 6.36403604209831, -21.2004245196791,
-18.4298965720027, -15.4727439674661, -12.5000000188826,
-9.46195132455473, -6.50210058796172, -3.6087418536248, -0.803687660466381,
1.92785137456101, 4.50144034135001, 7.04502179407714, 9.4923900700299,
-18.949008080699, -16.0993222115141, -13.0576794906449, -10.0000000416929,
-6.8751499159766, -3.83073199524737, -0.85470586242384, 2.03049268919135,
4.84007565390208, 7.48719577439149, 10.103451003317, 12.6207440750002,
-16.6975916489697, -13.7687478147708, -10.6426150174491,
-7.50000001737204, -4.28834857749088, -1.15936348108484,
1.8993300478083, 4.86467301588778, 7.75230001662893, 10.4729512074329,
13.1618801618003, 15.749098076345, -14.4461751785688, -11.4381734289039,
-8.2275505901759, -5.00000002447191, -1.70154714595145, 1.51200509229367,
4.65336604505171, 7.69885337279645, 10.6645243334332, 13.4587066573933,
16.2203093480789, 18.8774520776898, -12.1947587794688, -9.10759903820312,
-5.81248614477539, -2.49999998685766, 0.885254237248392,
4.18337365600429, 7.40740194561595, 10.53303374179, 13.5767486357356,
16.4444620880177, 19.2787385621527, 22.0058060645328, -10.8439088911858,
-7.70925439749847, -4.3634474625498, -1.00000001287041, 2.43733506716831,
5.78619475942614, 9.05982354879595, 12.2335419201639, 15.324083242737,
18.2359153430087, 21.1137960702944, 23.8828184428618, -9.94334233686319,
-6.77702465958718, -3.39742166553716, 0, 3.47205562528221,
6.85474217379226, 10.1614379102301, 13.3672140455251, 16.4889729851691,
19.4302175391865, 22.3371677435973, 25.1341600948813, -9.04277570278022,
-5.84479490959099, -2.43139589994524, 0.999999964530837,
4.50677619306401, 7.92328961474518, 11.2630522704558, 14.5008862385618,
17.65386274452, 20.6245196833994, 23.5605394277767, 26.3855016695576,
-7.69192585558588, -4.44645028701369, -0.982357198383821,
2.49999997960674, 6.05885704111126, 9.52611073025193, 12.9154738543,
16.20139443748, 19.4011973551469, 22.4159729565176, 25.3955969661306,
28.26251406118, -5.44050941298024, -2.11587588301946, 1.43270724943368,
4.99999997975781, 8.64565844485543, 12.1974793060474, 15.6695098080378,
19.0355747810953, 22.3134216501983, 25.4017284137289, 28.4540261536177,
31.390868095154, -3.18909298608498, 0.214698500430437, 3.84777171900399,
7.49999998232584, 11.2324597893836, 14.8688478588816, 18.4235457714435,
21.8697551319615, 25.2256459476667, 28.3874838685232, 31.5124553108925,
34.5192221013328, -0.93767655435575, 2.54527287783788, 6.26283620549316,
9.99999994622222, 13.8192612112551, 17.5402164225922, 21.177581692552,
24.703935510623, 28.1378702668879, 31.3732392810203, 34.5708845346342,
37.6475760821333, 1.31373989066688, 4.87584724074345, 8.67790062914087,
12.4999999475818, 16.4060626137908, 20.211584962133, 23.9316176184945,
27.5381158626977, 31.0500945945686, 34.3589946886835, 37.6293137257467,
40.775930095563, 3.56515632723005, 7.20642159881506, 11.0929651059621,
14.9999999743196, 18.9928639969906, 22.8829535185927, 26.685653610904,
30.3722961736837, 33.9623188847861, 37.3447501386438, 40.6877428842301,
43.9042840860314, 5.81657278071209, 9.53699603423, 13.5080295429032,
17.4999999732622, 21.5796653306424, 25.5543220714269, 29.439689530804,
33.2064765318009, 36.8745432620148, 40.3305056695729, 43.7461721043463,
47.0326380825422, 8.06798923781962, 11.8675703910931, 15.9230939931377,
19.999999945618, 24.16646674768, 28.2256906109677, 32.1937255147541,
36.0406569032114, 39.7867675812361, 43.3162610409814, 46.8046013208372,
50.1609920983889, 10.3194056876762, 14.1981447805855, 18.3381584482061,
22.4999999638964, 26.7532681695515, 30.8970591589679, 34.9477614660749,
38.8748372419928, 42.6989918944149, 46.302016461938, 49.8630305167836,
53.2893460973168, 12.5708221339073, 16.5287191664524, 20.7532229334868,
24.9999999567965, 29.3400695249561, 33.5684276997172, 37.7017973666391,
41.709017592859, 45.6112162148446, 49.2877719082728, 52.9214596849349,
56.4177000962446, 14.8222385342158, 18.859293574072, 23.1682873813043,
27.499999967824, 31.9268709444106, 36.2397962537599, 40.4558333385042,
44.5431979582271, 48.5234405437337, 52.273527375152, 55.9798888965917,
59.5460540879215, 17.0736549768214, 21.1898679333522, 25.5833518339557,
29.9999999534732, 34.5136723106916, 38.9111648247215, 43.2098692547788,
47.3773783417225, 51.4356648980012, 55.2592827852321, 59.0383180864958,
62.6744081170616)), class = "data.frame", .Names = c("W",
"t", "p", "tt", "hh", "pChange"), row.names = c(NA, -324L))
I am trying to plot a colored contour map with the columns tt, hh and W, W being the z axis.
This is the code I am using::
ggplot(df, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
This is the result I got:
What I want is the color to be continuous, as seen here I am following this example for my work.
What is going wrong over here?
The grid is not evenly spaced. One way to make an evenly spaced grid is to use interpolate using loess on an evenly spaced grid:
model <- loess(W ~ tt + hh, data = df)
create an evenly spaced grid using expand.grid:
new.data <- expand.grid(tt = seq(from = min(df$tt), to = max(df$tt), length.out = 500),
hh = seq(from = min(df$hh), to = max(df$hh), length.out = 500))
predict on new data using the model:
gg <- predict(model, newdata = new.data)
combine prediction and new data:
new.data = data.frame(W = as.vector(gg),
new.data)
and now the plot looks like:
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
You might also want to check some goodness of fit metric for loess
caret::RMSE(model$fitted, df$W)
#output
7498.393
using a narrower span could provide a better fit, especially if the data is not smooth:
model2 <- loess(W ~ tt + hh, data = df, span = 0.1)
caret::RMSE(model2$fitted, df$W)
#output
964.7582
ggplot(new.data2, aes(x = tt, y = hh, z = W)) +
stat_contour(geom = "polygon", aes(fill = ..level..) ) +
geom_tile(aes(fill = W)) +
stat_contour(bins = 10) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W"))
The difference is ever so slight
ggplot(new.data, aes(x = tt, y = hh, z = W)) +
geom_tile(aes(fill = W)) +
geom_contour(aes(x = tt, y = hh, z = W),
color = "red")+
geom_contour(data = new.data2,
aes(x = tt, y = hh, z = W),
color = "white", inherit.aes = FALSE)
EDIT: also check the great post by #Henrik which is linked by him in the comment. Especially the ?akima::interp function.
EDIT2: answer to the questions in comments:
To specify a different fill one can use
scale_fill_gradient
scale_fill_gradient2
scale_fill_gradientn
Here is an example of using scale_fill_gradientn with 5 colors based on quantiles:
v <- ggplot(new.data2, aes(x = tt, y = hh, z = floor(W))) +
geom_tile(aes(fill = W), show.legend = FALSE) +
stat_contour(bins = 10, aes(colour = ..level..)) +
xlab("% change in temperature") +
ylab("% change in ppt") +
guides(fill = guide_colorbar(title = "W")) +
scale_fill_gradientn(values = scales::rescale(quantile(new.data2$W)),
colors = rainbow(5))
I removed the polygon thing since it was below the geom_tile layer and was not visible.
To add direct labels:
library(directlabels)
direct.label(v, list("far.from.others.borders", "calc.boxes", "enlarge.box",
box.color = NA, fill = "transparent", "draw.rects"))