Created
March 18, 2016 04:53
-
-
Save loiyumba/07c23c1585e185d58339 to your computer and use it in GitHub Desktop.
Heatmap of selected Indian Cities
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
### Temperature of Indian Cities ### | |
# Load the require libraries | |
require(data.table) | |
require(ggplot2) | |
require(ggthemes) | |
# Set working directories | |
setwd("..\\Global Temperature") | |
# Load the dataset | |
city <- fread("GlobalLandTemperaturesByCity.csv") | |
# Filter India | |
indian_city <- subset(city, Country == "India") | |
# Correct the date format | |
indian_city$dt <- as.Date(indian_city$dt, format = "%Y-%m-%d") | |
# Extract months from data variable | |
indian_city$month <- format(indian_city$dt, "%m") | |
# Take the average city temperature | |
monthly_temp <- indian_city[, .(Ave_monthly = mean(AverageTemperature, na.rm = TRUE)), by = .(City, month)] | |
# Select few cities of India | |
capital <- monthly_temp[City %in% c("Agartala", "Aizawl", "Delhi", "Amravati", "Imphal", "Chandigarh", "Calcutta", | |
"Hyderabad", "Jaipur", "Bangalore","Lakhnau","Madras", "Port Blair", "Patna", "Shillong", | |
"Shimla", "Srinagar", "Raipur", "Ranchi", "Thiruvananthapuram", "Bhopal", "Dehra Dun", | |
"Bhubaneswar", "Gandhinagar"), ] | |
# Heatmap | |
ggplot(capital, aes(x = month, y = City, fill = Ave_monthly, frame = City)) + | |
geom_tile(color = "white", size = 0.1) + | |
scale_fill_gradient(name = "Average Temperature",low = "white", high = "red") + | |
coord_equal() + | |
labs(x = "Months", y = "", title = "Average Temp from 1816 - 2013") + | |
theme_tufte() + | |
theme(axis.ticks = element_blank()) + | |
theme(axis.text = element_text(size = 14)) + | |
theme(plot.title = element_text(size = 15)) + | |
theme(legend.title = element_text(size = 12)) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment