1

Does anyone knows a library/technique in R where you can plot a trajectory using multiple timesteps (points) where each point does have a lat/lon coordinate? I have this data

x <- read.table("https://forms.naturwissenschaften.ch/imilast/_ERAinterim_1.5_1979_MTEX/ERAinterim_1.5_NH_M02_19790101_20121231_MTEX.txt?_ga=2.18919096.1825595846.1546710263-1112023567.1546710263", sep="", fill = T, nrows = 1000,
                     header = F, skip = 2) # download only the first 1000 rows
colnames(x) <- c("Code","CycloneNo","StepNo","DateI10","Year","Month","Day","Time","LongE","LatN","Intensity1","Intensity2","Intensity3")

Until now I was using a library called esd (developed by Meteo Norway) - which is very nice (github/metno/esd), where there was a function (read.imilast and plot.trajectory), however, that is not the greatest visualization tool). Also, I tried using openair (trajPlot) but that was quite hard to optimize for my data.

I want to plot each trajectory (column CycloneNo) under a different color if possible and more important, to have like a start and end point for each trajectory.

Any ideas?

3

ggplot2 and dplyr should be enough for this. They're general, not specialised packages, but once you get used to them, they're very versatile.

library(ggplot2)
library(dplyr)    
x <- x %>% filter(Code!=90) ##Just cleaning to remove those 'NA' rows

##Label start and end points
x <- x %>% group_by(CycloneNo) %>% mutate(state=case_when(StepNo==1 ~ "Start",
                                                         StepNo==max(StepNo)~ "End",
                                                         T ~ "NA")) 

##Create visualisation
ggplot(x,aes(x=LongE,y=LatN,group=CycloneNo,color=as.factor(CycloneNo))) + ##This maps the x and y axes and tells the plot to group and colour based on the CycloneNo variable
      geom_path() + ##This will actually then plot the coloured trajectories
      geom_point(data=x %>% filter(state!="NA"),aes(fill=state,shape=state),size=4 ) + ##This adds markers to denote the start and end points of each trajectory
      geom_text(data=x %>% filter(state=="Start"),aes(label=CycloneNo),color="black",vjust=0,hjust=1) + ##This labels the start of each trajectory with the 'CycloneNo' variable
      guides(colour=F) ##This removes the legend for the cyclone numbers (as it's not really necessary/helpful in this case) 

You would need to apply transformations on cyclones 29, 36 and 46 to deal with them crossing the 180degree longitude boundary.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.