Countries I have visited

Ulf Hamster 4 min.
R travelling holidays residency

Load libraries

Install required packages if neccessary.

install.packages(c("tidyverse", "rworldmap"))
suppressMessages(library(tidyverse))
suppressMessages(library(rworldmap))

The Dataset

df <- tribble(
  ~Country, ~Year, ~Days, ~Notes,
  "Germany", 1981, 120, "infant",
  "Germany", 1982, 120, "infant",
  "Egypt", 1982, 245, "infant",
  "Egypt", 1983, 365, "baby",
  "Egypt", 1984, 120, "baby",
  "Germany", 1984, 245, "baby",
  "Germany", 1985, 365, "baby",
  "Germany", 1986, 365, "childhood",
  "Germany", 1987, 365, "childhood",
  "Germany", 1988, 365, "childhood",
  "Germany", 1989, 344, "childhood",
  "Netherlands", 1989, 21, "holiday camp in Ameland",
  "Germany", 1990, 344, "childhood",
  "Netherlands", 1990, 21, "holiday camp in Ameland",
  "Germany", 1991, 344, "childhood",
  "Netherlands", 1991, 21, "holiday camp in Ameland",
  "Germany", 1992, 344, "childhood",
  "Netherlands", 1992, 21, "holiday camp in Ameland",
  "Germany", 1993, 365, "childhood",
  "Germany", 1994, 365, "childhood",
  "Germany", 1995, 365, "childhood",
  "Germany", 1996, 365, "childhood",
  "Germany", 1997, 365, "childhood",
  "Germany", 1998, 365, "childhood",
  "Germany", 1999, 365, "childhood",
  "Germany", 2000, 365, "childhood",
  "Germany", 2001, 355, "high school graduation, compulsory community service",
  "France", 2001, 10, "hitch hiking from Paris to the Atlantic coast",
  "Germany", 2002, 30, "compulsory community service",
  "Taiwan", 2002, 120, "Chinese study in Taipei",
  "Germany", 2002, 215, "internship, regular studies in Bremen",
  "Germany", 2003, 361, "regular studies in Bremen",
  "Belgium", 2003, 4, "Belgium 2003 - Estiem",
  "Germany", 2004, 355, "regular studies in Bremen",
  "Hungary", 2004, 10, "Hungary 2004 - Estiem",
  "Germany", 2005, 173, "regular studies in Bremen",
  "Romania", 2005, 7, "VWI group trip to Romania",
  "Singapore", 2005, 185, "study abroad in  Singapore",
  "Singapore", 2006, 235, "study abroad in  Singapore",
  "Malaysia", 2006, 10, "Island trip to nowhere",
  "Indonesia", 2006, 5, "trip to Padang, Sumatra",
  "Thailand", 2006, 5, "Bangkok sightseeing. Stayover to HK.",
  "Hong Kong", 2006, 2, "Stayover in HK to enter China",
  "China", 2006, 5, "Visit Marius in Kunming (Yunnan)",
  "Germany", 2006, 120, "regular studies in Bremen",
  "Germany", 2007, 365, "regular studies in Bremen",
  "Germany", 2008, 365, "regular studies in Bremen",
  "Germany", 2009, 365, "regular studies in Bremen",
  "Germany", 2010, 365, "regular studies in Bremen",
  "Germany", 2011, 365, "work in Duesseldorf",
  "Germany", 2012, 365, "work in Duesseldorf",
  "Germany", 2013, 365, "work in Duesseldorf",
  "Germany", 2014, 365, "work in Duesseldorf",
  "Germany", 2015, 365, "work in Duesseldorf",
  "Germany", 2016, 365, "work in Duesseldorf",
  "Germany", 2017, 342, "work in Duesseldorf",
  "Italy", 2017, 7, "Rome sightseeing",
  "Morocco", 2017, 7, "winter holidays",
  "Turkey", 2017, 7, "winter holidays",
  "Germany", 2018, 313, "work in Duesseldorf",
  "Lithuania", 2018, 4, "trip to Vilnius",
  "Denmark", 2018, 3, "trip to Kopenhavn",
  "Latvia", 2018, 4, "trip to Riga",
  "Italy", 2018, 6, "2x Bergamo visit. Hiking",
  "United Kingdom", 2018, 5, "2x short trips",
  "Poland", 2018, 3, "Weekend trip to Warsawa",
  "Luxembourg", 2018, 3, "short trip",
  "Belgium", 2018, 1, "short trip",
  "Spain", 2018, 7, "winter holidays, Nov., Fuerteventura",
  "Spain", 2018, 7, "winter holidays, Dec., Gran Canaria",
  "Netherlands", 2018, 1, "stay over in Amsterdam",
  "Belgium", 2018, 10, "Antwerp and Brussels, each 5 days",
  "Germany", 2018, 2, "Berlin stayover",
  "Singapore", 2018, 5, "stayover",
  "Indonesia", 2018, 10, "Bali",
  "Australia", 2018, 83, "VIC 33, ACT 3, NSW 17, QLD 27",
  "United States of America", 2018, 8, "Hawaii, Honolulu",
  "Japan", 2018, 20, "Osaka/Kyoto/Nara 2 wk, Tokyo 1wk",
  "South Korea", 2018, 6, "Seoul"
)

