R scripts

################ggplot Growth Curve ####################

library(ggplot2)

p <- qplot(Minutes, OD, data = OD.ex.52) +
geom_line(aes(linetype = Sample), show_guide = TRUE) +
geom_point(aes(size = Sample), shape = 20, fill = “white”,
show_guide = FALSE) +
scale_y_continuous(limits = c(0,0.3))+
labs(x = “Minutes Incubated”, y = “OD600”) +
ggtitle(“Growth Curve”) +
theme_bw() +
theme(panel.grid.major = element_blank(),
panel.grid.minor = element_blank(),
plot.title = element_text(size = rel(1.2), face = “bold”, vjust = 1.5))

#############load samples effectively#################

testR <- read.csv(file.choose(),
sep=”;”, #separation columns
dec=”,”, #decimal
stringsAsFactors = FALSE, #number not as factors
header=TRUE, # include first row as header
strip.white = TRUE, #delete white spaces introduced by mistake
na.strings = c(“NA”,””)) #separation, decimal, string

###########Working directory and reading files############

getwd() ## get working directory
setwd(“C:/Users/marcelo/files/MSU bioinformatics/practice/”) # change working directory
dir() ##see what is inside the working directory

counts <- read.csv(“fragment_counts.txt”, sep = “\t”, header = F)

#############Read data from console or file#################

sites    <- scan(file, what=”character”)   # read data from file into a vector

> # inputting string data directly from the console
> name.x <- scan(, what=””)
1: bobby
2: kate
3: dave
4: mia
5:
Read 4 items
> name.x
[1] “bobby” “kate” “dave” “mia”

http://www.ats.ucla.edu/stat/r/faq/inputdata_R.htm

######## Split character string vectors into strings of characters#########

stringsplit <- function(string) unlist(strsplit(string, split=””))

##### make everything lowercase#####

tolower(sites)

##### how many sites?#########
nsites <- length(sites)

#####how many characters#####

nbases   <- nchar(sites)

#######Subsetting and changing data in dataframes######

How to change factors within dataframes

levels(metafile$V2)[3:4]<- “KW20”   #change levels 2 and 3 in V2 column

####### how to adjust R to control decimals and scientific notation##########

options( scipen = -1 ) #penalty for scientific notation
options( digits = 1 ) #number of digits to show

 

####### how to jitter points or text in a graph #################

example:

geom_text(aes(label = TF.nov), size=4, vjust=1, hjust=0.5, position = position_jitter(w = 0.1, h = 0.1)) +  # add control jitter to the overlaping text

http://docs.ggplot2.org/current/position_jitter.html

 

###### a very smart function that takes a vector of numbers and calculates the frequency########

p<- function(x) x/(sum(x))

t<- c(3,4,6,8,9)

p(t)

> p(t)
[1] 0.1 0.1 0.2 0.3 0.3

################Uses plyr library to create a dataframe with summary of stats given conditions########################

library(plyr)

# Run the functions length, mean, and sd on the value of “change” for each group,
# broken down by sex + condition
cdata <- ddply(e136, c(“Cx.Co”, “Donor.Origin”), summarise,
N = length(To.Tx),
mean = mean(To.Tx),
sd = sd(To.Tx),
se = sd / sqrt(N)
)
cdata

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s