Advertisement
Guest User

Untitled

a guest
Feb 18th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.85 KB | None | 0 0
  1. ##p1---------------------------------------------------------
  2.  
  3. #clear memory
  4. rm(list=ls())
  5.  
  6. #list defined variabls
  7. ls()
  8.  
  9. #create/load data
  10. data <- c(1, 2, 3, 4)
  11. data2 <- c(2, 4, 8, 16)
  12.  
  13. #check data
  14. ls()
  15. data
  16. data2
  17.  
  18. #plot graph
  19. plot(data, data2, xlab = "something", ylab = "something else")
  20.  
  21. #find working directory
  22. getwd()
  23.  
  24. #Set working directory (to access files for R)
  25. setwd("\\\\folder")
  26.  
  27. #read data file
  28. filename<-read.csv
  29.  
  30. #help command
  31. help(read.csv)
  32.  
  33. #access each individual column of a data set using "name"$header
  34. datasetname$trial
  35.  
  36. #find column names for data set
  37. names(dataset)
  38.  
  39. #see variables of a data set
  40. attributes(name)
  41.  
  42. #read csv command to read and name a set of data
  43. setname <- read.csv(file="file name",head=TRUE,sep=",")
  44.  
  45. #store number as a variable
  46. letter<-number
  47.  
  48. #initialize a list of numbers the numeric command can be used
  49. a<-numeric(10)
  50.  
  51. #String: a string is specified by using quotes
  52. a<-"hello"
  53.  
  54. #find type of string using
  55. typeof(string name)
  56. eg: typeof(a)
  57.  
  58. #look at file to check several variables
  59. summary(filename$variable)
  60.  
  61. #creating a dataframe
  62. #here we create several separate vectors
  63. > a <- c(1,2,3,4)
  64. > b <- c(2,4,6,8)
  65. > levels <- factor(c("A","B","A","B"))
  66. #we then create a data frame using these vectors
  67. > bubba <- data.frame(first=a,second=b,f=levels)
  68.  
  69. #variable types. 2 variables= TRUE and FALSE
  70. a=TRUE
  71. typeof(a)
  72.  
  73. b=FALSE
  74. typeof(b)
  75.  
  76. #creating a vector and make it into a table
  77. > a <- factor(c("A","A","B","A","B","B","C","A","C"))
  78. > results <- table(a)
  79. #check it
  80. > results
  81. > attributes(results)
  82. > summary(results)
  83.  
  84. #creating two way tables, a=x b=y collums (values are examples)
  85. > a <- c("Sometimes","Sometimes","Never","Always","Always","Sometimes","Sometimes","Never")
  86. > b <- c("Maybe","Maybe","Yes","Maybe","Maybe","No","Yes","No")
  87. > results <- table(a,b)
  88. > results
  89.  
  90. ##p2-----------------------------------------------------------------
  91.  
  92. #summary data set
  93. summary(datasetname)
  94.  
  95. #boxplot
  96. boxplot(quantileofdata[eg.ppm], col="")
  97.  
  98. #abline (add a line annotation)
  99. abline(h=value of Y to anotate) eg:(h=12)
  100.  
  101. #create dashed line
  102. lty=value
  103.  
  104. #histogram
  105. hist(quantiledata[eg. ppm), col="")
  106.  
  107. #show data points on graph
  108. rug(quantiledata)
  109.  
  110. #increase data buckets on graph
  111. graphcommand(breaks="[100]"
  112.  
  113. #add line to graph
  114. abline(v=Y value intended, lwd[line width]value[2]
  115.  
  116. #create table from data in r
  117. name <- table(quantile$quantile)
  118.  
  119. #histogram
  120. hist(quantile/data)
  121.  
  122. #scatter plot
  123. with(data, plot(quantile,quantile))
  124.  
  125. #colour quantile data in plot
  126. col=quantile$quantile
  127.  
  128. #set up plot window for 2 graphs
  129. par(mfrow=c(1,2), mar=c(5,4,2,1))
  130.  
  131. #create variable
  132. variable name <-subset(data, region=="quantile")
  133.  
  134. #make q plot
  135. qplot(displ, hwy, data=dataname)
  136.  
  137. #add gray areas and trndlines
  138. qplot(displ, hwy, data=dataname, geom=c"point", "smooth"))
  139.  
  140. #allpurpose qplot
  141. qplot=drv,hwy,data=dataname,geom="boxplot")
  142.  
  143. #assign boxplots by variable
  144. qplot(drv,hwy,data=mpg,geom="boxplot",color=manufacturer)
  145.  
  146. #histogram display freq for each variable
  147. qplot(hwy, data = dataname, fill =drv)
  148.  
  149. #scatter plot with 3 facets
  150. qplot(hwy, data = dataname, fill =drv)
  151.  
  152. #histogram with 3 facets
  153. qplot(hwy, data = mpg, facets = drv ~ ., binwidth = 2)
  154.  
  155. #lattice to creat object assigned with variable (variable)
  156. variable <- ggplot(mpg, aes(displ,hwy))
  157.  
  158. #add layer to variable
  159. variable(geom_point()
  160.  
  161. #add smooth line to table
  162. +geom_smooth()
  163.  
  164. #change line to linear model(lm)
  165. +geom_smooth(method="lm)
  166.  
  167. #add colour legend to graph
  168. g+geom_point(aes(color=drv), size = 4, alpha = 1/2)
  169.  
  170.  
  171. #ggplot first layer
  172. ggplot(data=pharynx,aes(x=day, y=pumps))
  173.  
  174. #gplot 2nd layer
  175. ggplot(pharynx, aes(x=as.factor(day), y=pumps)) + geom_boxplot(fill="red", alpha=0.2) + xlab("day")
  176.  
  177.  
  178. #qplot boxplot 1
  179. qplot(x=day, y=pumps,color=treatment,data=pharynx,geom="boxplot")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement