library(readr)# CSV file I/O, e.g. the read_csv function
library(dplyr)#for data manipulation
library(plotly)#data visualization

page_views = read_csv("../input/page_views_sample.csv")

#cleaning the dataset to get the dataset for the views taken place in US alone
page_views$country = substr(page_views$geo_location,1,2)
page_views_usa = subset(page_views, country == 'US')
page_views_usa$state = substr(page_views_usa$geo_location,4,5)
state_views <- page_views_usa%>%group_by(state)%>%summarise(volume = n())%>% arrange(desc(volume))
head(state_views)
 
#plotly map of the US with a color impressions on the volume based on state
state_views$hover <- with(state_views, paste("State:", state, '<br>', "Volume:", volume))
  
  l <- list(color = toRGB("white"), width = 2)
     # specify some map projection/options
       g <- list(
           scope = 'usa',
           projection = list(type = 'albers usa'),
           showlakes = TRUE,
           lakecolor = toRGB('white')
         )     
     plot_ly(state_views, z = state_views$volume, text = state_views$hover, locations = state_views$state, type = 'choropleth',
                                   locationmode = 'USA-states', color = state_views$volume, colors = 'Reds',
                                   marker = list(line = l), colorbar = list(title = "Volume by State")) %>%
         layout(title = 'Breakdown', geo = g)