Showing posts with label boxplot. Show all posts
Showing posts with label boxplot. Show all posts

Thursday, April 18, 2013

RG#80: Plotting boxplot and histogram (overlayed or in margin)

 # data
set.seed(4566)
data <- rnorm(100)


# layout where the boxplot is at top 
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(1,3))
par(mar=c(3.1, 3.1, 1.1, 2.1))
boxplot(data, horizontal=TRUE,  outline=TRUE,ylim=c(-4,4), frame=F, col = "green1")
hist(data,xlim=c(-4,4), col = "pink")


# layout boxplot is at the bottom 
nf <- layout(mat = matrix(c(1,2),2,1, byrow=TRUE),  height = c(3,1))
par(mar=c(3.1, 3.1, 1.1, 2.1))
hist(data,xlim=c(-4,4), col = "pink")
boxplot(data, horizontal=TRUE,  outline=TRUE,ylim=c(-4,4), frame=F, col = "green1", width = 10)


# Added to the  plot:
par(mar=c(3.1, 3.1, 1.1, 2.1))
hist(data,xlim=c(-4,4), col = "pink")
boxplot(data, horizontal=TRUE,  outline=TRUE,  ylim=c(-4,4), frame=F, col = "green1", add = TRUE)





Friday, April 12, 2013

RG#54: Notched boxplot


# data 
set.seed(13)
x <- c(rnorm(1000, 50, 20), rnorm(500, 70, 10), rnorm(500, 30, 10), runif(1000, 0, 100))
g <- rep (c("normal", "binormal", "uniform"), each = 1000)

#plot 
boxplot(x, col = c("bisque"), notch = TRUE)

boxplot(x ~ g, col = c("bisque", "cyan4", "yellow"), notch = TRUE)






RG#53: Trellis plot in Box-Percentile Panel


set.seed(13)
x <- c(rnorm(1000, 50, 20), rnorm(500, 70, 10), rnorm(500, 30, 10), runif(1000, 0, 100))
g <- rep (c("normal", "binormal", "uniform"), each = 1000)
# default trellis box plot
require(lattice)
#regular box plot  
bwplot(g ~ x, col = "red", fill = c("cyan4", "tan", "blue"))
# box-percentile plot with data density (rug plot) bwplot(g ~ x, panel=panel.bpplot, probs=seq(.01,.49,by=.01), datadensity=TRUE,
col = "red" )


# continuous plot up until quartiles ("Tootsie Roll plot")
bwplot(g ~ x, panel=panel.bpplot, probs=seq(.01,.25,by=.01))

# start at quartiles then make it continuous ("coffin plot")
bwplot(g ~ x, panel=panel.bpplot, probs=seq(.25,.49,by=.01))

# same as previous but add a spike to give 0.95 interval
bwplot(g ~ x, panel=panel.bpplot, probs=c(.025,seq(.25,.49,by=.01)))

# decile plot with reference lines at outer quintiles and median
bwplot(g ~ x, panel=panel.bpplot, probs=c(.1,.2,.3,.4), qref=c(.5,.2,.8))



# prototype of bplot 
bpplt()


LG#52: Box-percentile plots

# data
set.seed(1)
normal <- rnorm(10000, 7.5,3)
binormal <- c(rnorm(5000, 3, 2.5), rnorm(5000, 5, 2.5))
uniform <- runif (10000, -5, 17)

require(Hmisc)
bpplot(normal, binormal, uniform)


Wednesday, April 10, 2013

RG#50: beeswarm box violin plot


# data
set.seed(1234)
bimodal <- c(rnorm(250, -2, 0.6), rnorm(250, 2, 0.6))
uniform <- runif(500, -4, 4)
normal <- rnorm(500, 0, 1.5)
dataf <- data.frame (group = rep(c("bimodal","uniform", "normal"), each = 500), xv = c(bimodal, uniform, normal), cg = rep( c("A","B"), 750))




require(beeswarm)
beeswarm(xv ~ group, data = dataf,method = 'swarm',
pch = 16, pwcol = as.numeric(cg),
xlab = '', ylab = 'xv', labels = c('bimodal', 'uniform', 'normal'))

 

# center 
beeswarm(xv ~ group, data = dataf,
        method = 'center',
        pch = 16, pwcol = as.numeric(cg),
        xlab = '', ylab = 'xv',
        labels = c('bimodal''uniform', 'normal'))
# hexagon 
beeswarm(xv ~ group, data = dataf,
        method = 'hex',
        pch = 16, pwcol = as.numeric(cg),
        xlab = '', ylab = 'xv',
        labels = c('bimodal''uniform', 'normal'))
