Advertisement
Guest User

Untitled

a guest
May 4th, 2015
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.49 KB | None | 0 0
  1. > library("nycflights13")
  2. > library("lubridate")
  3. > library("DiagrammeR")
  4. >
  5. > # Choose a day from 2013 for NYC flight data
  6. > # (You can choose any Julian day, it's interesting to see results for different days)
  7. > day_of_year <- 10
  8. >
  9. > # Get a data frame of complete cases (e.g., flights have departure and arrival times)
  10. > nycflights13 <-
  11. + nycflights13::flights[which(complete.cases(nycflights13::flights) == TRUE), ]
  12. >
  13. > # Generate a POSIXct vector of dates using the 'ISOdatetime' function
  14. > # Columns 1, 2, and 3 are year, month, and day columns
  15. > # Column 4 is a 4-digit combination of hours (00-23) and minutes (00-59)
  16. > date_time <-
  17. + data.frame("date_time" =
  18. + ISOdatetime(year = nycflights13[,1],
  19. + month = nycflights13[,2],
  20. + day = nycflights13[,3],
  21. + hour = gsub("[0-9][0-9]$", "", nycflights13[,4]),
  22. + min = gsub(".*([0-9][0-9])$", "\\1", nycflights13[,4]),
  23. + sec = 0, tz = "GMT"))
  24. >
  25. > # Add the POSIXct vector 'date_time' to the 'nycflights13' data frame
  26. > nycflights13 <- cbind(date_time, nycflights13)
  27. >
  28. > # Select flights only from the specified day of the year 2013
  29. > nycflights13_day <-
  30. + subset(nycflights13,
  31. + date_time >= ymd('2013-01-01', tz = "GMT") + days(day_of_year - 1) &
  32. + date_time < ymd('2013-01-01', tz = "GMT") + days(day_of_year))
  33. >
  34. > # Create the 'nodes' data frame where at least one column is named "nodes" or "node_id"
  35. > # Column 12 is the 3-letter code for the airport departing from
  36. > # Column 13 is for the airport arriving to
  37. > # (Option: change df to 'nycflights13_day' and only airports used for the day will be included)
  38. > nodes_df <- create_nodes(nodes = unique(c(nycflights13[,12],
  39. + nycflights13[,13])),
  40. + label = FALSE)
  41. >
  42. > # The 'edges' data frame must have columns named 'edge_from' and 'edge_to'
  43. > # The color attribute is determined with an 'ifelse' statement, where
  44. > # column 8 is the minutes early (negative values) or minutes late (positive values)
  45. > # for the flight arrival
  46. > edges_df <- create_edges(edge_from = nycflights13_day[,12],
  47. + edge_to = nycflights13_day[,13],
  48. + color = ifelse(nycflights13_day[,8] < 0,
  49. + "green", "red"))
  50. >
  51. > # Set the graph diagram's default attributes for...
  52. >
  53. > # ...nodes
  54. > node_attrs <- c("style = filled", "fillcolor = lightblue",
  55. + "color = gray", "shape = circle", "fontname = Helvetica",
  56. + "width = 1")
  57. >
  58. > # ...edges
  59. > edge_attrs <- c("arrowhead = dot")
  60. >
  61. > # ...and the graph itself
  62. > graph_attrs <- c("layout = circo",
  63. + "overlap = false",
  64. + "fixedsize = true",
  65. + "ranksep = 3",
  66. + "outputorder = edgesfirst")
  67. >
  68. > # Generate the graph diagram in the RStudio Viewer.
  69. > # The green lines show flights that weren't late (red indicates late arrivals)
  70. > # This graph is for a single day of flights, airports that are unconnected on a
  71. > # given day may be destinations on another day
  72. > graphviz_graph(nodes_df = nodes_df, edges_df = edges_df,
  73. + graph_attrs = graph_attrs, node_attrs = node_attrs,
  74. + edge_attrs = edge_attrs, directed = TRUE) %>>%
  75. + graphviz_render(width = 1200, height = 800)
  76. Error: could not find function "%>>%"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement