Home > Enterprise >  Does anyone know how i can edit ggMarginal to show density lines here
Does anyone know how i can edit ggMarginal to show density lines here

Time:03-28

I am trying to add density lines around my scatterplot. Does anyone know how to edit this code to make it work? something to do with the 'p' argument i think

 scatter4 = ggplot(mammal, aes(x = gestation, y = dream))  
   geom_point(colour = 'red')  
   ggMarginal(p, type = "densigram")  
   xlab("Gestation (in days)")  
   ylab("Dreaming sleep (in hrs/day)") 

CodePudding user response:

ggMarginal is a function that modifies an existing ggplot object. You cannot add it to an existing plot with the operator.

First create your plot with its labels, etc, and store it to a variable. Then pass this variable as the first argument to ggMarginal

library(tourr)
library(ggplot2)
library(ggExtra)

scatter4 = ggplot(mammal, aes(x = gestation, y = dream))  
   geom_point(colour = 'red')  
   xlab("Gestation (in days)")  
   ylab("Dreaming sleep (in hrs/day)") 
 
ggMarginal(scatter4, type = "densigram") 


Data

mammal <- openintro::mammals
names(mammal)[5] <- "dream"

Created on 2022-03-27 by the reprex package (v2.0.1)

  • Related