Make42
Make42

Reputation: 13108

Get geom_contour / geom_tile to work with my data

I would like to plot the following data with ggplot2:

res = structure(list(x = c(0, 0, 0, 0, 0, 
0, 0, 0, 0, 0, 0.301029995663981, 0.301029995663981, 0.301029995663981, 
0.301029995663981, 0.301029995663981, 0.301029995663981, 0.477121254719662, 
0.477121254719662, 0.477121254719662, 0.477121254719662, 0.477121254719662, 
0.602059991327962, 0.602059991327962, 0.698970004336019, 0.698970004336019, 
0.698970004336019, 0.778151250383644, 0.778151250383644, 0.903089986991944, 
1, 1, 1, 1.04139268515823, 1.04139268515823, 1.07918124604762, 
1.07918124604762, 1.14612803567824, 1.14612803567824, 1.17609125905568, 
1.17609125905568, 1.17609125905568, 1.20411998265592, 1.23044892137827, 
1.25527250510331, 1.25527250510331, 1.27875360095283, 1.30102999566398, 
1.30102999566398, 1.30102999566398, 1.30102999566398, 1.34242268082221, 
1.34242268082221, 1.38021124171161, 1.39794000867204, 1.39794000867204, 
1.41497334797082, 1.47712125471966, 1.47712125471966, 1.47712125471966, 
1.47712125471966, 1.51851393987789, 1.53147891704226, 1.54406804435028, 
1.55630250076729, 1.57978359661681, 1.57978359661681, 1.60205999132796, 
1.60205999132796, 1.6232492903979, 1.6232492903979, 1.64345267648619, 
1.65321251377534, 1.66275783168157, 1.68124123737559, 1.7160033436348, 
1.7160033436348, 1.73239375982297, 1.74036268949424, 1.75587485567249, 
1.77815125038364, 1.77815125038364, 1.77815125038364, 1.81954393554187, 
1.8750612633917, 1.88081359228079, 1.88649072517248, 1.89762709129044, 
1.90308998699194, 1.94448267215017, 1.95424250943932, 1.97772360528885, 
1.99563519459755, 2.00432137378264, 2.01703333929878, 2.02118929906994, 
2.02530586526477, 2.04532297878666, 2.05690485133647, 2.07918124604762, 
2.07918124604762, 2.08635983067475, 2.12385164096709, 2.12385164096709, 
2.13033376849501, 2.14921911265538, 2.15533603746506, 2.18184358794477, 
2.19312459835446, 2.20411998265592, 2.2148438480477, 2.23299611039215, 
2.25527250510331, 2.26245108973043, 2.26481782300954, 2.2718416065365, 
2.31806333496276, 2.32837960343874, 2.34635297445064, 2.37839790094814, 
2.38021124171161, 2.41497334797082, 2.45024910831936, 2.47712125471966, 
2.49415459401844, 2.52244423350632, 2.55630250076729, 2.56110138364906, 
2.56348108539441, 2.61909333062674, 2.6222140229663, 2.62634036737504, 
2.64738297011462, 2.67117284271508, 2.67942789661212, 2.72997428569956, 
2.73957234445009, 2.74429298312268, 2.75127910398334, 2.8234742291703, 
2.8481891169914, 2.86451108105839, 2.89042101880091, 2.92737036303902, 
2.9484129657786, 2.96142109406645, 2.99431715266964, 2.99956548822598, 
3.04060234011407, 3.04218159451577, 3.05230909964732, 3.1034616220947, 
3.10754912974469, 3.14921911265538, 3.16554107672237, 3.21669359916975, 
3.26245108973043, 3.34321159017975, 3.45024910831936, 3.5194341949137, 
3.56336240948661, 3.62634036737504, 3.64443858946784, 3.7394141026987, 
3.74138799247927, 3.75127910398334, 3.82059549654449, 3.8481891169914, 
3.86433305503339, 3.88756104093001, 3.92737036303902, 3.94556705344239, 
3.96123119704466, 3.994361151908, 3.99673051543515, 4.04040452891416, 
4.05238609538937, 4.10356428005096, 4.10734566547209, 4.16533337259698, 
4.21648259735246), y = c(1.65321251377534, 
2.35983548233989, 2.65321251377534, 3.65379118738781, 3.95486937106648, 
4.13097669160562, 4.2559234587329, 4.35283828998107, 4.43202274815908, 
4.49895806578639, 1.95424250943932, 2.43775056282039, 2.65224634100332, 
2.95424250943932, 3.95482118305179, 4.55692936239444, 2.13033376849501, 
2.50379068305718, 3.13033376849501, 4.13091244210747, 4.60806582276434, 
2.56110138364906, 2.9532763366673, 2.61172330800734, 2.64933485871214, 
3.6534054906645, 2.64836001098093, 3.12936759572299, 3.25430633233129, 
2.95036485437612, 3.35121634533934, 3.95443548632848, 1.54406804435028, 
3.35102285258412, 2.94939000664491, 3.43039759138697, 3.43007505555194, 
3.49734438101758, 1.49136169383427, 3.1264561134318, 4.13052674538416, 
3.55533632799527, 3.49692964807321, 3.12548126570059, 3.60648885044265, 
1.43136376415899, 3.2513948500401, 3.55485243437205, 3.65195606953307, 
4.25546548199246, 1.84509804001426, 3.60605874941031, 3.25042000230889, 
3.34830486304816, 4.35237549500052, 3.65137494391304, 1.79239168949825, 
3.34733001531695, 3.42748610909579, 4.43155674104815, 2.02118929906994, 
4.49850353067876, 3.4944328987264, 3.42651126136458, 1.73239375982297, 
4.55649547765645, 3.55242484570409, 3.95298606519706, 3.49345805099519, 
4.60764800010383, 2.14612803567824, 1.96848294855394, 3.60346915973384, 
3.55144999797288, 3.64884770837289, 3.95240493957702, 3.60260252042026, 
2.24303804868629, 1.90848501887865, 2.09342168516224, 3.64806712944893, 
4.12907732425274, 2.32221929473392, 2.19033169817029, 2.03342375548695, 
2.38916608436453, 4.12846389106476, 4.25401606086104, 2.44715803134222, 
2.26951294421792, 2.13033376849501, 2.4983105537896, 4.35090671553786, 
3.94987770403687, 2.33645973384853, 4.25338647298777, 2.53147891704225, 
2.20951501454263, 2.39445168082622, 3.94909712511292, 4.43007505555194, 
2.27646180417324, 4.35028679289621, 2.4456042032736, 2.49136169383427, 
4.49699879774009, 2.33445375115093, 4.12596896309256, 4.42946157675723, 
4.55497345833324, 2.38560627359831, 4.1251883841686, 2.42813479402879, 
4.60612329172563, 4.49638990246769, 4.25090769970086, 4.55438011841261, 
2.83250891270624, 4.60553129449641, 4.2501271207769, 4.34781771270891, 
2.79239168949825, 4.34703713378495, 4.42699895875654, 3.00860017176192, 
4.42621837983258, 4.49393182177355, 2.72916478969277, 4.55191332349795, 
4.49316516946319, 2.96848294855394, 3.13353890837022, 4.60304688910324, 
4.55115711644088, 4.60230963888826, 2.90525604874845, 3.23044892137827, 
3.09342168516224, 3.3096301674259, 3.19033169817029, 3.03019478535675, 
3.37657695705651, 3.26951294421792, 3.4345689040342, 3.12710479836481, 
3.33645973384853, 3.48572142648158, 3.20628604441243, 3.5321171162488, 
3.39445168082622, 3.4456042032736, 3.27323283404305, 3.49094120535679, 
3.33122478102073, 3.38237730346811, 3.42764837118693, 3.83314711191279, 
3.79197120102077, 4.00919585351952, 3.72875947516787, 3.96806246007645, 
4.13411332984232, 3.9048777669634, 4.2310105861795, 4.09300119668475, 
4.31018332757169, 4.18991120969281, 4.02983001931066, 4.37710581726905, 
4.26909245574043, 4.43507953840449, 4.12674814156019, 4.33599917760813, 
4.48621788466797, 4.20593479368468, 4.39396107133753, 4.44509021759835, 
4.27286227558891, 4.33083974120847, 4.38198100004346), z= c(0.999955575299867, 
0.995494691765546, 0.999955172027345, 0.999926033679331, 0.99991678686342, 
0.999904897765098, 0.999889045047711, 0.999866850117616, 0.999833555259654, 
0.999778073679538, 1, 0.995646349631614, 0.995651686652471, 1, 
0.999972262287807, 0.999778073679538, 1, 0.995641873770785, 1, 
0.999968299255032, 0.999778073679538, 0.995637388697481, 0.996218639615977, 
0.99563289438323, 0.98973439426202, 0.999531546635765, 0.996346520228623, 
0.995630590442202, 0.995700614858305, 0.991237206774749, 0.995421737403938, 
0.99944524575613, 0.890804087072412, 0.996706453948752, 0.997894212480754, 
0.9952313503305, 0.997214353163362, 0.995156171705362, 0.797734340293203, 
0.99412074211333, 0.999524488825487, 0.99510347821892, 0.997279820563602, 
0.998398645711802, 0.995074011754091, 0.808240781874722, 0.994498636216541, 
0.997274349944522, 0.997534455977712, 0.999556180190842, 0.911526659847912, 
0.997341917233643, 0.998520641671675, 0.994814416855481, 0.999866850117616, 
0.996400305727459, 0.87661760128074, 0.998528415594123, 0.994995278564684, 
0.999944518419885, 0.923766915954416, 0.999926024559846, 0.995013004366604, 
0.998512747875354, 0.892048739271579, 0.999889036839769, 0.995296444594529, 
0.998363474980584, 0.998449020496791, 1, 0.937391381845729, 0.88630698005698, 
0.995683663764723, 0.998456268995128, 0.996942725412362, 0.997753245312327, 
0.998463676933206, 0.932063520385405, 0.906784188034188, 0.902655853125975, 
0.999211025912868, 0.998319860516722, 0.935141772717124, 0.892675528593095, 
0.916625818813778, 0.933979974968711, 0.99797115232208, 0.998816480508913, 
0.936082151326681, 0.900781424425095, 0.909648496743688, 0.941749529696318, 
0.998801651058542, 0.998224786419616, 0.902713212944752, 0.998446630667949, 
0.948403003474168, 0.914512167894619, 0.905409638015124, 0.999195606346389, 
0.998779405237461, 0.915854639728232, 0.998402201411389, 0.906857475588999, 
0.919802756920318, 0.998890368397692, 0.918407982460065, 0.998636867966397, 
0.998446515756769, 0.999001331557923, 0.917002597867957, 0.999144079885877, 
0.926101087078337, 0.998890368397692, 0.998520491196923, 0.998964420445299, 
0.998335552596538, 0.965786613531383, 0.998890368397692, 0.999260300318071, 
0.998979184235054, 0.945068381487184, 0.999201100705694, 0.999223257878384, 
0.970512204607325, 0.9992787394585, 0.999482171918923, 0.949076170636718, 
0.999445184198846, 0.999556147359077, 0.955345091849107, 0.972770560769266, 
0.999556147359077, 0.999556147359077, 0.999556147359077, 0.957312469974607, 
0.976758309780197, 0.961975867967269, 0.978470254957507, 0.967741935483871, 
0.960958809116546, 0.980911021498962, 0.968909348441926, 0.98101693279946, 
0.964121370675761, 0.970412083323391, 0.982320091691662, 0.966808309726157, 
0.986710717719865, 0.971368614019007, 0.973053381130051, 0.969099716051445, 
0.980818067506595, 0.969583675044624, 0.971053722535177, 0.977810103799403, 
0.990208587595695, 0.984134028625319, 0.991377397368838, 0.983079995561966, 
0.986875891583452, 0.992861898069384, 0.985607861784752, 0.993919488704452, 
0.989163399659738, 0.994340878828229, 0.990546358350717, 0.988423699977809, 
0.995117620949845, 0.991455836662228, 0.995561473590768, 0.989525542585771, 
0.992454505104305, 0.995339547270306, 0.99106746560142, 0.992898357745229, 
0.991788726142921, 0.991862701583074, 0.992454505104305, 0.992676431424767
)), row.names = c(NA, -180L), class = c("tbl_df", "tbl", "data.frame"
))

