Home > Software design >  adding hulls to a correlation plot
adding hulls to a correlation plot

Time:11-16

I am having trouble adding hulls to my plot. I have added a photo for reference my existing script below.

enter image description here

The X and Y axes represent the PC1 scores for shape measurements of two different bones. I have successfully plotted this and gotten the r2 to measure correlation of shape between the bones across different genera of primates. Within this plot, each dot represents a different genus of primate. What I want to do now, is create hulls to represent specific groupings of these genera.

I have found a lot online about adding hulls to other plots (and have done this just fine) but am struggling to add it to this. My code is below (sans any attempts to add a hull).

Thank you!!

ggplot(myd_,aes(x = PC1_mean_calcaneus, y = PC1_mean_hamate, label=genus))  
  geom_point()   
  geom_text(hjust=0, vjust=0)  #add my labels per dot
  geom_smooth(method = "lm", se=FALSE)  
  stat_regline_equation(label.y = .6, aes(label = ..eq.label..))  
  stat_regline_equation(label.y = .5, aes(label = ..rr.label..))

CodePudding user response:

Filter your dataset to just points lying on the convex hull using chull(), and pass this filtered dataset to geom_polygon():

library(ggplot2)

ggplot(mpg, aes(cty, hwy))  
  geom_point()  
  geom_polygon(data = mpg[chull(mpg$cty, mpg$hwy), ], color = "red", fill = NA)

To plot separate hulls per group, filter to chull() rows by group:

library(dplyr)

mpg_hulls <- mpg %>%
  group_by(cyl) %>%
  group_modify(~ .x[chull(.x$cty, .x$hwy), ])

ggplot(mpg, aes(cty, hwy, color = factor(cyl), fill = factor(cyl)))  
  geom_point()  
  geom_polygon(data = mpg_hulls, alpha = .1)

CodePudding user response:

To create a convex hull around each grouping, you could create a small function to pull the vertices and feed those to a geom_polygon() call. Note that in the data argument, you can pass a function. Here we can define a custom function to pull the chull() for each group and then pass that as the data argument to geom_polygon.

library(tidyverse)

# function to pull chull by group
f <- function(d, grp = cyl, x = "disp", y = "mpg"){
  d %>% 
    group_by({{grp}}) %>% 
    group_split() %>% 
    map_dfr(~.[chull(.[[x]], .[[y]]),])
}

mtcars %>% 
  mutate(cyl = factor(cyl)) %>% 
  ggplot(aes(disp, mpg, color = cyl))  
  geom_point()  
  geom_polygon(data = f, fill = NA)

Created on 2022-11-15 with reprex v2.0.2

  • Related