Skip to content

Instantly share code, notes, and snippets.

@michaelbarton
Created August 4, 2008 13:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michaelbarton/3886 to your computer and use it in GitHub Desktop.
Save michaelbarton/3886 to your computer and use it in GitHub Desktop.
library(reshape)
library(lattice)
#
# Categorical example
#
data1 <- read.csv(file="plots/categorical.csv")
# Reshape the data into long format
data1 <- melt(data1,measure.var=c("Systems.biology","Functional.genomics","Non.coding.RNA"))
# Barplot of number of cups of tea per day
plot1 <- bwplot(value ~ variable, data = data1)
plot1$ylab <- "Cups of Tea per day"
#
# Continuous example
#
data2 <- read.csv(file="/Users/mike/Desktop/plots/continuous.csv")
# Plot data as a scatter plot
plot2 <- xyplot(productivity ~ distance, data=data2)
plot2$xlab <- "Distance to tea making area (feet)"
plot2$ylab <- "Weekly productivity (hours)"
# Add a custom panel with a loess trend
custom_panel_loess <- function(x,y,...){
panel.xyplot(x,y,...)
panel.loess(x,y)
}
plot2$panel <- custom_panel_loess
#
# Factored catergorical data
#
data3<- read.csv(file="/Users/mike/Desktop/plots/categorical_categorical.csv")
# Split data by seasons
winter <- data3[,1:3]
summer <- data3[,4:6]
# Convert to long format
winter <- melt(winter,measure.var=c("SB","FG","ncRNA"))
summer <- melt(summer,measure.var=c("SB.1","FG.1","ncRNA.1"))
# Add the name of the season as an extra column
summer <- cbind(summer,season="summer")
winter <- cbind(winter,season="winter")
# Convert back to a single data set
data3 <- rbind(winter,summer)
# Rename the variables for consistency
levels(data3$variable)[4:6] <- levels(data3$variable)[1:3]
# Print a plot of the data
plot3 <- bwplot(value ~ variable | season, data=data3)
plot3$ylab <- "Cups of Tea per day"
print(plot3)
#
# Factored continuous data
#
data4 <- read.csv("/Users/mike/Desktop/plots/continuous_categorical.csv")
# Name each of the sets of data
water <- cbind(data4[,1:2],drink="water")
tea <- cbind(data4[,3:4],drink="tea")
hipflask <- cbind(data4[,5:6],drink="hipflask")
# Name the columns and bind the data into a single dataset
col.names <- c("volume","productivity")
names(water)[1:2] <- col.names
names(tea)[1:2] <- col.names
names(hipflask)[1:2] <- col.names
data4 <- rbind(water,tea,hipflask)
# Plot the factored data
plot4 <- xyplot(productivity ~ volume | drink, data=data4)
plot4$xlab <- "Average volume ingested (litres / day)"
plot4$ylab <- "Weekly productivity (hours)"
custom_panel <- function(x,y,...){
# Add a custom panel
panel.xyplot(x,y,...)
panel.loess(x,y,col="red",lty=2)
}
plot4$panel <- custom_panel
print(plot4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment