Display Data on a World Map with rworldmap

Online Search Interest for “Flowers”

It’s no surprise that every year, online search interest for “Flowers” is the highest around Mother’s Day as people are looking for the perfect flowers for Mom. Take a look at the time series plot below for worldwide data from 2018:

Source: Google Trends, Search Interest on a Scale from 1-100

We can see that online search interest for “Flowers” peaked right before Mother’s Day, followed by the week before Valentines Day, but how does search interest vary around the world?

R’s rworldmap package easily allows you to make a heat map of countries, which will show us which countries have the highest online search interest for “Flowers”.

First, install and load the rworldmap package. I would also suggest installing and loading the RColorBrewer package which will give you access to several preset color palettes that will make your heat map look more visually appealing.

Next, make sure you have your data loaded into RStudio. I used Google Trends data for search interest over the past year. If you are looking for online search trends, I highly recommend Google Trends as it is easy to use and to get data from.

We can see above that I assigned my data file to the data frame “MapData” and also assigned the column names. The head() function allows you to preview the first few rows of your data frame.

One note about my data file, Google Trends data does not include the Latitude and Longitude coordinates. I entered these afterwards as they are necessary if you want to use the rworldmap package. I used the list Here.

With both the packages and data installed, I joined my data to the preset map in rworldmap. This involved identifying a country code column in my data that would be used to match the country identifier in the rworldmap library. This can be seen below:

Assigned to new data frame “mapped_data”, I used the joinCountryData2Map() function from the rworldmap package on my original “MapData” data frame.

The argument joinCode tells rworldmap to join the countries from my “MapData” data frame to it’s country identifiers by “NAME”. The argument nameJoinColumn tells rworldmap the name of the column in “MapData” that contains the country name.

Now, the “mapped_data” data frame has the countries from “MapData” joined to their respective country identifier in rworldmap. Once the data frame was created, the only thing left was to display it.

Using rworldmap’s mapCountryData() function on the “mapped_data” data frame I was able to plot the heat map.

The argument nameColumnToPlot tells rworldmap which column’s values to use for the heat map. The argument col assigns which colors to use. Click here for more information on how to use the RColorBrewer package. Lastly, the argument oceanCol tells rworldmap what color to make the ocean in the map. In this case, I used white instead of ocean blue so that the heat map colors really pop.

Now, let’s take a look at the heat map!

And there we have a world heat map showing online search interest for “flowers” over the past year. In purple, we can clearly see which countries showed the most interest for online “flowers” search and in orange, which countries had the least interest.

Do these results surprise you? What might change the results of this heat map? Let me know!