Showing posts with label combination plot. Show all posts
Showing posts with label combination plot. Show all posts

Friday, May 3, 2013

RG#109:small plot(s) with in a big plot

require(ggplot2)
library(gridBase)

plot(cos, -pi, 2*pi, ylim = c(-1.3, 1.5), col = "red")
myd <- data.frame (X = 1:10, Y = c(3, 4, 8, 7, 2, 1, 9, 4, 2, 3))
qp <- qplot(X, Y, data=myd) + theme_bw()

print(qp, vp=viewport(.65, .65, .25, .25))

 library(lattice)
library(gridBase)

plot.new()
pushViewport(viewport())
set.seed(1234)
xvars <- rnorm(25, 5, 1)
yvars <- rnorm(25, 5, 1)
xyplot(yvars~xvars,  xlim = c(0, 10), ylim = c(0, 10) )
pushViewport(viewport(x=.6,y=.85,width=.20,height=.15,just=c("left","top")))
grid.rect()
par(plt = gridPLT(), new=TRUE)
plot(xvars,yvars)
popViewport(2)






 

Thursday, May 2, 2013

RG#103: Combing different types of plot in trellis type


require(lattice)
require(latticeExtra)

#xy plot, conditioned from quakes data, with histogram 
qk <- xyplot(lat ~ long | equal.count(depth, 2), quakes,
    aspect = "iso", pch = "+", cex = 1.5, xlab = NULL, ylab = NULL)
qk <- c(qk, depth = histogram(quakes$depth), layout = c(3, 1))
update(qk, scales = list(at = list(NA, NA, NA, NA),
                          y = list(draw = FALSE)))



# suppress scales on the first 2 panels
update(qk, scales = list(at = list(NULL, NULL, NA), y = list(draw = FALSE)))




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)





Wednesday, April 17, 2013

RG#77: Histogram and Cumulative Histogram with overlayed density plot


## Make some sample data
x <- sample(0:30, 200, replace=T, prob=15 - abs(15 - 0:30))

## Calculate and plot the two histograms
hcum <- h <- hist(x, plot=FALSE)

hcum$counts <- cumsum(hcum$counts)
plot(hcum, main="")
plot(h, add=T, col="grey")

## Plot the density and cumulative density
d <- density(x)
lines(x = d$x, y = d$y * length(x) * diff(h$breaks)[1], lwd = 2)
lines(x = d$x, y = cumsum(d$y)/max(cumsum(d$y)) * length(x), lwd = 2)



RG#72: XY plot with heatmap strip at margin


set.seed(1234)
mydata <- data.frame (Xv = rnorm (300, 50, 10), Yv = rnorm (300, 10, 3))
mydata$xcat <- cut (mydata$Xv, 10)
mydata$ycat <- cut (mydata$Yv, 10)



# plot 

require(ggplot2)
require(grid)

#Scatter plot without axis titles
p<-ggplot(mydata, aes(x=Xv, y=Yv)) +    geom_point(shape=19)  +
  theme_bw() 

#tile plot for the x axis
px<-ggplot(mydata,aes(x=xcat,y=1,fill=xcat))+geom_tile()+
  scale_x_discrete(expand=c(0,0))+
  scale_fill_hue(h=c(0,120))+
  scale_y_continuous(expand=c(0,0),breaks=1,labels="10")+
  theme(legend.position="none",
        axis.title=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_text(color="white"),
        axis.ticks.y=element_line(color="white"))

#tile plot for the y axis
py<-ggplot(mydata,aes(x=1,y=ycat,fill=ycat))+geom_tile()+
  scale_y_discrete(expand=c(0,0))+
  scale_x_continuous(expand=c(0,0),breaks=1,labels="1")+
  scale_fill_hue(h=c(181,360))+
  theme(legend.position="none",
        axis.title=element_blank(),
        axis.text.y=element_blank(),
        axis.ticks.y=element_blank(),
        axis.text.x=element_text(color="white"),
        axis.ticks.x=element_line(color="white"))

#Define layout for the plots (2 rows, 2 columns)
layt<-grid.layout(nrow=2,ncol=2,heights=c(7/8,1/8),widths=c(1/8,7/8),default.units=c('null','null'))
#View the layout of plots
grid.show.layout(layt)

#Draw plots one by one in their positions
grid.newpage()
pushViewport(viewport(layout=layt))
print(py,vp=viewport(layout.pos.row=1,layout.pos.col=1))
print(p,vp=viewport(layout.pos.row=1,layout.pos.col=2))
print(px,vp=viewport(layout.pos.row=2,layout.pos.col=2))




RG#73: triple (three) heatmap plot - one center (XY) and other two at X and Y axis direction


 library(ggplot2);
 library(reshape2)
 library (grid)

#X axis quantitaive ggplot data
datfx <- data.frame(indv=factor(paste("ID", 1:20, sep = ""),
     levels =rev(paste("ID", 1:20, sep = ""))), matrix(sample(LETTERS[1:7],80, T), ncol = 4))
 # converting data to long form for ggplot2 use
  datf1x <- melt(datfx, id.var = 'indv')
plotx <-  ggplot(datf1x, aes(indv, variable)) + geom_tile(aes(fill = value),
 colour = "white")  +   scale_fill_manual(values= terrain.colors(7))+ scale_x_discrete(expand=c(0,0))
px <- plotx

