geom_mark_hull - how to finetune area/border of hull?

I was wondering whether someone could help me with this ggforce problem (using for the first time. I am trying to annotate only those points which are above the line. Using geom_mark_hull seems to be what I need. I think my filter argument (y>x) is correct, but still the hull includes also points which fall outside of it. I played around with the expand argument, but couldn't really figure it out. Any idea?

Apologies also for the clumsy copy-pasting of the data.

library(tidyverse)
library(ggforce)


df <- structure(list(x = c(0.41692437447287, 0.342333654773385, 
                                  0.370405589779634, 0.435775451950523, 0.407407407407407, 0.355710470327283, 
                                  0.383207030503725, 0.418327059039574, 0.360569337674981, 0.32043952954407, 
                                  0.308606894488896, 0.446909838727135, 0.363319974688884, 0.356385606262112, 
                                  0.380916853187433, 0.33063131794774, 0.36220161767607, 0.376345321536635, 
                                  0.358463211308445, 0.390471120574927, 0.314081996434938, 0.471514745308311, 
                                  0.312457239092679, 0.451902135033305, 0.353963936173196, 0.368858987256875, 
                                  0.408483163187567, 0.480649124603256, 0.515149349721309, 0.564700396362789, 
                                  0.427544094806493, 0.501299336574032, 0.430444068046248, 0.459108168630124, 
                                  0.489339573582943, 0.367744547340722, 0.409559554034662, 0.419176742915475, 
                                  0.515707593794319, 0.423066948242007, 0.535890487109999, 0.422405921084251, 
                                  0.58848201322794, 0.250291339338351, 0.2316047867444, 0.265736461931895, 
                                  0.445302672522234, 0.37167006703585, 0.438667839085513, 0.35178173250873, 
                                  0.425835370823146, 0.401748649939385, 0.309168061200096, 0.401872637727637, 
                                  0.430761406567858, 0.504295990408952, 0.46157663405643, 0.392783637766915, 
                                  0.407650886246354, 0.388889768893254, 0.367553865652725, 0.36798114016288, 
                                  0.483757558711855, 0.490627223468885, 0.520520440125291, 0.531407942238267, 
                                  0.522637869259101, 0.288294092741564, 0.421571339650045, 0.390189505180916, 
                                  0.4514329580348, 0.335011955655387, 0.423964678924719, 0.490160349854227, 
                                  0.365154759587308, 0.4535632973925, 0.379573398749514, 0.342045126794311, 
                                  0.509712398850411, 0.518218901824182, 0.297610474631751, 0.518294339036977, 
                                  0.434747603833866, 0.566869075829384, 0.493356120487075, 0.581443914081146, 
                                  0.574228737003579, 0.546181875792142, 0.506317482196187, 0.383193208717656, 
                                  0.414824115673618, 0.335287603413458, 0.350651594431829, 0.345256916996047, 
                                  0.189816360601002, 0.228091501823406, 0.222791555646649, 0.182215164747916, 
                                  0.194491567707814, 0.173071421760275, 0.220474406991261, 0.211263247466543, 
                                  0.22138699848565, 0.222119419753494, 0.228419209418427, 0.337982675084422, 
                                  0.261495363214838, 0.176961602671119, 0.212157330154946, 0.234004775607665, 
                                  0.262843982520373, 0.322620032297452, 0.200698549167114, 0.259853648936361, 
                                  0.25830161438509, 0.281890841541808), y = c(0.406272793581328, 
                                                                                         0.31651376146789, 0.37420777073574, 0.394572025052192, 0.394598155467721, 
                                                                                         0.349556167737986, 0.353350739773716, 0.393244873341375, 0.334345794392523, 
                                                                                         0.308378541289934, 0.294218942189422, 0.364122508507535, 0.337986533092462, 
                                                                                         0.338880798835032, 0.355615662029881, 0.312128922815946, 0.35382012968706, 
                                                                                         0.350076103500761, 0.34370477568741, 0.38141592920354, 0.29216799091941, 
                                                                                         0.371182458888019, 0.345722524831785, 0.403042557288658, 0.354819172867278, 
                                                                                         0.361579802524684, 0.384479047980757, 0.410176779741997, 0.461839933993399, 
                                                                                         0.487903225806452, 0.402213213542237, 0.42563056379822, 0.391850723533892, 
                                                                                         0.391261538461538, 0.430812490750333, 0.363496143958869, 0.390338552120969, 
                                                                                         0.363570333306432, 0.432318992654774, 0.384460462158151, 0.482098546614676, 
                                                                                         0.394859813084112, 0.50309635868219, 0.248785520828855, 0.261967213114754, 
                                                                                         0.306892067620286, 0.39344262295082, 0.340084643288996, 0.360054163845633, 
                                                                                         0.340183396102833, 0.384397528321318, 0.334538878842676, 0.31954444298992, 
                                                                                         0.357007319056655, 0.401106136423492, 0.410909090909091, 0.429143956396281, 
                                                                                         0.357388771186441, 0.325703346096221, 0.363880895783811, 0.342269555637165, 
                                                                                         0.360756960722657, 0.406763650103911, 0.433143442360622, 0.46252118367879, 
                                                                                         0.484194867980662, 0.465246899134098, 0.272034094862577, 0.391666666666667, 
                                                                                         0.358268138463187, 0.434737087017217, 0.310689170182841, 0.380021030494217, 
                                                                                         0.472278738555443, 0.360333531864205, 0.382991133924405, 0.352498843128181, 
                                                                                         0.324945970978697, 0.43421686746988, 0.479700115340254, 0.257407159136613, 
                                                                                         0.435536515707874, 0.373120753303729, 0.49480415667466, 0.43688227198799, 
                                                                                         0.484883057615516, 0.425960637300843, 0.49983283182882, 0.445731214651225, 
                                                                                         0.355819125277984, 0.359888720283257, 0.317800914169629, 0.30501379507183, 
                                                                                         0.393353941267388, 0.199254805656703, 0.254204780171142, 0.258497472546627, 
                                                                                         0.201825408508759, 0.20587084148728, 0.191910192444761, 0.246504624650462, 
                                                                                         0.235352532274081, 0.259445350734095, 0.252319981205216, 0.256135462437038, 
                                                                                         0.34679179397643, 0.265089722675367, 0.190706679574056, 0.227638473053892, 
                                                                                         0.250652382914435, 0.273615635179153, 0.346929163526752, 0.220352353405206, 
                                                                                         0.275297996449404, 0.274067863483922, 0.294108680740797)), class = c("tbl_df", 
                                                                                                                                                              "tbl", "data.frame"), row.names = c(NA, -116L))
df %>% 
  mutate(group_id=case_when(x>0.5 ~"a",
                            TRUE ~ "b")) %>% 
  ggplot()+
  geom_point(aes(x, y, color=group_id))+
  geom_abline(intercept = 0, slope=1)+
  geom_mark_hull(aes(x, y, filter=y>x))

Created on 2019-10-04 by the reprex package (v0.3.0)

I got pretty close with this, basically just messing with expand and radius to get the hull as tight to the points as possible. I guess expand does what it implies in some way (although I'm still not exactly sure) while making the radius smaller forces the angles to be tighter and helps bring the hull in closer.

df %>% 
  mutate(group_id=case_when(x>0.5 ~"a",
                            TRUE ~ "b")) %>% 
  ggplot()+
  geom_point(aes(x, y, color=group_id))+
  geom_abline(intercept = 0, slope=1)+
  geom_mark_hull(aes(x, y, filter=y>x), expand = unit(1.1, "mm"), radius = unit(0.1,
  "mm"))

I guess the tough thing is that there are some points really close to the x=y line on both sides of it, so it's always going to look a little messy. Hope this helps somewhat.

1 Like

excellent. many thanks! I am still a bit surprised that the hull can 'accidentally' include points which are outside of the filter.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.