EM823823
EM823823

Reputation: 183

Scatter plot with normal marginals in R

How can you add normal marginal distributions to a plot in R?

Here is an example code and I would like to have normal marginals added to the sides (top and right side):

x<- rweibull(100, 2.6, 3)
y<- rweibull(100, 1.8, 3)
xy.df<- data.frame(cbind(x,y))
p1<- ggplot(xy.df, aes(x,y)) +
  geom_point(colour = "blue", size = 0.25) +
  geom_density2d() +
  theme_classic() +
    border()
ggMarginal(p1, type="density")

Upvotes: 1

Views: 305

Answers (2)

Justin Landis
Justin Landis

Reputation: 2071

This may be a bit late, but you can accomplish what you want from the ggside package. You will need to use the dev version from the github to use the the full functionality though.

Currently, the CRAN version of ggside does not have (x|y)side variants to stat_function(), but the current dev branch has a version working.

Here we plot the normal density estimates filled in with blue, and then the theoretical distribution is colored red.

library(ggplot2)
library(ggside)
#> Registered S3 method overwritten by 'ggside':
#>   method from   
#>   +.gg   ggplot2
x<- rweibull(100, 2.6, 3)
y<- rweibull(100, 1.8, 3)
xy.df<- data.frame(cbind(x,y))
p <- ggplot(xy.df, aes(x, y)) +
  geom_point(colour = "blue", size = 0.25) +
  geom_density2d() +
  geom_xsidedensity(fill = "blue", alpha = .3) +
  geom_ysidedensity(fill = "blue", alpha = .3) +
  stat_xsidefunction(fun = dweibull, args = list(shape = 1.8, scale = 3), colour = "red") +
  stat_ysidefunction(fun = dweibull, args = list(shape = 2.6, scale = 3), colour = "red") +
  theme_classic() 
p

Created on 2022-02-01 by the reprex package (v2.0.1)

Upvotes: 1

St&#233;phane Laurent
St&#233;phane Laurent

Reputation: 84529

If you mean a theoretical normal density, you can do that with the cowplot package:

library(ggplot2)
library(cowplot)

x <- 1:30
y <- x + rnorm(30, 0, 1)
dat <- data.frame(x = x, y = y)

gg <- ggplot(dat) + geom_point(aes(x = x, y = y))

y_density <- axis_canvas(gg, axis = "y", coord_flip = TRUE) +
  geom_function(fun = dnorm, args = list(mean = 15, sd = 5)) +
  coord_flip()

# create the combined plot
combined_plot <- insert_yaxis_grob(gg, y_density, position = "right")

# show the result
ggdraw(combined_plot)

enter image description here

Upvotes: 1

Related Questions