When I try

ggplot(res,
       aes(x = x,
           y = y,
           fill = z)) +
  geom_raster(interpolate = TRUE)

I get

Warning messages:
1: Raster pixels are placed at uneven horizontal intervals and will be shifted. Consider using geom_tile() instead. 
2: Raster pixels are placed at uneven vertical intervals and will be shifted. Consider using geom_tile() instead.

I was not able to get geom_tile() to work either, nor geom_contour().

ggplot(res,
       aes(x = x,
           y = y,
           z = z)) +
  geom_contour()

tells me

Warning messages:
1: stat_contour(): Zero contours were generated 
2: In min(x) : no non-missing arguments to min; returning Inf
3: In max(x) : no non-missing arguments to max; returning -Inf

What am I doing wrong and what can I do to get results?

What I wanted to see was something like

enter image description here

or

enter image description here

The point is that I want to expect this - in nature - 3D dataset in such a way that I am able to draw conclusions from this. Floats are pretty difficult to dicern by color or size. So I wanted at least to see the pattern. That was the idea at least.

Upvotes: 1

Views: 943

Answers (1)

Allan Cameron
Allan Cameron

Reputation: 174293

The difficulty here is that you have a sparse collection of x and y values, each of which has an associated z value. I assume you want to treat these as point samples of a surface, and plot the inferred surface. We can only do that if we interpolate the z values at regularly spaced x, y grid points, and we can only do it sensibly within the convex hull of your points. I think you were trying to get geom_raster to do that using interpolate, but it is limited in its ability to do that with irregularly spaced points.

One solution is to get a 2D interpolation using interp from the akima package:

library(akima)

grid <- with(res, interp(x = x, y = y, z = z, linear = TRUE, extrap = TRUE,
                         xo = seq(min(x), max(x), length = 100), 
                         yo = seq(min(y), max(y), length = 100))) 

df <- as.data.frame(interp2xyz(grid))

Now df is a regular grid of interpolated points, so we can easily call geom_raster. For demonstration purposes, we will add the x, y points to show how sparse the data is from which this has been reconstructed:

ggplot(df, aes(x, y, fill = z)) + 
  geom_raster(interpolate = TRUE) +
  geom_point(data = res, alpha = 0.2) +
  scale_fill_gradientn(colours = c("red", "gold", "forestgreen"),
                       na.value = "#FFFFFF00")

enter image description here

Note that anything outside the convex hull (i.e. the overall outline of the points) is NA since we cannot guess what might be happening there from our sample.

For this reason a filled contour plot doesn't work too well:

df$z[is.na(df$z)] <- mean(df$z, na.rm = TRUE)

ggplot(df, aes(x, y, fill = z)) + 
  stat_contour_filled(aes(z = z, fill = ..level..))

enter image description here

Upvotes: 4

Related Questions