Advertisement
Guest User

Untitled

a guest
Dec 7th, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.25 KB | None | 0 0
  1. library(htmlwidgets)
  2. library(magrittr)
  3. library(dygraphs)
  4. library(webshot)
  5. library(dplyr)
  6. library(varhandle)
  7.  
  8. fileName = "data/sledzie.csv"
  9. herrings = read.csv(fileName, header = TRUE, sep = ",", dec = ".")
  10. herrings[herrings == "?"] <- NA
  11.  
  12. completeData <- herrings %>% mutate(
  13. cfin1 = case_when((is.na(cfin1) & !is.na(lag(cfin1))) ~ lag(cfin1), is.na(cfin1) ~ lead(cfin1), TRUE ~ cfin1),
  14. cfin2 = case_when((is.na(cfin2) & !is.na(lag(cfin2))) ~ lag(cfin2), is.na(cfin2) ~ lead(cfin2), TRUE ~ cfin2),
  15. chel1 = case_when((is.na(chel1) & !is.na(lag(chel1))) ~ lag(chel1), is.na(chel1) ~ lead(chel1), TRUE ~ chel1),
  16. chel2 = case_when((is.na(chel2) & !is.na(lag(chel2))) ~ lag(chel2), is.na(chel2) ~ lead(chel2), TRUE ~ chel2),
  17. lcop1 = case_when((is.na(lcop1) & !is.na(lag(lcop1))) ~ lag(lcop1), is.na(lcop1) ~ lead(lcop1), TRUE ~ lcop1),
  18. lcop2 = case_when((is.na(lcop2) & !is.na(lag(lcop2))) ~ lag(lcop2), is.na(lcop2) ~ lead(lcop2), TRUE ~ lcop2),
  19. sst = case_when((is.na(sst) & !is.na(lag(sst))) ~ lag(sst), is.na(sst) ~ lead(sst), TRUE ~ sst)
  20. )
  21.  
  22. completeData <- completeData %>% filter_all(all_vars(!is.na(.)))
  23. groupedData <- completeData %>% group_by(cfin1, cfin2, chel1, chel2, lcop1, lcop2, fbar, recr, cumf, totaln, sst, sal, nao)
  24. groupedData <- groupedData %>% mutate(mean_glony = mean(c(cfin1, cfin2, chel1, chel2, lcop1, lcop2)))
  25. groupedData <- unfactor(groupedData)
  26.  
  27. make_sizes <- function(name){
  28. herr_vec <- groupedData[[name]]
  29. max <- dim(herrings)[1]
  30. average <- as.integer(max/60)
  31. sizes <- c()
  32. sum <- 0
  33.  
  34. for (index in 1:length(herr_vec) ) {
  35. sum <- sum + herr_vec[index]
  36.  
  37. if (index %% average == 0) {
  38. mean <- sum/60
  39. sizes <- c(sizes, mean)
  40. sum <- 0
  41. }
  42. }
  43. return(sizes)
  44. }
  45.  
  46. normalize <- function(x){
  47. ww = ((x - min(x)) / (max(x) - min(x)))
  48. return(ww)
  49. }
  50.  
  51. make_df <- function(x1,x2) {
  52. return(data.frame(
  53. time= c(seq(0,58,1), 60),
  54. value=normalize(make_sizes(x1)),
  55. value2=normalize(make_sizes(x2))
  56.  
  57. ))
  58. }
  59.  
  60. data1 <- make_df("length","sst")
  61. data2 <- make_df("length","nao")
  62. data3 <- make_df("totaln","length")
  63. data4 <- make_df("totaln","fbar")
  64. data5 <- make_df("totaln","cumf")
  65. data6 <- make_df("fbar","cumf")
  66. data7 <- make_df("nao","sst")
  67. data8 <- make_df("nao","mean_glony")
  68.  
  69. p1 <- dygraph(data1, main = "Porównanie dlugosci śledzia do temperatury wody", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Długość śledzia") %>%
  70. dySeries("value2", label = "Temp. wody")
  71. p1
  72.  
  73. p2 <- dygraph(data2, main = "Porównanie dlugosci śledzia do oscylacji północnoatlantyckiej", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Długość śledzia") %>%
  74. dySeries("value2", label = "Oscylacja północnoatlantycka")
  75. p2
  76.  
  77. p3 <- dygraph(data3, main = "Porównanie liczby złowionych śledzi do ich długości", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Liczba złowionych śledzi") %>%
  78. dySeries("value2", label = "Długość śledzi")
  79. p3
  80.  
  81. p4 <- dygraph(data4, main = "Porównanie liczby złowionych śledzi do łącznego rocznego procentu zostawionego narybku", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Liczba złowionych śledzi ") %>%
  82. dySeries("value2", label = "Łączny roczny procent zostawionego narybku")
  83. p4
  84.  
  85. p5 <- dygraph(data5, main = "Porównanie liczby złowionych śledzi do regionalnego rocznego procentu zostawionego narybku", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Liczba złowionych śledzi ") %>%
  86. dySeries("value2", label = "Regionalny procent zostawionego narybku")
  87. p5
  88.  
  89. p6 <- dygraph(data6, main = "Porównanie procentu zostawionego narybku globalnego vs regionalnego", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Globalny roczny %") %>%
  90. dySeries("value2", label = "Regionalny %")
  91. p6
  92.  
  93. p7 <- dygraph(data7, main = "Oscylacja polnoconatlantycka a temperatura wody", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Oscylacja północnoatlantycka") %>%
  94. dySeries("value2", label = "Temp. wody przy powierzchni")
  95. p7
  96.  
  97. p8 <- dygraph(data8, main = "Oscylacja polnoconatlantycka a średnie łączne zagęszczenie glonów", ylab = "Znormalizowana wartość") %>% dyOptions(fillGraph = TRUE, fillAlpha = 0.4) %>% dySeries("value", label = "Oscylacja północnoatlantycka") %>%
  98. dySeries("value2", label = "Średnie zagęszczenie glonów")
  99. p8
  100.  
  101. names <- c("p1.png","p2.png","p3.png","p4.png")
  102.  
  103. width<- 1080
  104. height <- 610
  105.  
  106. saveWidget(p1, "temp.html", selfcontained = FALSE)
  107. webshot("temp.html", file = names[1],
  108. cliprect = c(10,30,width+50,height+50)
  109. ,vwidth = width, vheight = height )
  110.  
  111. saveWidget(p2, "temp.html", selfcontained = FALSE)
  112. webshot("temp.html", file = names[2],
  113. cliprect = c(10,30,width+50,height+50)
  114. ,vwidth = width, vheight = height )
  115.  
  116. saveWidget(p3, "temp.html", selfcontained = FALSE)
  117. webshot("temp.html", file = names[3],
  118. cliprect = c(10,30,width+50,height+50)
  119. ,vwidth = width, vheight = height )
  120.  
  121. saveWidget(p4, "temp.html", selfcontained = FALSE)
  122. webshot("temp.html", file = names[4],
  123. cliprect = c(10,30,width+50,height+50)
  124. ,vwidth = width, vheight = height )
  125.  
  126. saveWidget(p5, "temp.html", selfcontained = FALSE)
  127. webshot("temp.html", file = names[1],
  128. cliprect = c(10,30,width+50,height+50)
  129. ,vwidth = width, vheight = height )
  130.  
  131. saveWidget(p6, "temp.html", selfcontained = FALSE)
  132. webshot("temp.html", file = names[2],
  133. cliprect = c(10,30,width+50,height+50)
  134. ,vwidth = width, vheight = height )
  135.  
  136. saveWidget(p7, "temp.html", selfcontained = FALSE)
  137. webshot("temp.html", file = names[3],
  138. cliprect = c(10,30,width+50,height+50)
  139. ,vwidth = width, vheight = height )
  140.  
  141. saveWidget(p8, "temp.html", selfcontained = FALSE)
  142. webshot("temp.html", file = names[4],
  143. cliprect = c(10,30,width+50,height+50)
  144. ,vwidth = width, vheight = height )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement