R tips: loop for variables.
When creating a series of plots, R is able to use loop iteration. Here is a couple of features that R users should know.
- String spliting and pasting.
- Get data variable from a string.
- Print plot in a loop.
####String spliting and pasting ```r loc <- ‘anticodon_stem,anticodon_stem,anticodon_stem,’
#split the string loc. loc_list <- strsplit(loc, split=’,’)
#convert the list to a vector loc_vec <- unlist(loc_list)
#paste strings to create data_name #iterate batches for (batch in c(‘delta’, ‘wt’)) { #iterate stems for (stem in c(‘as’, ‘ds’, ‘cs’, ‘ts’)) { data_name <- paste(‘filtered_data’, batch, stem, sep=’_’) } } ```
####Get data variable from a string ```r #get() get a variable by the name as a string. It is the key to loop over variables. data_name <- ‘filtered_data_wt’ #data_name variable has already existed. data <- get(data_name)
#subdata has not existed, so the string can be used directly as a variable name to be asigned. subdata <- subset(data, loc = ‘acceptor_stem’) subdata_name <- ‘filtered_data_delta_as’ #convert string to variable name. assign(subdata_name, subdata)
#get() converting a string to a column name ggplot(data=plot_data, aes(dGflex_37C, get(paste(‘TS_’, batch, ‘_norm_score’, sep=’’)))) + geom_point(aes(color = isRTD_37C), size=2) ```
####Print plot in a loop ```r for (batch in c(‘delta’, ‘wt’)) { #iterate stems for (stem in c(‘as’, ‘ds’, ‘cs’, ‘ts’)) { data_name <- paste(‘filtered_data’, batch, stem, sep=’’) plot_data <- get(data_name) plot_file_name1 <- paste(‘dot_3D_Plot_RTD_37C_score vs (dGflex_37C-n-TS’, batch, ‘norm)_tpl’, stem, ‘.pdf’, sep=’’)
pdf(plot_file_name1)
#in for loop, auto printing does not work.
print(
ggplot(data=plot_data,
aes(dGflex_37C, get(paste('TS_', batch, '_norm_score', sep='')))) +
geom_point(aes(color = isRTD_37C), size=2) +
theme(axis.text=element_text(size=12),
axis.title=element_text(size=14,face="bold")) +
xlim(c(-30, -15)) + ylim(c(-0.2, 1))
)
dev.off()
} }
```