Trellis graph in Lattice: changing the points and x scale

299 Views Asked by At

I am trying to make a Trellis graph with Lattice but I am not able to make it as I wish. I like how it looks so far, and I just need some help tuning it to look exactly what I want. I will give you the data I am trying to plot, the R code I am using, the graph I get, and finally the graph that I would like to make.
1. Here is the data:CSV File
2. Here is my R code:

Infection<-read.table("stack.csv", header=TRUE, sep=",")
attach(Infection)
head(Infection)
Infection.st<-stack(Infection[,2:13])
head(Infection.st)
Infection.st[,"gene"]<-Infection[,1]
head(Infection.st)
summary(Infection.st$ind)
Infection.st$ind<-factor(Infection.st$ind, levels=c("Disease.12h", "Control.12h", "Disease.4d", "Control.4d",
                                                    "Disease.3h", "Control.3h", "Disease.24h", "Control.24h",
                                                    "Disease.48h", "Control.48h", "Disease.72h", "Control.72h"))
summary(Infection.st$ind)

xyplot(values~ind|gene, xlab = NULL,layout=c(7,16), main="Gene Expression after infection", auto.key=list(columns = 6, cex = .8),
       pch = 21, cex = .7, scales = list(x = list(draw = FALSE)), data=Infection.st, group=ind)

3) Here is the graph I get: https: //dl.dropboxusercontent.com/u/62277598/Stack.pdf (remove the space after colon)
4) Finally the graph I want would look like this: Desired Graph
Now I would like to get the following things in my graph as you can see in my hand made picture:
a) each time point with different symbols, but disease empty and control filled (same color is okay). Another option is to have different types of items for each point and vary the color for disease and control (for example, red and green).
b) change the scale of the x axis. Since it is time series data, it would be nice to represent it. However it does not to be precise. For example, just a space between the 12h and 4 days, then smaller space after 4 days. It would be nice to have disease and control sample close in each time point.
c) finally it would be great if I could change the scale. There are some genes that are low expressed and I am just plotting it in a new graph. I thought to sort the expression by 4 days and then put genes grouped by the expression level.
PS: I don't have 10 reputation to post more than two links or images. Thanks for your time.

1

There are 1 best solutions below

0
On

I asked this question sometime ago. Here is the solution. The only thing I did not do was to change the distance between the time points. That is actually unnecessary.

library("latticeExtra")
library("scales")

#Command sets the working directory
setwd("/your-directory")

OR<-read.table("stack.csv", header=TRUE, sep=",")
attach(OR)
head(OR)
OR.st<-stack(OR[,2:13])
head(OR.st)
OR.st[,"gene"]<-OR[,1]
head(OR.st)
summary(OR.st$ind)
OR.st$ind<-factor(OR.st$ind, levels=c("Disease-12h", "Disease-4d", "Control-4d", "Disease-3h", "Control-3h", "Disease-24h","Control-24h", "Disease-48h", "Control-48h", "Disease-72h","Control-72h")
summary(OR.st$ind)

#comand to make symbols for each time point
supsym <- trellis.par.get("superpose.symbol")
supsym$col <- c("gold","gold","coral","coral","green","green","red","red","blueviolet","blueviolet","cornflowerblue","cornflowerblue","azure4", "azure4") #this is the line around the symbol
supsym$fill <- c("gold","gold","coral","coral","green","green","red","red","blueviolet","blueviolet","cornflowerblue","cornflowerblue","azure4", "azure4")  #this is the color to fill the symbols
supsym$pch <- c(15,19,23, 24, 15,19,15,19,15,19,15,19,15,19,15,19) #here is where you can change the type of symbol you are using
supsym$cex <- c(.8,.8,.8,.7,.8,.8,.8,.8,.8,.8,.8,.8,.8,.8,.8,.8,.8) #here you can change the size of each symbol
trellis.par.set("superpose.symbol",supsym)
colors()
options(scipen=10)
options(digits=10)

#below is just setting the color of each panel headings
bgColors <- c("gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44",
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44",
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44", 
              "gray44", "gray80", "gray44", "gray80", "gray44")

txtColors <- c("white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white",
               "white", "black", "white", "black", "white")


# Create a function to be passes to "strip=" argument of xyplot
myStripStyle <- function(which.panel, factor.levels, ...) {
  panel.rect(0, 0, 1, 1,
             col = bgColors[which.panel],
             border = 1)
  panel.text(x = 0.5, y = 0.5,
             font=2,
             lab = factor.levels[which.panel],
             col = txtColors[which.panel])
}       



print(xyplot(values~ind|Gene,groups=ind,data=OR.st,
             layout=c(5,7),as.table=TRUE,
             type="p",
             par.strip.text=list(custBgCol=bgColors,
                                 custTxtCol=txtColors),
             strip=myStripStyle,
             auto.key=list(text=c("Disease-12h", "Disease-4d", "Control-4d", "Disease-3h",
             "Control-3h", "Disease-24h","Control-24h", "Disease-48h", "Control-48h", "Disease-72h","Control-72h"),
                           space="bottom", columns=7, pch=8, cex=.8),
             relation="same",
             #use relation="sliced", to have same scales across panels
             #scales = list(x = list(draw = FALSE),y=list(log=TRUE)),
             scales = list(x = list(draw = FALSE, abbreviate = FALSE), y = list(log = 10)),
             yscale.components = yscale.components.log10ticks,
             #aspect = "y",
             #scales=list(alternating=FALSE),
             #between=.5,#space between
             main="Gene Expression after Infection",
             #ylim=c(0,1000),#this changes the y limits
             xlab="",
             ylab=expression('RPKMs')))

Let me know if you have any questions.