-
Notifications
You must be signed in to change notification settings - Fork 0
/
ggplot_energy_prod.R
142 lines (104 loc) · 4.93 KB
/
ggplot_energy_prod.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
## Tutorium
# Task 2:
# wordcloud
library(wordcloud)
wordcloud::wordcloud(words = names(lang_count), freq = lang_count, min.freq = 1)
# Matrixes etc -----
'Indexing dataframes etc.'
fruits <- matrix(c(1,2,1,3,1,1,3,1,2), ncol = 3)
fruits[4] # single brackets return values in order of columns
fruits[[2]]
as.data.frame(fruits)[[2]]
# Working with Datasets -----
energy_prod <- read.csv("Data/day2_data_energy_prod_EU_2020-08-03_2020-08-09.csv")
names(energy_prod)
head(energy_prod)
summary(energy_prod)
unique(energy_prod$ProductionTypeName)
library(ggplot2)
# Clean data
energy_prod <- energy_prod[!energy_prod$ActualGenerationOutput > energy_prod$InstalledGenCapacity *4, ]
#nrow(energy_prod)
ggplot(data = energy_prod, aes(x = ProductionTypeName)) +
geom_bar()
g <- ggplot(data = energy_prod, aes(x = InstalledGenCapacity, y=ActualGenerationOutput))
g + geom_point()
plants <- unique(energy_prod$GenerationUnitEIC)
barplot()
group_by(energy_prod, GenerationUnitEIC, ProductionTypeName)
energy_prod %>% group_by(ProductionTypeName) %>% summarize(Sum = sum(GenerationUnitEIC))
df_agg_type <- aggregate(energy_prod$InstalledGenCapacity,
by = list(energy_prod$ProductionTypeName),
FUN = sum)
colnames(df_agg_type) <- c("ProductionTypeName", "InstalledGenCapacity_sum")
df_agg_type$InstalledGenCapacity_sum <- df_agg_type$InstalledGenCapacity_sum/1000
df_agg_type <- df_agg_type[order(df_agg_type$InstalledGenCapacity_sum),] # sort the dataframe by indexing with order
# sort for plotting in the right, y is a character, and ggplot converts it to factor
df_agg_type$ProductionTypeName <- factor(df_agg_type$ProductionTypeName, levels = unique(df_agg_type$ProductionTypeName))
ggplot(data = df_agg_type, aes(x= df_agg_type$ProductionTypeName, y = df_agg_type$InstalledGenCapacity_sum, fill = df_agg_type$ProductionTypeName))+
geom_bar(stat = "identity", show.legend = F)+
#scale_color_manual(name = "Legend", values = unique(df_agg_type$ProductionTypeName))+
coord_flip()+
xlab("Installed Capacity")+
ylab("Production Type")
# Tasks
df_agg_cap <- aggregate(energy_prod$InstalledGenCapacity,
by = list(energy_prod$ProductionTypeName),
FUN = sum)
df_agg_prod <- aggregate(energy_prod$ActualGenerationOutput,
by = list(energy_prod$ProductionTypeName),
FUN = sum)
colnames(df_agg_cap) <- c("ProductionTypeName", "InstalledGenCapacity_sum")
colnames(df_agg_prod) <- c('ProductionTypeName', 'ActualProduction')
merged_df <- merge(df_agg_cap, df_agg_prod, by = "ProductionTypeName")
ggplot(data = energy_prod, aes(x = InstalledGenCapacity, y = ActualGenerationOutput, color = ProductionTypeName))+
geom_point()
ts_prod <- aggregate(energy_prod$InstalledGenCapacity,
by= list(energy_prod$DateTime),
FUN = sum)
colnames(ts_prod) <- c('DateTime', 'ProductionCapacity')
ggplot(data = ts_prod)+
geom_point(aes(x = DateTime, y = ProductionCapacity))
# Spatial Plotting
library(sf)
library(ggspatial)
library(RColorBrewer)
library(terra)
library(ggspatial)
library(raster)
library(ggplot2)
europe <- st_read("Data/ne_10m_admin_0_countries.shp")
ctr <- europe$WB_A2
energy_prod$MapCodev2 <- energy_prod$MapCode
energy_prod$MapCodev2[grep("DE_", energy_prod$MapCodev2)] <- "DE"
energy_prod$MapCodev2 <- gsub("NIE", "GB", energy_prod$MapCodev2)
# Aggregating the dataframe
gen_out <- aggregate(energy_prod[c('ActualGenerationOutput', 'ActualConsumption', 'InstalledGenCapacity')],
by = list(energy_prod$MapCodev2), FUN = sum, na.rm = T)
colnames(gen_out)[1] <- "ctr_code"
# Checking if there are still missing countries
gen_out$ctr_code[!gen_out$ctr_code %in% europe$WB_A2]
europe[europe$NAME_EN == "Norway",]$WB_A2 <- "NO"
st_write(europe, "Data/admin_ctr_cleared.gpkg")
euro_geom <- europe[c("NAME_LONG", "WB_A2", "geometry")]
box <- st_bbox(c(xmin = -30, xmax = 33, ymin = 30, ymax = 81), crs = st_crs(4326))
merged_df <- merge(europe, gen_out, by.x = "WB_A2", by.y = "ctr_code") %>% st_crop(box) %>% st_transform(st_crs(3035))
ggplot(data = merged_df, aes(fill = ActualGenerationOutput))+
geom_sf()+
ggtitle("Generation Output per Country")+
#coord_sf(xlim = c(2521500,6754500), ylim=c(900500,4516500), expand = F)+
#annotation_scale(location = 'bl')+
annotation_north_arrow(location = 'bl', pad_x = unit(0.2, "in"), pad_y = unit(0.5, "in"))+
scale_fill_gradientn("Generated Energy [GW]", colours = brewer.pal(9, 'YlOrBr'))+
theme(legend.position = 'bottom', legend.key.width = unit(0.5, "in"))
rnaturalearth::ne_download(scale = 50, type = "MSR_50")
dem <- rast("data/PRISMA_SR_50M/PRIMSA_SR_50M.tif") %>% crop(box) %>% project("epsg:3035")
crop(dem, )
ggplot()+
geom_stars(data = st_as_stars(dem))
rst_df <- cbind.data.frame(
crds(dem, na.rm = F),
values(dem))
ggplot()+
geom_raster(data = rst_df, aes(x = x, y=y, alpha = value))+
scale_alpha(range = c(1,0), na.value = 0)