Search code examples
rplotscatter-plot

How to get cluster point in a scatter plot?


enter image description here

I have a scatter plot....How to plot centroid in it??

stat_summary(fun = mean)??? or??

I tried the code below but... i need a cluster point?

plot(Period_1_AB, Period_2_AB, pch = 0 , cex = .8)

points(Period_1_BA, Period_2_BA, pch = 2, cex = .8)

abline(0,1)

Solution

  • If I understand you correctly, you can do this by simply plotting the mean using points():

    Data

    set.seed(123)
    df <- data.frame(x = runif(100),
                     y = runif(100))
    

    Plot - here, I plotted the centroid of the points in red.

    plot(df, pch = 19)
    points(mean(df$x), mean(df$y), pch = 19, col = "red")
    

    enter image description here

    Edit: per your comment requesting ggplot2::geom_point, one way would be:

    ggplot(df, aes(x, y)) +
      geom_point() +
      geom_point(data = data.frame(lapply(df[], mean)),  
                 aes(x = x, y = y), col="red")
    

    enter image description here