# square
dataf$col <- as.numeric(dataf$cg)
dataf$col[dataf$col ==1] = "green4"
dataf$col[dataf$col ==2] = "purple"
beeswarm(xv ~ group, data = dataf,
       method = 'square',
        pch = 16, pwcol = col,
        xlab = '', ylab = 'xv',
        labels = c('bimodal''uniform', 'normal'))





Tuesday, April 9, 2013

RG#48: beanplot


# data 
set.seed(1234)
 bimodal <- c(rnorm(250, -2, 0.6), rnorm(250, 2, 0.6))
uniform <- runif(500, -4, 4)
 normal <- rnorm(500, 0, 1.5)
 ylim <- c(-7, 7)



# plot 
 library("beanplot")
beanplot(bimodal, uniform, normal, ylim = ylim, main = "beanplot",col = c("cyan3", "purple", "purple"), border = "cyan3")
 # col - these colors stand for the area of the beans (without the border, use border for that color), the lines # inside the bean, the lines outside the bean, and the average line per bean



nn

Saturday, April 6, 2013

RG#18: Violin Box plot

#Violin box plot
#data
 set.seed (45)
xv <- rnorm(500, 50,15)
gv <- c(rep("A",100), rep("B",100), rep("C",100), rep("D",100), rep("E", 100))
dataf <- data.frame(xv, gv)


#plot
require(lattice)

library("latticeExtra")
bwplot(xv, panel = panel.violin, box.ratio = 3, xlab="y(units)")


 bwplot(xv~gv, data=dataf, panel = panel.violin, box.ratio = 3, xlab="y(units)", col = "green4")



# using vioplot 
require(vioplot)
vioplot(xv[1:100], xv[101:200], xv[201:300], xv[301:400], xv[401:500],col="cyan2", border="cyan2", lty=1, lwd=1, rectCol="red",
colMed="white", pchMed=19, add=FALSE, wex=1,drawRect=TRUE  )


vioplot(xv[1:100], xv[101:200], xv[201:300], xv[301:400], xv[401:500],col="white", border="red", lty=1, lwd=1, rectCol="red",
colMed="yellow", pchMed=19, add=TRUE, wex=1,drawRect=TRUE  )

 set.seed (45)
xv <- rnorm(500, 50,15)

pch1 <- rep(1:5, each = 100)


plot(1, type="n", axes=F, xlab="", ylab="")


vioplot(xv[1:100], xv[101:200], xv[201:300], xv[301:400], xv[401:500],col="white", border="red", lty=1, lwd=1, rectCol="red",
colMed="yellow", pchMed=19, add=FALSE, wex=1,drawRect=TRUE )

points(pch1, xv, pch = 19, col = "blue") 







RG #19: Box plot (Box and whisker plot) - single or multiple group


#data
set.seed (12345)
var <- rnorm(1000, 50,10)
group <- c(rep("A",100), rep("B",100), rep("C",100), rep("D",100), rep("E", 100))
dataf <- data.frame(var, group)
#plot
require(lattice)
# single 
 bwplot(var, xlab="y(units)",fill = "green4")

# grouped 
 bwplot(group ~var, data=dataf, xlab="y(units)", col = "blue", fill = heat.colors(5))

#fill user defined colors
 mycol <- c("midnightblue", "darkseagreen", "yellow", "green3", "tan")
 bwplot(group~var, data=dataf, xlab="y(units)", fill= mycol)



# using ggplot2:
 require(ggplot2)
plot2 <- ggplot (dataf, aes (y = var, x= group, fill = group ))
plot2 + geom_boxplot()+theme_bw( )



Friday, April 5, 2013

RG#6: XY Scatter plot with box plot at margin

 # data
set.seed(123)
myd <- data.frame (X = rnorm (100, 10,4), Y = rnorm(100, 8,3))


# plotting

# plotting scatter plot,  ?par see fig, par will position your scatter plot
# parameters are set so that we can provide space for boxplot in margin
 par(fig=c(0,0.8,0,0.8), new=TRUE)
plot(myd$X, myd$Y, xlab="X", ylab="Y")


# now plotting first boxplot, in margin, you might need to change
# the numbers in parameters where you  to put the boxplots.

  par(fig=c(0,0.8,0.55,1), new=TRUE)
 boxplot(myd$X, horizontal=TRUE, col = "red", axes=FALSE)


# now plotting second boxplot, in margin
par(fig=c(0.65,1,0,0.8),new=TRUE)
boxplot(myd$Y, col = "blue", axes=FALSE)

# just giving title to the plot
mtext("Scatterplot with boxplot at margin", side=3, outer=TRUE, line=-3)