Data visualisation avec R

Aujourd'hui, amusons nous avec R et des données de navigation.
https://www.kaggle.com/cwpotwora/climate-data-from-ocean-ships/cliwocvideo/code



Description du dataset
Explore changing climatology with data from early shipping logs
In the mid-eighteenth to nineteenth centuries, navigating the open ocean was an imprecise and often dangerous feat. In order to calculate their daily progress and avoid running/sailing into the unknown, a ship's crew kept a detailed logbook with data on winds, waves, and any remarkable weather.
Handwritten in archived logbooks, these rich datasets were nearly impossible to study until the European Union funded their digitization in 2001. You can visit the EU project website for detailed information on the countries and ships included.
We're hosting the full 1750-1850 dataset on Kaggle to promote the exploration of this unique and invaluable climatology resource.

Le code du script R écrit par Swag. 

library(ggplot2) library(animation) library(stringr) # #Video of output: https://youtu.be/MvxX4JVjJTg # # # #Requires ffmpeg to be installed. I created and ran it on Rstudio. #It took about 10 hours to run the full set on my laptop, if you #just want to get a feel for it I suggest selecting a range such #as [18000:1850] when creating 'dates' CliwocAll <- read.csv("../input/CLIWOC15.csv") #CLiwoc will contain the information taht will be be used to plot. Cliwoc <- CliwocAll[,c("Lat3","Lon3", "Nationality")] #Some British entries have trailing whitespace, this will trim them. Cliwoc$Nationality <- factor(str_trim(Cliwoc$Nationality)) # Builds a date from the month, day, year info, will be used for ordering plots into video # diplay as title and showing progress of script CLiwocDateInfo <- CliwocAll[,c("Year","Month","Day")] Cliwoc$date <- (ISOdate(year = CLiwocDateInfo$Year, month =CLiwocDateInfo$Month,day = CLiwocDateInfo$Day)) # dates will store the unique dates that will be plotted. It will skip date for which ther are no # long/lat points. If a ceratin range needs to be selected for it can be done here. dates <- sort(unique(Cliwoc$date)) mapWorld <- borders("world", colour = "grey50", fill = "grey50") #Animation options ani.options(interval = .05, ani.width = 1920, ani.height = 1080, # this will need to be changed depending on where ffmpeg dpi = 1200, ffmpeg = '.../ffmpeg/2.7.2/bin/ffmpeg') # Defines the colors for the plot. This is used in the manual_scale function to allow # for a legend showing all nations to be displlayed in all plots that make the final video NatPlotColor <- c("American" = "blue2", "British" = "red", "Danish"= "chocolate4", "Dutch" = "magenta", "French" = "purple", "Hamburg" = "mediumvioletred", "Spanish" = "olivedrab1", "Swedish" = "violet") # Used to set limits of the manual scale NatList <- c("American", "British", "Danish", "Dutch", "French", "Hamburg", "Spanish", "Swedish") # Creates the video. Currently plotting everything takes several hours. When editing try to # do all tasks that ca be done outside of the for loop as there are several thousand loops and #the time required can increase quickly. saveVideo( for(dt in seq(1,length(dates),1)){ print(format(dates[dt],'%m/%d/%Y')) # shows what date is being plotted. dtsubset <- subset(Cliwoc, date == dates[dt]) # dtsubset is the data that will be used in the cupprent loop only # saveVideo seems to require plot( mp <- ggplot() +mapWorld +geom_point( data = dtsubset, mapping = aes(x=Lon3, y=Lat3, colour = Nationality), size = 4 ) #xlim is se to +-85 to compensate for blank space that occurs at top and botom of map +coord_fixed( ratio = 1, xlim = c(-180,180), ylim = c(-85,85) ) +scale_colour_manual(values = NatPlotColor, limits = NatList) +theme( panel.background = element_rect(fill = "lightskyblue2"), title = element_text(size = 30), legend.key.size = unit(2,"cm"), legend.text = element_text(size = 20), legend.position = ("right"), legend.key = element_rect(fill = "lightskyblue2"), plot.margin = unit(c(0,0,0,0), "cm") ) +xlab("Longitude") +ylab("Latitude") +guides(colour = guide_legend(override.aes = list(size=3.5))) +ggtitle(paste0("Position of ships on:","\n",format(dates[dt],'%m/%d/%Y'))) ) } ,video.name = "LongLatPlotDateNat1.mp4",img.name = "Frame")

Comments