четвер, 31 жовтня 2013 р.

Basic visualisation in R


# plot two histograms together in R
p1 <- font=""> hist(rnorm(500,4))                     # centered at 4
p2 <- font=""> hist(rnorm(500,4))                     # centered at 4
plot( p1, col=rgb(0,0,1,1/4), xlim=c(0,10))  # first histogram
plot( p2, col=rgb(1,1,0,1/2), xlim=c(0,10), add=T)  # second





# clustering sample w/ k-means
x <- c(rnorm(10,10,3), rnorm(10,50,13), rnorm(10,100,4), rnorm(5,20,2))
k <- font="">kmeans(x, 4)
plot(x, col=k$cluster)



set.seed(100) # init the random generator in R
# make some fun w/ linear regression
# prepare dataset
x <- color="#000000" font="" rnorm="" style="font-weight: bold;">(100, mean=100, sd=50)
y <- color="#0000cf" font="" style="font-weight: bold;">2+3.2*x
z <- color="#0000cf" font="">rnorm(100)

# x ~ y
plot(y~x, main="Sample of lineral regression",
      xlab="X values",
      ylab="Y values",
      col=rgb(0.8,0.1,0)
)


# log(y) ~ log(x)
plot(y~x, main="Sample of log(y) ~ log(x) regression",
                   xlab="X values",
                   ylab="Y values", log="xy",  col=rgb(0.8,0.1,0))


# y ~ log(x)
plot(y~x, main="Sample of y ~ log(x) regression",
                   xlab="X values",
                   ylab="Y values", log="x",  col=rgb(0.8,0.1,0))


# log(y) ~ x, and then let's add a line to a chart
plot(y~x, main="Sample of log(y) ~ x regression",
                   xlab="X values",
                   ylab="Y values", log="y",  col=rgb(0.8,0.1,0))
smoothingSpline = smooth.spline(x, y)
lines(smoothingSpline)


# small exercise w/ lattice library code
library(lattice)
xyplot(z~y|ifelse(x<100,qnorm(1),dnorm(4)))


# and even more, 2 charts on the panel 
xyplot(z~y+x)


Немає коментарів:

Дописати коментар