Instagram - Fountain Pen

This is some geeky Instagram analysis for Fountain Pens using Pablo Barberá’s instaR package. I like using Instagram for Fountain Pen geekiness. I occasionally share fountain pen pictures. When I learnt about instaR package, I wanted to combine my fountain pen passion with my R passion. Here are the results I got.

First of all, we need to install instaR package to analyze Instagram data. Use following commands to install instaR package.

rm(list=ls())
# Load devtools library
library(devtools)

# First install the package from Github
install_github("instaR", "pablobarbera", subdir = "instaR")

After installing instaR, we need to get authorization from Instagram API. Use the following commands to get authorization from Instagram and to save the authorization information to your local hard drive.

# Load instaR package
library(instaR)

# Follow these instructions (from package help) to get authorization.
# To obtain your app_id and app_secret, do the following steps. First, 
# go to http://instagram.com/developer/ and register your application with 
# any name. Then, run the instaOAuth function with your "Client ID" and 
# "Client Secret" as arguments. It will return a URL, which you will need 
# to paste into the field "OAuth redirect_uri" in your application settings 
# on Instagram. After changing it, press Enter in R. A new browser window 
# will open and sign the token. If everything works works well, you will 
# get a message that says you can return to R.

my_oauth <- instaOAuth("YOUR CLIENT ID", "YOUR CLIENT SECRET", scope = "basic")

# Save authorization file
save(my_oauth, file="~/my_oauth/my_oauth_instagram")

# Load authorizatino file
load("~/my_oauth/my_oauth_instagram")

After creating authorization file we can start analyzing some fountain pen related stuff on Instagram. First I counted the number of shares with tag fountainpen. This can be seen by running the following command.

# Now let's analyze some geeky fountain pen stuff
# ----------------------------------------------------------------------------------------------
# Count the number of posts with "fountainpen" tag.
fp_count <- getTagCount(tag="fountainpen", token = my_oauth)

When I created this blog post the number of posts with fountainpen tag were 69769.

We can also count the brand names. The following commands count the number of posts with the brand names given and then create a data frame. From this data frame, we can create a bar plot of brands.

# Create brand names and count the posts with those brand names.
aurora_count      <- getTagCount(tag="aurora", token = my_oauth)
edison_count      <- getTagCount(tag="edisonpenco", token = my_oauth) + 
                     getTagCount(tag="edisonpen", token = my_oauth)
fc_count          <- getTagCount(tag="franklinchristoph", token = my_oauth)
kaweco_count      <- getTagCount(tag="kaweco", token = my_oauth)
lamy_count        <- getTagCount(tag="lamypen", token = my_oauth)
montblanc_count   <- getTagCount(tag="montblanc", token = my_oauth)
montegrappa_count <- getTagCount(tag="montegrappa", token = my_oauth)
nakaya_count      <- getTagCount(tag="nakaya", token = my_oauth)
noodlers_count    <- getTagCount(tag="noodlers", token = my_oauth)
omas_count        <- getTagCount(tag="omas", token = my_oauth)
parker_count      <- getTagCount(tag="parker", token = my_oauth)
pelikan_count     <- getTagCount(tag="pelikan", token = my_oauth)
pilotpen_count    <- getTagCount(tag="pilotpen", token = my_oauth)
sailorpen_count   <- getTagCount(tag="sailorpen", token = my_oauth)
visconti_count    <- getTagCount(tag="visconti", token = my_oauth)

brand <- c("aurora","edison","fc", "kaweco","lamy","montblanc","montegrappa","nakaya",
           "noodlers","omas","parker","pelikan","pilotpen","sailorpen","visconti")

count <- c(aurora_count, edison_count, fc_count, kaweco_count, lamy_count, 
           montblanc_count, montegrappa_count, nakaya_count, noodlers_count, 
           omas_count, parker_count, pelikan_count, pilotpen_count, 
           sailorpen_count, visconti_count)

brand.data <- data.frame(Brand=brand, Count=count)

# Create bar plot from brand.data
library(ggplot2)
bar.plot <- qplot( data = brand.data, x = Brand, y = Count, geom="bar", stat = "identity" )
bar.plot <- bar.plot + theme(axis.text.x = element_text(angle = 45, size = 10))

plot of chunk unnamed-chunk-5

As seen from the plot, there are some outliers since some brand names are also used by other people who are not fpgeeks. We can remove these outliers and plot it again.

# Remove outlier brands
brand.data <- brand.data[brand.data[,1]!="aurora" & 
                         brand.data[,1]!="montblanc" & 
                         brand.data[,1]!="parker",]

# Replot
bar.plot <- qplot( data = brand.data, x = Brand, y = Count, geom="bar", stat = "identity" ) 
bar.plot <- bar.plot + theme(axis.text.x = element_text(angle = 45, size = 10))

plot of chunk unnamed-chunk-7

This plot looks better. It shows that Omas and Pelikan are the most popular ones. But remember that these tags are also used by non-fpgeeks.