#Y axis quantitaive ggplot data
datfy <- data.frame(indv=factor(paste("ID", 21:40, sep = ""),
     levels =rev(paste("ID",21:40, sep = ""))), matrix(sample(LETTERS[7:10],100, T), ncol = 5))
 # converting data to long form for ggplot2 use
  datf1y <- melt(datfy, id.var = 'indv')
ploty <-  ggplot(datf1y, aes( variable, indv)) + geom_tile(aes(fill = value),
 colour = "white")  +   scale_fill_manual(values= c("cyan4", "midnightblue", "green2", "lightgreen")) + scale_x_discrete(expand=c(0,0))
py <- ploty  +  theme(legend.position="left",  axis.title=element_blank())
 )

# plot XY quantative fill
datfxy <- data.frame(indv=factor(paste("ID", 1:20, sep = ""),
     levels =rev(paste("ID", 1:20, sep = ""))), matrix(rnorm (400, 50, 10), ncol = 20))
names (datfxy) <- c("indv",paste("ID", 21:40, sep = ""))
 datfxy <- melt(datfxy, id.var = 'indv')
  levels (datfxy$ variable) <- rev(paste("ID", 21:40, sep = ""))
pxy <- plotxy <-  ggplot(datfxy, aes(indv, variable)) + geom_tile(aes(fill = value),
 colour = "white")  + scale_fill_gradient(low="red", high="yellow") + theme(
        axis.title=element_blank())



#Define layout for the plots (2 rows, 2 columns)
layt<-grid.layout(nrow=2,ncol=2,heights=c(6/8,2/8),widths=c(2/8,6/8),default.units=c('null','null'))
#View the layout of plots
grid.show.layout(layt)

#Draw plots one by one in their positions
grid.newpage()
pushViewport(viewport(layout=layt))
print(py,vp=viewport(layout.pos.row=1,layout.pos.col=1))
print(pxy,vp=viewport(layout.pos.row=1,layout.pos.col=2))
print(px,vp=viewport(layout.pos.row=2,layout.pos.col=2))






RG#71: Barplot (histogram) with heatmap strip at margin


require(grid)
require(ggplot2)

plt1<-ggplot(myd, aes(x=nam, y=Yv, fill = nam)) +   geom_bar(stat = "identity")  +
 theme(axis.title=element_blank()) + scale_fill_manual(values= c("green1", "green3", "green4", "blue1",
  "blue3", "purple", "tan", "gray50")) +  theme_bw()



 #tile plot for the x axis
px<-ggplot(myd,aes(x=nam,y=1,fill=Zv))+geom_bar(stat = "identity", width=1, col = "yellow") +
 scale_fill_gradient(low = "green4", high = "red") + scale_x_discrete(expand=c(0,0)) +  theme(
        axis.title=element_blank(),
        axis.text.x=element_blank(),
        axis.ticks.x=element_blank(),
        axis.text.y=element_text(color="white"),
        axis.ticks.y=element_line(color="white"))

#Define layout (2 rows, 1 columns)
lyt<-grid.layout(nrow=2,ncol=1,heights=c(7/8,1/8),widths=c(8),default.units=c('null','null'))

#View the layout of plots
#grid.show.layout(lyt)

#plots
grid.newpage()
pushViewport(viewport(layout=lyt))
print(plt1,vp=viewport(layout.pos.row=1,layout.pos.col=1))
print(px,vp=viewport(layout.pos.row=2,layout.pos.col=1))




Monday, April 8, 2013

RG#37: XY line or scatter plot graph with two Y axis

# data
set.seed(1234)
x <- 1:20
y1 <- rnorm(20, 5, 1)
y2 <- rnorm(20,20, 2)

y3 <- rnorm(20,5, 3)



# plot
par(mar=c(5,4,4,5)+.1)
plot(x,y1,type="l",col="red", ylab = "Y1/Y3")

points (x,y3, col = "green4", pch = 19)

par(new=TRUE)
plot(x, y2, type="l",col="blue",xaxt="n",yaxt="n",xlab="",ylab="")

axis(4)
mtext("y2",side=4,line=3)

 

Saturday, April 6, 2013

Plot#17: heatmap plot with dendograms at margin


# data 
set.seed(1234)
 P <- vector()
DF <- as.data.frame(matrix(rep(NA, 100), nrow=10))
names(DF) <- c(paste("M",1:10, sep=""))
for(i in 1:10) {
                    DF[,i] <- rnorm(10, 10, 3)
                    }
rownames (DF) <- paste("O", 1:10, sep = "")
DFmat <- as.matrix (t(DF))


# plot, installation of the following package needed 
require(graphics); 
require(grDevices)

 rcol <- rainbow(nrow(DFmat), start=0, end=.3)
 ccol <- rainbow(ncol(DFmat), start=0, end=.3)
# using heat.colors (10)
heatv <- heatmap(DFmat, col = heat.colors(10), scale="column", RowSideColors = rcol, ColSideColors = ccol, margins=c(5,10), xlab = "Observations", ylab= "Variables", main = " Heat map plot of DF data")




# using cm.colors (10)
cmtv <- heatmap(DFmat, col = cm.colors(10), scale="column", RowSideColors = rcol, ColSideColors = ccol, margins=c(5,10), xlab = "Observations", ylab= "Variables", main = " Heat map plot of DF data")

# defining won color, using color brewer  
require(RColorBrewer)
plotclr <- brewer.pal(6,"Greens")

greencol <- heatmap(DFmat, col = plotclr, scale="column", RowSideColors = rcol, ColSideColors = ccol, margins=c(5,10), xlab = "Observations", ylab= "Variables", main = " Heat map plot of DF data")


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)