0

I would like to visualize covariance in R. I found a promising example here:

https://evangelinereynolds.netlify.app/post/geometric-covariance/

Obviously it can be done in R and ggplot2.

enter image description here

I can create a scatterplot and add the two means of data.

x=c(4,13,19,25,29)
y=c(10,12,28,32,38)
data <- data.frame(x, y)
ggplot(aes(x=x, y=y), data=data) + 
  geom_point(size=3) +
  labs(title="Distribution of x and y around their means") +
  theme_bw() +
  geom_vline(xintercept=mean(x), size=1.5, color="red") + 
  geom_hline(yintercept=mean(y), size=1.5, color="red")

enter image description here

How do I add arrows from each data point to the respective means and color the squares?

Marco
  • 2,368
  • 6
  • 22
  • 48
  • arrows: `?geom_segment`, rectangles: `?geom_rect` – tjebo Apr 11 '21 at 18:05
  • and check this canonical thread for how to draw arrows https://stackoverflow.com/questions/38008863/how-to-draw-a-nice-arrow-in-ggplot2 – tjebo Apr 11 '21 at 18:07

1 Answers1

1

Maybe something like

library(ggplot2)
x=c(4,13,19,25,29,10,30)
y=c(10,12,28,32,38,35,11)
data <- data.frame(x, y)
simple.arrow <- arrow(length = unit(0.2, "cm"))
ggplot(aes(x=x, y=y), data=data) + 
  geom_vline(xintercept=mean(x), linetype="dotted") + 
  geom_hline(yintercept=mean(y), linetype="dotted") +
  geom_segment(aes(y = mean(y), xend = x, yend = y, colour=y>mean(y)), arrow = simple.arrow) +
  geom_segment(aes(x = mean(x), xend = x, yend = y, colour=x>mean(x)), arrow = simple.arrow) +
  geom_rect(aes(
    xmin = ifelse(x > mean(x), mean(x), x),
    xmax = ifelse(x > mean(x), x, mean(x)),
    ymin = ifelse(y > mean(y), mean(y), y),
    ymax = ifelse(y > mean(y), y, mean(y)),
    fill = (x-mean(x)) * (y-mean(y)) > 0
  ),
  alpha = 0.1) +
  geom_point() +
  theme_minimal()

example plot showing covariance

Rob Syme
  • 362
  • 2
  • 11