Search code examples
rggplot2plotdistribution

Three different sets of points on the same plot using ggplot


I have created three sets of points by multivariate normal distibution:

library('MASS')
library('ggplot2')
library('reshape2')
library("ClusterR")
library("cluster")
mu1<-c(1,1)
mu2<-c(1,-9)
mu3<-c(-7,-2)
mu1
mu2
mu3
sigma1<-matrix(c(1,1,1,2), nrow=2, ncol=2, byrow = TRUE)
sigma2<-matrix(c(1,-1,-1,2), nrow=2, ncol=2, byrow = TRUE)
sigma3<-matrix(c(2,0.5,0.5,0.3), nrow=2, ncol=2, byrow = TRUE)
sigma1
sigma2
sigma3
simulation1<-mvrnorm(100,mu1,sigma1)
simulation1
simulation2<-mvrnorm(100,mu2,sigma2)
simulation2
simulation3<-mvrnorm(100,mu3,sigma3)
simulation3
X<-rbind(simulation1,simulation2,simulation3)
colnames(X)<-c("x","y")
X<-data.frame(X)
X

I need to represent these sets on one plot with different colours. Here I will attach image how it should be look like: Graphic Can somebody help me, how I can do this? I knew that graphic on image is created by ggplot, but I don'tknow exactly how to use it to my sets.


Solution

  • Simply add some indicator to dataframe X will make that plot

    X$group <- rep(c("1","2","3"), each = 100)
    X %>%
      ggplot(aes(x,y, group = group, color = group)) +
      geom_point() + xlab("aaaa") + ylab("bbbb")
    

    enter image description here