{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {
        "_cell_guid": "9058dac4-0740-e058-05d9-6339e62f10c5"
      },
      "source": [
        "A good way to visualize the users in the US (state wise)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "c3413e47-8235-db12-9b26-f1c757a5a46a"
      },
      "outputs": [],
      "source": [
        "library(readr)# CSV file I/O, e.g. the read_csv function\n",
        "library(dplyr)#for data manipulation\n",
        "library(plotly)#data visualization\n",
        "\n",
        "page_views = read_csv(\"../input/page_views_sample.csv\")\n",
        "\n",
        "#cleaning the dataset to get the dataset for the views taken place in US alone\n",
        "page_views$country = substr(page_views$geo_location,1,2)\n",
        "page_views_usa = subset(page_views, country == 'US')\n",
        "page_views_usa$state = substr(page_views_usa$geo_location,4,5)\n",
        "state_views <- page_views_usa%>%group_by(state)%>%summarise(volume = n())%>% arrange(desc(volume))\n",
        "head(state_views)\n",
        " \n",
        "#plotly map of the US with a color impressions on the volume based on state\n",
        "state_views$hover <- with(state_views, paste(\"State:\", state, '<br>', \"Volume:\", volume))\n",
        "  \n",
        "  l <- list(color = toRGB(\"white\"), width = 2)\n",
        "     # specify some map projection/options\n",
        "       g <- list(\n",
        "           scope = 'usa',\n",
        "           projection = list(type = 'albers usa'),\n",
        "           showlakes = TRUE,\n",
        "           lakecolor = toRGB('white')\n",
        "         )     \n",
        "     plot_ly(state_views, z = state_views$volume, text = state_views$hover, locations = state_views$state, type = 'choropleth',\n",
        "                                   locationmode = 'USA-states', color = state_views$volume, colors = 'Reds',\n",
        "                                   marker = list(line = l), colorbar = list(title = \"Volume by State\")) %>%\n",
        "         layout(title = 'Breakdown', geo = g)"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "e494c6e9-5cf0-406c-0c2b-34b4ee02eec7"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "7d834918-dfa9-8069-9861-06538d271775"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "56dbe791-aff1-5c98-a369-40a4d91705e0"
      },
      "outputs": [],
      "source": [
        ""
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "_cell_guid": "3baa5a2f-b63e-841b-f043-ac318d5a5479"
      },
      "outputs": [],
      "source": [
        ""
      ]
    }
  ],
  "metadata": {
    "_change_revision": 0,
    "_is_fork": false,
    "kernelspec": {
      "display_name": "R",
      "language": "R",
      "name": "ir"
    },
    "language_info": {
      "codemirror_mode": "r",
      "file_extension": ".r",
      "mimetype": "text/x-r-source",
      "name": "R",
      "pygments_lexer": "r",
      "version": "3.3.2"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}