In order to see all the code click on Code in upper right and choose “Show all code”.
getwd() - This command return the current working directory of R session. all files read, save etc will happen in that directory by default.
setwd("path/to/the/right/directory") - Change (set) working directory. Windows users should use backslash/ to seperate directories and not normal slash (\) as default in windows.
read.csv("filename.csv") - read CSV (Comma Seperated Values) file. File name should be between qutotaion mark. by default it take the first line as header (or names) to the columns.
View(variable name) - Command that show nicely the variable in RStudio dedicated window.
vector[5] - return the 5 th element in a vector.
data.table[1,] - Return the first row and all columns from a a table.
data.table[,1] - Return the first columns and all rows from a a table.
names(variable) - Return the names (or columns) of the table.
Data file: Iris1A.csv
getwd()
file1 = read.csv("Iris1A.csv")
pl8reader = file1
# View(pl8reader)
pl8reader[1,]
pl8reader[,1]
names(pl8reader)[1]
Install packages - you can use command install.packages("package-name"). Or you can use package tab (lower right pane) and clock install.
To use the package (or library) you can click on square near package name in the right window. Or better use the command library(package-name).
# install.packages("plotly")
library(plotly)
plot_ly( data = pl8reader
,x = pl8reader$Time..s.
,y = pl8reader[,3]
)
plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s.
,y = pl8reader[,3]
)
Google: plotly add line to plot plotly add name to legend
add_trace() - Command to add new trace to a graph. more information in the documentation page.
Another way to get the documentation is type in the console ?function-name anc click enter. or ??package-name for package help.
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s.
,y = pl8reader[,3]
,name = colnames(pl8reader)[3]
)
add_trace(p
,y = pl8reader[,4]
,name = colnames(pl8reader)[4]
)
layout() - documentation page Google: plotly r add main title
In order to show correct time in hours I just duvude the seconds value with 60*60=3600.
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s. /3600
,y = pl8reader[,3]
,name = colnames(pl8reader)[3]
)
p = add_trace(p
,y = pl8reader[,4]
,name = colnames(pl8reader)[4]
)
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm")
,xaxis = list(title = "Time (H)")
)
Google: plotly r error bars
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s./3600
,y = pl8reader[,3]
,name = colnames(pl8reader)[3]
,error_y = ~list(value = pl8reader[,5])
)
p = add_trace(p
,y = pl8reader[,4]
,name = colnames(pl8reader)[4]
,error_y = ~list(value = pl8reader[,6])
)
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm")
,xaxis = list(title = "Time (H)")
)
Google: plotly r area
upper_errorB4 = pl8reader[,3] + pl8reader[,5]
lower_errorB4 = pl8reader[,3] - pl8reader[,5]
upper_errorCa = pl8reader[,4] + pl8reader[,6]
lower_errorCa = pl8reader[,4] - pl8reader[,6]
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s./3600
,y = pl8reader[,3]
,name = colnames(pl8reader)[3]
)
p = add_trace(p, y = lower_errorB4)
p = add_trace(p, y = upper_errorB4, fill = "tonexty")
p = add_trace(p, y = pl8reader[,4], name = colnames(pl8reader)[4])
p = add_trace(p, y = lower_errorCa)
p = add_trace(p, y = upper_errorCa, fill = "tonexty")
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm")
,xaxis = list(title = "Time (H)")
)
edge = list(color = 'rgba(0,0,0,0)')
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s./3600
,y = pl8reader[,3]
,name = colnames(pl8reader)[3]
)
p = add_trace(p, y = lower_errorB4, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorB4, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
p = add_trace(p, y = pl8reader[,4],name = colnames(pl8reader)[4])
p = add_trace(p, y = lower_errorCa, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorCa, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm")
,xaxis = list(title = "Time (H)")
)
No grid no zeroline.
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
,x = pl8reader$Time..s./3600
,y = pl8reader[,3]
,name = colnames(pl8reader)[3]
)
p = add_trace(p, y = lower_errorB4, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorB4, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
p = add_trace(p, y = pl8reader[,4],name = colnames(pl8reader)[4])
p = add_trace(p, y = lower_errorCa, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorCa, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm", showgrid = FALSE)
,xaxis = list(title = "Time (H)", showgrid = FALSE, zeroline = F)
)
View(pl8reader)
seq(from = 1, to = 35, by = 2.3)
for(i in seq(from = 3, to = 29, by = 4))
{print(names(pl8reader)[i])}
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
)
for(i in seq(from = 3, to = 29, by = 4)){
j=i+1
upper_errorB4 = pl8reader[,i] + pl8reader[,i+2]
lower_errorB4 = pl8reader[,i] - pl8reader[,i+2]
lower_errorB4[lower_errorB4<0] = 0
upper_errorCa = pl8reader[,j] + pl8reader[,j+2]
lower_errorCa = pl8reader[,j] - pl8reader[,j+2]
lower_errorCa[lower_errorCa<0] = 0
p = add_trace(p, y = pl8reader[,i], name = colnames(pl8reader)[i], color = bbb[i])
p = add_trace(p, y = lower_errorB4, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorB4, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
p = add_trace(p, y = pl8reader[,j],name = colnames(pl8reader)[j], color = bbb[i])
p = add_trace(p, y = lower_errorCa, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorCa, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
}
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm", showgrid = FALSE)
,xaxis = list(title = "Time (H)", showgrid = FALSE, zeroline = F))
Data file: Iris2A.csv
file2 = read.csv("Iris2A.csv")
pl8reader = file2
dim(pl8reader)
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
)
for(i in seq(from = 2, to = 25, by = 4)){
j=i+1
upper_errorB4 = pl8reader[,i] + pl8reader[,i+2]
lower_errorB4 = pl8reader[,i] - pl8reader[,i+2]
lower_errorB4[lower_errorB4<0] = 0
upper_errorCa = pl8reader[,j] + pl8reader[,j+2]
lower_errorCa = pl8reader[,j] - pl8reader[,j+2]
lower_errorCa[lower_errorCa<0] = 0
p = add_trace(p, y = pl8reader[,i],name = colnames(pl8reader)[i])
p = add_trace(p, y = lower_errorB4, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorB4, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
p = add_trace(p, y = pl8reader[,j],name = colnames(pl8reader)[j])
p = add_trace(p, y = lower_errorCa, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorCa, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
}
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm", showgrid = FALSE)
,xaxis = list(title = "Time (H)", showgrid = FALSE, zeroline = F))
Data file: Iris3A.csv
Google: r read csv skip empty lines There is some problem about std
# file3 = read.csv("Iris3A.csv")
# file3 = read.csv(file = "Iris3A.csv" ,skip = as.numeric(rownames(file3[which(file3[,1]!=''),])[1]))
# pl8reader = file3[3:length(file3),]
#
# View(pl8reader)
p = plot_ly( data = pl8reader
,type = "scatter"
,mode = "lines"
)
for(i in seq(from = 2, to = 29, by = 4)){
j=i+1
upper_errorB4 = pl8reader[,i] + pl8reader[,i+2]
lower_errorB4 = pl8reader[,i] - pl8reader[,i+2]
lower_errorB4[lower_errorB4<0] = 0
upper_errorCa = pl8reader[,j] + pl8reader[,j+2]
lower_errorCa = pl8reader[,j] - pl8reader[,j+2]
lower_errorCa[lower_errorCa<0] = 0
p = add_trace(p, y = pl8reader[,i],name = colnames(pl8reader)[i])
p = add_trace(p, y = lower_errorB4, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorB4, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
p = add_trace(p, y = pl8reader[,j],name = colnames(pl8reader)[j])
p = add_trace(p, y = lower_errorCa, showlegend = F, line = edge)
p = add_trace(p, y = upper_errorCa, fill = "tonexty",showlegend = F, line = edge, fillcolor = 'rgba(1,1,1,0.1)')
}
layout(p
,title = "Growth curve"
,yaxis = list(title = "OD<sub>600</sub> nm", showgrid = FALSE)
,xaxis = list(title = "Time (H)", showgrid = FALSE, zeroline = F))