Jump to content

How to use R/Tutorials/Introduction: Difference between revisions

From IdeaWazaWiki
wikademia>Orderud
initial module
 
wikademia>Wikademia
m 16 revisions
 
(15 intermediate revisions by 7 users not shown)
Line 1: Line 1:
'''[[w:R programming language|R]]''' is both a programming language, and a software environment for statistical computing.
{{welcome and expand}}
 
[[Category:Programming]]
 
[[Category:Pages moved from Wikibooks]]
== Plotting data with R ==
[[Category:How to use R]]
This tutorial demonstrates how to plot data from files in R.
 
* Given the data file ''data.txt'' located at ''<path>'':
1970    45    63
1980    52    59
1990    59    52
2000    63    45
 
* This data can easilty be loaded into R using the following commands:
setwd("<path>")                # change working directory
data = read.table("data.txt")  # load data
 
* Plotting can then be performed as follows:
# Setup empty plot window
plot(0, type = "n", main = "Plot Title", xlab = "X axis", ylab = "Y axis", xlim = c(1970,2000), ylim = c(0,100))
grid()                              # add grid
lines(data[,1], data[,2], lty = 1)  # draw first dataset
lines(data[,1], data[,3], lty = 2)  # draw second dataset
legend("topright", c("first dataset", "second dataset"), lty = c(1, 2) )
 
* Export of the plot to pdf is easilty performed by adding ''pdf("filename.pdf")'' prior to the plotting, and ''dev.off()'' after the plotting.
 
 
 
 
{{stub}}

Latest revision as of 05:40, 11 April 2009