Difference between revisions of "User:Narya/Notes/Visualization"
< User:Narya | Notes
(R code examples for plots) |
|||
Line 4: | Line 4: | ||
* [http://igraph.org/r/doc/plot.common.html iGraph] | * [http://igraph.org/r/doc/plot.common.html iGraph] | ||
* [http://www.cbs.dtu.dk/~eklund/beeswarm/ Beeswarm] | * [http://www.cbs.dtu.dk/~eklund/beeswarm/ Beeswarm] | ||
+ | |||
+ | == Code examples == | ||
+ | |||
+ | === Data manipulation === | ||
+ | Read data from file | ||
+ | data<-read.csv("somefile.csv", header = TRUE, sep = "\t", dec = ".") | ||
+ | |||
+ | Write data to file | ||
+ | write.csv(my.data.object, file="blah.csv") | ||
+ | |||
+ | Group by one or more columns | ||
+ | library("plyr") | ||
+ | data.grouped<-ddply(data, .(col1,col2), summarise, freq=length(some.other.column)) | ||
+ | |||
+ | Join tables | ||
+ | df.new<-merge(dataset1, dataset2, by.x="id", by.y="my.id") | ||
+ | |||
+ | === Beeswarm === | ||
+ | def.par <- par(no.readonly = TRUE) # save default, for resetting... | ||
+ | |||
+ | # Enlarge margins: mar=c(bottom, left, top, right) | ||
+ | # Allow to draw outside the plot region: xpd=TRUE | ||
+ | # vertical axis labels: las=2 | ||
+ | par(mar=c(8,4,4,2), xpd=TRUE) | ||
+ | |||
+ | # Make the beeswarm plot | ||
+ | beeswarm(perc_reads ~ ID, data = grouped_data, | ||
+ | log = TRUE, pch = 16, | ||
+ | pwcol=clone.color, | ||
+ | main = 'Expansion', method="swarm", corral="wrap", corralWidth=0.7, | ||
+ | las=2) | ||
+ | # Add legend | ||
+ | coord<-locator(1) # get location via mouse click | ||
+ | legend(coord$x,coord$y, legend = c("GEM1","MAIT","GEM2"), | ||
+ | title = "Cell types", pch = 16, col = c(2,3,4),cex=0.75) | ||
+ | |||
+ | par(def.par)#- reset to default | ||
+ | |||
+ | === XY plot with points of different size === | ||
+ | def.par <- par(no.readonly = TRUE) # save default, for resetting... | ||
+ | par(mar=c(8, 8, 4, 4) + 0.1) | ||
+ | symbols(x=data$v_start, y=data$j_start, circles=data$freq, xaxt='n', yaxt='n', inches=1/3, ann=F, bg="steelblue2", fg=NULL, xlim=c(22050000,22800000), ylim=c(22940000,23020000)) | ||
+ | title(main=inFile) | ||
+ | axis(1, at=data$v_start, labels=data$v_name, las=2) | ||
+ | axis(2, at=data$j_start, labels=data$j_name, las=2) | ||
+ | par(def.par)#- reset to default | ||
+ | dev.off() |
Revision as of 14:26, 21 January 2015
Contents
Documentation visualization in R
Code examples
Data manipulation
Read data from file
data<-read.csv("somefile.csv", header = TRUE, sep = "\t", dec = ".")
Write data to file
write.csv(my.data.object, file="blah.csv")
Group by one or more columns
library("plyr") data.grouped<-ddply(data, .(col1,col2), summarise, freq=length(some.other.column))
Join tables
df.new<-merge(dataset1, dataset2, by.x="id", by.y="my.id")
Beeswarm
def.par <- par(no.readonly = TRUE) # save default, for resetting...
# Enlarge margins: mar=c(bottom, left, top, right) # Allow to draw outside the plot region: xpd=TRUE # vertical axis labels: las=2 par(mar=c(8,4,4,2), xpd=TRUE)
# Make the beeswarm plot beeswarm(perc_reads ~ ID, data = grouped_data, log = TRUE, pch = 16, pwcol=clone.color, main = 'Expansion', method="swarm", corral="wrap", corralWidth=0.7, las=2) # Add legend coord<-locator(1) # get location via mouse click legend(coord$x,coord$y, legend = c("GEM1","MAIT","GEM2"), title = "Cell types", pch = 16, col = c(2,3,4),cex=0.75)
par(def.par)#- reset to default
XY plot with points of different size
def.par <- par(no.readonly = TRUE) # save default, for resetting... par(mar=c(8, 8, 4, 4) + 0.1) symbols(x=data$v_start, y=data$j_start, circles=data$freq, xaxt='n', yaxt='n', inches=1/3, ann=F, bg="steelblue2", fg=NULL, xlim=c(22050000,22800000), ylim=c(22940000,23020000)) title(main=inFile) axis(1, at=data$v_start, labels=data$v_name, las=2) axis(2, at=data$j_start, labels=data$j_name, las=2) par(def.par)#- reset to default dev.off()