From Technologia Incognita
Jump to: navigation, search

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(, file="blah.csv")

Group by one or more columns

data.grouped<-ddply(data, .(col1,col2), summarise, freq=length(some.other.column))

Join tables<-merge(dataset1, dataset2, by.x="id", by.y="")


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,
         main = 'Expansion', method="swarm", corral="wrap", corralWidth=0.7,
# 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))
axis(1, at=data$v_start, labels=data$v_name, las=2)  # rotate axis label with 'las'
axis(2, at=data$j_start, labels=data$j_name, las=2)
par(def.par)#- reset to default

Plot igraph object

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")