User:Narya/Notes/Visualization
< User:Narya | Notes
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
library(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()
Plot igraph object
library("igraph") lo<-layout.reingold.tilford(g, root="vertexname") # if there is some hierarchy plot(g,layout=lo, vertex.size=5, vertex.label=V(g), edge.label=E(g)$weight, main = "My title")