We can also use the information to create maps. The following commands create a world map by using the location information of the Instagram posts. The first example is for fountainpen.

# Map fountainpen tag
fountainpen <- searchInstagram(tag = "fountainpen",
                           token=my_oauth, 
                           n=4500) # Just get 4500 shares

library(grid)

map.data <- map_data("world")

points   <- data.frame(x = as.numeric(fountainpen$lon), y = as.numeric(fountainpen$lat))

fountainpen.map <- ggplot(map.data) 

fountainpen.map <- fountainpen.map + geom_map(aes(map_id = region), 
                                      map = map.data, 
                                      fill = "white", 
                                      color = "grey20", 
                                      size = 0.25) 

fountainpen.map <- fountainpen.map + expand_limits(x = map.data$long, y = map.data$lat) 

fountainpen.map <- fountainpen.map + theme(axis.line = element_blank(), 
                                   axis.text = element_blank(), 
                                   axis.ticks = element_blank(), 
                                   axis.title = element_blank(), 
                                   panel.background = element_blank(), 
                                   panel.border = element_blank(), 
                                   panel.grid.major = element_blank(), 
                                   plot.background = element_blank(), 
                                   plot.margin = unit(0*c(-1.5,-1.5,-1.5,-1.5),"lines"))

fountainpen.map <- fountainpen.map + geom_point(data = points, 
                                        aes(x = x, y = y), 
                                        size = 3, 
                                        alpha = 1/5, 
                                        color = "darkblue")

Here is the map for fountainpen tag.

fountainpen.map

plot of chunk unnamed-chunk-9

In fountain pen community, some tags are occasionally used. Following commands count these tags.

# Count some popular tags in fountain pen community
fpgeeks_count        <- getTagCount(tag="fpgeeks", token = my_oauth)
fountainpenday_count <- getTagCount(tag="fountainpenday", token = my_oauth)

At the time of creation of this blog post, the number of posts with fpgeeks tag were 7157 and the number of posts with fountainpenday tag were 820.

We can also plot a map of fpgeeks tag. Following commands create this map.

# Map fpgeeks tag
fpgeeks <- searchInstagram(tag = "fpgeeks",
                           token=my_oauth, 
                           n=5000)

library(grid)

map.data <- map_data("world")

points   <- data.frame(x = as.numeric(fpgeeks$lon), y = as.numeric(fpgeeks$lat))

fpgeeks.map <- ggplot(map.data) 

fpgeeks.map <- fpgeeks.map + geom_map(aes(map_id = region), 
                                      map = map.data, 
                                      fill = "white", 
                                      color = "grey20", 
                                      size = 0.25) 

fpgeeks.map <- fpgeeks.map + expand_limits(x = map.data$long, y = map.data$lat) 

fpgeeks.map <- fpgeeks.map + theme(axis.line = element_blank(), 
                                   axis.text = element_blank(), 
                                   axis.ticks = element_blank(), 
                                   axis.title = element_blank(), 
                                   panel.background = element_blank(), 
                                   panel.border = element_blank(), 
                                   panel.grid.major = element_blank(), 
                                   plot.background = element_blank(), 
                                   plot.margin = unit(0*c(-1.5,-1.5,-1.5,-1.5),"lines"))

fpgeeks.map <- fpgeeks.map + geom_point(data = points, 
                                        aes(x = x, y = y), 
                                        size = 3, 
                                        alpha = 1/5, 
                                        color = "darkblue")

Here is the map for fpgeeks tag.

fpgeeks.map

plot of chunk unnamed-chunk-12

Similarly we can get a map for fountainpenday.

# Map fountainpenday tag
fountainpenday <- searchInstagram(tag = "fountainpenday",
                           token=my_oauth, 
                           n=500) # Just get 500 shares

library(grid)

map.data <- map_data("world")

points   <- data.frame(x = as.numeric(fountainpenday$lon), y = as.numeric(fountainpenday$lat))

fountainpenday.map <- ggplot(map.data) 

fountainpenday.map <- fountainpenday.map + geom_map(aes(map_id = region), 
                                           map = map.data, 
                                           fill = "white", 
                                           color = "grey20", 
                                           size = 0.25) 

fountainpenday.map <- fountainpenday.map + expand_limits(x = map.data$long, y = map.data$lat) 

fountainpenday.map <- fountainpenday.map + theme(axis.line = element_blank(), 
                                           axis.text = element_blank(), 
                                           axis.ticks = element_blank(), 
                                           axis.title = element_blank(), 
                                           panel.background = element_blank(), 
                                           panel.border = element_blank(), 
                                           panel.grid.major = element_blank(), 
                                           plot.background = element_blank(), 
                                           plot.margin = unit(0*c(-1.5,-1.5,-1.5,-1.5),"lines"))

fountainpenday.map <- fountainpenday.map + geom_point(data = points, 
                                           aes(x = x, y = y), 
                                           size = 3, 
                                           alpha = 1/5, 
                                           color = "darkblue")

Here is the map for fountainpenday tag.

fountainpenday.map

plot of chunk unnamed-chunk-14