Skip to content
Gota Morota edited this page Mar 5, 2017 · 3 revisions

Contour Plot

Say we wish to maximize (a+b)^4b^5exp(-(a+2*b)).

model = function(a,b){
          (a+b)^4*b^5*exp(-(a+2*b))
        }

X = seq(-2, 10,0.1)
Y = seq(1, 10,0.1)
Z = outer(X,Y ,model)
contour(X,Y,Z, xlab="mu", ylab="lambda")

Save multiple plot in one file

pdf(file="hoge.pdf", onefile=TRUE)
plot(y1~x1)
plot(y1~x1)
plot(y1~x1)
dev.off()

Plot by a group

x <- read.table(textConnection("Group Ind Age Trait
 1 1 2 21
 1 2 1 22
 1 2 2 21
 1 3 1 24
 1 3 2 45
 1 4 1 23
 1 4 2 26
 2 1 1 45
 2 1 2 12
 2 2 1 25
 2 2 2 26
 2 3 1 45
 2 3 2 43
 2 4 1 23
 2 4 2 47
 "), header=T)
closeAllConnections()
 
# base graphics
par(mfrow = c(1,2))
plot(Trait ~ Age, data = x, subset = {Group == 1})
plot(Trait ~ Age, data = x, subset = {Group == 2})

# lattice
require(lattice)
xyplot(Trait ~ Age | Group, data = x)

# ggplot2
require(ggplot2)
g <- ggplot(x, aes(x = Age, y = Trait))
g + geom_point(size = 2.5) + facet_wrap(~ Group) + theme_bw()

Distribution

Beta distribution with a = 1, b = 0.67.

curve(dbeta(x,1,0.67), 0, 1, yaxt="n", xlab="theta", ylab="")

Individual growth curve with mean for each day

# lattice
require(lattice)
xyplot(Reaction~Days, data=sleepstudy, group=Subject, type='l', 
panel= function(...) {
         panel.xyplot(...)
         panel.average(...,fun=mean, horizontal=FALSE, col='red', lwd=3)
      }
)

# ggplot2
require(ggplot2)
g <- ggplot(sleepstudy, aes(x = Days, y = Reaction, group = Subject, colour + = Subject))
g + geom_line(size = 1) + geom_smooth(aes(group = 1), size = 2) + theme_bw()
 
# get rid of the legend
g + geom_line(size = 1) +geom_smooth(aes(group = 1), size = 2) + theme_bw() +opts(legend.position = 'none')

New device

data(mtcars)
with(mtcars, plot(mpg,disp))
dev.new()
with(mtcars, plot(mpg,wt))

Plot the area under a standard normal distributions

x <- seq(-4, 4, 0.1)
y <- dnorm(x)
plot(x, dnorm(x), type="l", col="black", lwd=3)
abline(v=-1, lwd=3, col="blue")
abline(h=0, lwd=3, col="black")
polygon(c(x[1:31],rev(x[1:31])), c(rep(0,31),rev(y[1:31])), col="lightblue")

Clone this wiki locally