How many days did I spend in each country?

# sum up days for each country
df2 <- aggregate(df$Days, by=list(df$Country), FUN=sum)
# add column names
names(df2) <- c("Country", "Days")
# order by the longest total stay
df2 = df2[order(df2$Days, decreasing=T), ]

Top-10

head(df2, 10)

Categorize Length of Stay

Label each country by the length of stay.

df3 <- df %>% mutate(Type=cut(Days, 
  breaks=c(0, 7, 30, 90, Inf),
  labels=c("short", "holiday", "extended", "residency"),
  include.lowest = TRUE))

# eyeball the dataframe
head(df3 %>% slice(10:16))

Places of residency [in years]

df4r <- df3 %>% 
  filter(Type=='residency') %>% 
  group_by(Country) %>% summarize(Years = sum(Days)/365) %>%
  arrange(desc(Years))

df4r

Extended Stays [in months]

df4e <- df3 %>% 
  filter(Type=='extended') %>% 
  group_by(Country) %>% summarize(Months = sum(Days)/30.5, Visits = n()) %>%
  arrange(desc(Months))

df4e

Holidays [in weeks]

df4h <- df3 %>% 
  filter(Type=='holiday') %>% 
  group_by(Country) %>% summarize(Weeks = sum(Days)/7., Visits = n()) %>%
  arrange(desc(Weeks))

df4h

Short Trips [in days]

df4s <- df3 %>% 
  filter(Type=='short') %>% 
  group_by(Country) %>% summarize(Days = sum(Days), Visits = n()) %>%
  arrange(desc(Days))

df4s

Visualize

Preprocess

# Aggregate days and use its log. 
# Use LogDays with 'catMethod="fixedWidth"'
# Use Days 'catMethod="logFixedWidth"'
df5 <- df %>% group_by(Country) %>% summarize(
    LogDays = log(sum(Days)), Days = sum(Days)) 


# match country names
matched <- joinCountryData2Map(
    df5, joinCode="NAME", nameJoinColumn="Country")
28 codes from your data successfully matched countries in the map
0 codes from your data failed to match with a country code in the map
215 codes from the map weren't represented in your data

World

# generate the world map
mapCountryData(
    matched, mapRegion = "world",
    mapTitle="Countries I have visited", 
    #nameColumnToPlot="LogDays", catMethod = "fixedWidth", 
    nameColumnToPlot="Days", catMethod = "logFixedWidth",
    colourPalette = "heat",
    oceanCol = rgb(208, 244, 248, maxColorValue=255),
    missingCountryCol = "white",
    addLegend = FALSE
)

png

Europe

mapCountryData(
    matched, mapRegion = "europe",
    mapTitle="Countries I have visited in Europe", 
    #nameColumnToPlot="LogDays", catMethod = "fixedWidth", 
    nameColumnToPlot="Days", catMethod = "logFixedWidth",
    colourPalette = "heat",
    oceanCol = rgb(208, 244, 248, maxColorValue=255),
    missingCountryCol = "white",
    addLegend = FALSE
)

png