Skip to content

Instantly share code, notes, and snippets.

@cavedave
Last active May 28, 2019 05:44
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save cavedave/2b99bd3b4e966c4f0211b6544a948026 to your computer and use it in GitHub Desktop.
Save cavedave/2b99bd3b4e966c4f0211b6544a948026 to your computer and use it in GitHub Desktop.
library(dplyr)
library(lubridate)
library(dplyr)
library(tidyr)
library(ggplot2)
library(animation)
library(viridis)
library(scales)
library(ggthemes)
data <- read.csv("gridwatch.csv")
#make dataframe smaller. Though you could just download less columns
data2<-select (data,-c(french_ict,dutch_ict,irish_ict,ew_ict,north_south,scotland_england,other,nemo))
#get date int data format
data2$timestamp<-ymd_hms(data$timestamp)
#instead of checks every hour make a column grouped bythe days amount stored
data3<-data2 %>% group_by(day=floor_date(data2$timestamp, "day")) %>%
summarize(amount=sum(coal), rest=sum(demand))
#make a column with % coal
data3<-data3 %>%
mutate(perCoal = (amount/rest)*100)
#more cleaning up the dataframe
data3 <- data.frame(amount=data3$amount,
rest=data3$rest,
perCoal=data3$perCoal,
date = data3$day,
year = as.numeric(format(data3$day, format = "%Y")),
month = as.numeric(format(data3$day, format = "%m")),
day = as.numeric(format(data3$day, format = "%d")))
#Find day of the year
data3$doy <- strftime(data3$date, format = "%j")
#Make day a number. This means margin has to be changed later to reduce gaps at axis
data3$doy<-as.numeric(data3$doy)
#2011 is not all there so i will remove it
data3<-data3%>%
filter(year > "2011")
#leap day makes the graph a bit ugly
data3<-data3%>%
filter(doy != "366")
#small number of values over 50% so ill floor them for visual reasons.
data3$perCoal[data3$perCoal >50] <- 50
#make bins for the data
data3$Percent <- cut(data3$perCoal, breaks = seq(-5, 55, by = 5))
#make ugly graph
gg <- ggplot(data3, aes(x=doy, y=year, fill=Percent))#perCoal
gg <- gg + geom_tile( size=0.1)
gg
#make less ugly graph
cbPalette <- c("#009E73","#f0f0f0","#D4D4D4","#bfbfbf","#B4B4B4","#909090","#7f7f7f","#636363","#494848","#404040","#000000" )
breaks <- c(0,1,5,10,25,50)
# To use for fills, add
gg <- gg + scale_fill_manual(breaks=levels(factor(data3$Percent)), #breaks = c(0,1,5,10,25,50),
values=cbPalette,
labels=c("0", "1", "5","10","15","20","25","30","35","40","50"))
gg <- gg + scale_colour_grey(name="% UK power from Coal")
plot.title = '% UK Electricity from Coal'
plot.subtitle = 'Gridwatch Data'
theme(
plot.title=element_text( hjust=1, vjust=0.5, face='bold')
)
gg <- gg + ggtitle(bquote(atop(.(plot.title), atop(italic(.(plot.subtitle)), ""))))
#get rid of margins
gg<-gg+scale_x_continuous(expand = c(0,0))
gg<-gg+scale_y_continuous(expand = c(0,0))
gg <- gg + labs(x='Day', y=NULL)
gg
#put legend at top
gg <- gg + theme(legend.position="top")
gg <- gg + theme(panel.background = element_blank())
gg
#put this year at bottom
gg<-gg + scale_y_reverse()
#change title and such
gg <- gg + theme(plot.title=element_text(hjust=0.5))
gg <- gg + theme(axis.text=element_text(size=12))
gg <- gg+ theme(plot.title = element_text(size=24))
gg <- gg + theme(axis.ticks=element_blank())
gg
ggsave("fourtTry.png", width = 10, height = 10)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment