vendredi 18 juin 2021

Adding "noise" to a graph

I am using the R programming language. I made the following graph using the "ggplot" library:

#load library
library(RSSL)
library(ggplot2)

#generate first data
d <- generateCrescentMoon(1000,2,1)
d$c = ifelse(d$Class == "+", "1","0")
d$Class = NULL

ggplot(d, aes(x=X1, y=X2, color=c, shape=c)) +  geom_point() 

enter image description here

Now, I am trying to add some "noise" to different regions of this graph. I did the following:

#noise the first region (x1: -5 to 0 AND x2: -10 to 10)

c <- sample(0:1, 1000, TRUE)

X1 <- runif(100, min=-5, max=0)
X2 <- runif(100, min=-10, max=10)

a = data.frame(X1,X2,c)
a$c = as.factor(a$c)

g = rbind(a,d)

This has added noise to the desired region: enter image description here

Now, I am trying to add "noise" to the corner regions

Region1: (x1: -10 to -5 AND x2: -5 to -10) Region2: (x1: 5 to 10 AND x2: 5 to 10)

I do this by re-writing the existing files and merging them all together:

#Add noise to Region2
c <- sample( 0:1, replace=TRUE, prob=c(0.5, 0.5) )
X1 <- runif(100, min=5, max=10)
X2 <- runif(100, min=5, max=10)
f = data.frame(c,X1,X2)
f$c = as.factor(f$c)

#Append
gg = rbind(g,f)

#Add noise to Region1
c <- sample( 0:1, replace=TRUE, prob=c(0.5, 0.5) )
X1 <- runif(100, min=-10, max=-5)
X2 <- runif(100, min=-10, max=-5)
f = data.frame(c,X1,X2)
f$c = as.factor(f$c)

#Append ("g" is the final file)
g= rbind(gg,f)

But when I try to plot this graph, the noise is not appearing in "Region 2"

#plot
ggplot(g, aes(x=X1, y=X2, color=c, shape=c)) +  geom_point() 

enter image description here

Does anyone know why this is happening? Is this because of a random number generating process? Or is there an error in my code?

Thanks




Aucun commentaire:

Enregistrer un commentaire