Answered 2 days AfterMay 17, 2021

Answer To: R programming

Saravana answered on May 20 2021
135 Votes
Bali.rda
Bali.rda
Bali_network_Names.R
## Necessary libraries are loaded
rm(list = ls())
#load the rda file
library(ggnetwork)
library(ggplot2)
file = "/media/priyan/Files/GreyNodes/Assignment11/Bali.rda"
load(file = file)
## Network is created with ggnetwork package
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
## Bali network with nodes labels
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = vertex.names))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
Bali_network_Role.R
## Necessary libraries are loaded
rm(list = ls())
#load the rda file
library(ggnetwork)
library(ggplot2)
file = "/media/priyan/Files/GreyNodes/Assignment11/Bali.rda"
load(file = file)
## Network is created with ggnetwork package
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
## Bali network with nodes labels
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = role))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
Bali_network_name_code.docx
## Necessary libraries are loaded
rm(list = ls())
#load the rda file
library(ggnetwork)
library(ggplot2)
file = "/media/priyan/Files/GreyNodes/Assignment11/Bali.rda"
load(file = file)
## Network is created with ggnetwork package
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
## Bali network with nodes labels
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = vertex.names))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
Bali_network_names.Rmd
---
title: "Bali_network with node labels"
date: "5/18/2021"
output:
html_document:
toc: True
---
## Necessary libraries are loaded
```{r}
rm(list = ls())
#load the rda file
library(ggnetwork)
library(ggplot2)
load(file = "./Bali.rda")
```
## Network is created with ggnetwork package
```{r}
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
```
## Bali network with nodes labels
```{r}
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = vertex.names))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
```
Bali_network_names.html
Bali_network with node labels
5/18/2021
        Necessary libraries are loaded
        Network is created with ggnetwork package
        Bali network with nodes labels
Necessary libraries are loaded
rm(list = ls())
#load the rda file
library(ggnetwork)
## Loading required package: ggplot2
library(ggplot2)
load(file = "./Bali.rda")
Network is created with ggnetwork package
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
Bali network with nodes labels
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = vertex.names))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
Bali_network_role.Rmd
---
title: "Bali_network with node as role"
date: "5/18/2021"
outp
ut:
html_document:
toc: True
---
## Necessary libraries are loaded
```{r}
rm(list = ls())
#load the rda file
library(ggnetwork)
library(ggplot2)
load(file = "./Bali.rda")
```
## Network is created with ggnetwork package
```{r}
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
```
## Bali network with nodes labels
```{r}
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = role))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
```
Bali_network_role.html
Bali_network with node as role
5/18/2021
        Necessary libraries are loaded
        Network is created with ggnetwork package
        Bali network with nodes labels
Necessary libraries are loaded
rm(list = ls())
#load the rda file
library(ggnetwork)
## Loading required package: ggplot2
library(ggplot2)
load(file = "./Bali.rda")
Network is created with ggnetwork package
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
Bali network with nodes labels
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = role))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
Bali_network_role_code.docx
## Necessary libraries are loaded
rm(list = ls())
#load the rda file
library(ggnetwork)
library(ggplot2)
file = "/media/priyan/Files/GreyNodes/Assignment11/Bali.rda"
load(file = file)
## Network is created with ggnetwork package
#nt<-ggnetwork(Bali, layout = "fruchtermanreingold", cell.jitter = 0.75)
#nt<-ggnetwork(Bali, layout = "target", niter = 100)
nt<- ggnetwork(Bali)
## Bali network with nodes labels
ggplot(nt, aes(x = x, y = y, xend = xend, yend = yend)) +
geom_edges(color = "grey50") +
#geom_nodes(aes(color = role)) +
geom_label(aes(label = role))+
theme_blank()+
ggsave("Bali_role.svg",dpi = 500, width = 12, height = 9)
CO2_emmision2.R
### The necessary library are loaded
# load necessary packages
rm(list = ls())
library(sf)
library(sp)
library(dplyr)
library(rnaturalearth)
library(ggplot2)
library(broom)
library(ggiraph)
library(tidyr)
### Data preparation
# file variable should point to the file folder that contains co2_emissions.csv
file = "/media/priyan/Files/GreyNodes/Assignment11/co2_emissions.csv"
co2em <- read.csv(file, sep = ",", dec = ".", header = TRUE) # contains CO2 emissions in kt for the countries in the world for the last decades, downloaded from World Bank website, first few descriptive lines were removed and the file renamed```
### get World lat, lon
library(tidyverse)
# For spatial display we need borders of European countries, here I use spatial data provided by the R package rnaturalearth
world <- rnaturalearth::countries110 %>% st_as_sf()
world2 <- map_data("world")
countrycodes_wrld <- pull(world, iso_a3) # get the European country codes as template for filtering the emission data
co2em_wrld <- filter(co2em, countryCode %in% countrycodes_wrld)
### Join spatial and emission data
# join spatial and emission data and project crs
co2_wrld<- inner_join(world, co2em_wrld, by=c("iso_a3" = "countryCode"))
st_transform(co2_wrld, crs="+proj=laea +lon_0=18.984375")
class(co2_wrld)
### Data cleaning befor plotting
# plot CO2 emissions in Europe
# getlongitude and latitude for plotting with ggplot
eu.centers <- st_centroid(co2_wrld) # center points
eu.spdf <- methods::as(co2_wrld, 'Spatial') # convert to spatial class object
eu.spdf@data$id <- row.names(eu.spdf@data) # create id column with numbers
eu.tidy <- broom::tidy(eu.spdf) # now it is just a regular dataframe
eu.tidy <- dplyr::left_join(eu.tidy, eu.spdf@data, by='id')
### plotting with GGPLOT2
# mapping with ggplot
g1998 <- ggplot(eu.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=X2010,
tooltip=sprintf("%s
%s",iso_a3,round(X2010, digits = 0)))) + # display country code and emissions by hovering
coord_fixed(xlim=c(-200,200),ylim=c(-75,100)) + # set coordinate extent manually
hrbrthemes::theme_ipsum() +
scale_fill_distiller(palette = "Spectral", limits=c(0, 9000000))+ # set limits for comparison of different maps
# alternatively viridis::scale_fill_viridis(option="plasma", direction = -1)+
labs(title='CO2 emissions 2010', subtitle='in world',
caption='Source: World Bank', fill="CO2 [kt]")+
ggsave("CO2_emissions_2010.svg",dpi = 500, width = 7, height = 9)
ggiraph(code=print(g1998))
CO2_emmision2.Rmd
---
title: "Co2_emission"
date: "5/18/2021"
output:
html_document:
toc: True
---
### The necessary library are loaded
```{r}
# Install and load necessary packages
rm(list = ls())
library(sf)
library(sp)
library(dplyr)
library(rnaturalearth)
library(ggplot2)
library(broom)
library(ggiraph)
library(tidyr)
```
### Data preparation
Download co2_emissions.csv file to the same file folder as the R files.
```{r}
co2em <- read.csv("./co2_emissions.csv", sep = ",", dec = ".", header = TRUE) # contains CO2 emissions in kt for the countries in the world for the last decades, downloaded from World Bank website, first few descriptive lines were removed and the file renamed
```
### get World lat, lon
```{r}
library(tidyverse)
# For spatial display we need borders of European countries, here I use spatial data provided by the R package rnaturalearth
world <- rnaturalearth::countries110 %>% st_as_sf()
world2 <- map_data("world")
#europe <- world[world$region_un=="Europe"&world$name!='Russia',] %>% dplyr::select("iso_a3", "name")
```
```{r}
countrycodes_wrld <- pull(world, iso_a3) # get the European country codes as template for filtering the emission data
co2em_wrld <- filter(co2em, countryCode %in% countrycodes_wrld)
```
### Join spatial and emission data
```{r}
# join spatial and emission data and project crs
co2_wrld<- inner_join(world, co2em_wrld, by=c("iso_a3" = "countryCode"))
st_transform(co2_wrld, crs="+proj=laea +lon_0=18.984375")
class(co2_wrld)
```
### Data cleaning befor plotting
```{r}
# plot CO2 emissions in Europe
# getlongitude and latitude for plotting with ggplot
eu.centers <- st_centroid(co2_wrld) # center points
eu.spdf <- methods::as(co2_wrld, 'Spatial') # convert to spatial class object
eu.spdf@data$id <- row.names(eu.spdf@data) # create id column with numbers
eu.tidy <- broom::tidy(eu.spdf) # now it is just a regular dataframe
eu.tidy <- dplyr::left_join(eu.tidy, eu.spdf@data, by='id')
#eu.tidy <- eu.tidy[!(eu.tidy$long < -50 | eu.tidy$lat > 73), ] # remove islands belonging to Europe but situated far away in the world (for better map display)
```
### plotting with GGPLOT2
The plot is saved in file: CO2_emissions_2010.svg
```{r}
# mapping with ggplot
g1998 <- ggplot(eu.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=X2010,
tooltip=sprintf("%s
%s",iso_a3,round(X2010, digits = 0)))) + # display country code and emissions by hovering
coord_fixed(xlim=c(-200,200),ylim=c(-75,100)) + # set coordinate extent manually
hrbrthemes::theme_ipsum() +
scale_fill_distiller(palette = "Spectral", limits=c(0, 9000000))+ # set limits for comparison of different maps
# alternatively viridis::scale_fill_viridis(option="plasma", direction = -1)+
labs(title='CO2 emissions 2010', subtitle='in world',
caption='Source: World Bank', fill="CO2 [kt]")+
ggsave("CO2_emissions_2010.svg",dpi = 500, width = 7, height = 9)
ggiraph(code=print(g1998))
```
CO2_emmision2.html
Co2_emission
5/18/2021
        The necessary library are loaded
        Data preparation
        get World lat, lon
        Join spatial and emission data
        Data cleaning befor plotting
        plotting with GGPLOT2
The necessary library are loaded
# Install and load necessary packages
rm(list = ls())
library(sf)
## Linking to GEOS 3.5.1, GDAL 2.2.2, PROJ 4.9.2
library(sp)
library(dplyr)
##
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
##
## filter, lag
## The following objects are masked from 'package:base':
##
## intersect, setdiff, setequal, union
library(rnaturalearth)
library(ggplot2)
library(broom)
library(ggiraph)
library(tidyr)
Data preparation
Download co2_emissions.csv file to the same file folder as the R files.
co2em <- read.csv("./co2_emissions.csv", sep = ",", dec = ".", header = TRUE) # contains CO2 emissions in kt for the countries in the world for the last decades, downloaded from World Bank website, first few descriptive lines were removed and the file renamed
get World lat, lon
library(tidyverse)
## โ”€โ”€ Attaching packages โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyverse 1.3.0 โ”€โ”€
## โœ“ tibble 3.1.1 โœ“ stringr 1.4.0
## โœ“ readr 1.3.1 โœ“ forcats 0.5.1
## โœ“ purrr 0.3.4.9000
## โ”€โ”€ Conflicts โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ tidyverse_conflicts() โ”€โ”€
## x dplyr::filter() masks stats::filter()
## x dplyr::lag() masks stats::lag()
# For spatial display we need borders of European countries, here I use spatial data provided by the R package rnaturalearth
world <- rnaturalearth::countries110 %>% st_as_sf()
world2 <- map_data("world")
#europe <- world[world$region_un=="Europe"&world$name!='Russia',] %>% dplyr::select("iso_a3", "name")
countrycodes_wrld <- pull(world, iso_a3) # get the European country codes as template for filtering the emission data
co2em_wrld <- filter(co2em, countryCode %in% countrycodes_wrld)
Join spatial and emission data
# join spatial and emission data and project crs
co2_wrld<- inner_join(world, co2em_wrld, by=c("iso_a3" = "countryCode"))
st_transform(co2_wrld, crs="+proj=laea +lon_0=18.984375")
## Simple feature collection with 169 features and 116 fields
## Geometry type: MULTIPOLYGON
## Dimension: XY
## Bounding box: xmin: -10326570 ymin: -10821730 xmax: 11718260 ymax: 12476540
## CRS: +proj=laea +lon_0=18.984375
## First 10 features:
## scalerank featurecla labelrank sovereignt sov_a3 adm0_dif
## 1 1 Admin-0 country 3 Afghanistan AFG 0
## 2 1 Admin-0 country 3 Angola AGO 0
## 3 1 Admin-0 country 6 Albania ALB 0
## 4 1 Admin-0 country 4 United Arab Emirates ARE 0
## 5 1 Admin-0 country 2 Argentina ARG 0
## 6 1 Admin-0 country 6 Armenia ARM 0
## 7 1 Admin-0 country 2 Australia AU1 1
## 8 1 Admin-0 country 4 Austria AUT 0
## 9 1 Admin-0 country 5 Azerbaijan AZE 0
## 10 1 Admin-0 country 6 Burundi BDI 0
## level type admin adm0_a3 geou_dif
## 1 2 Sovereign country Afghanistan AFG 0
## 2 2 Sovereign country Angola AGO 0
## 3 2 Sovereign country Albania ALB 0
## 4 2 Sovereign country United Arab Emirates ARE 0
## 5 2 Sovereign country Argentina ARG 0
## 6 2 Sovereign country Armenia ARM 0
## 7 2 Country Australia AUS 0
## 8 2 Sovereign country Austria AUT 0
## 9 2 Sovereign country Azerbaijan AZE 0
## 10 2 Sovereign country Burundi BDI 0
## geounit gu_a3 su_dif subunit su_a3 brk_diff
## 1 Afghanistan AFG 0 Afghanistan AFG 0
## 2 Angola AGO 0 Angola AGO 0
## 3 Albania ALB 0 Albania ALB 0
## 4 United Arab Emirates ARE 0 United Arab Emirates ARE 0
## 5 Argentina ARG 0 Argentina ARG 0
## 6 Armenia ARM 0 Armenia ARM 0
## 7 Australia AUS 0 Australia AUS 0
## 8 Austria AUT 0 Austria AUT 0
## 9 Azerbaijan AZE 0 Azerbaijan AZE 0
## 10 Burundi BDI 0 Burundi BDI 0
## name name_long brk_a3 brk_name
## 1 Afghanistan Afghanistan AFG Afghanistan
## 2 Angola Angola AGO Angola
## 3 Albania Albania ALB Albania
## 4 United Arab Emirates United Arab Emirates ARE United Arab Emirates
## 5 Argentina Argentina ARG Argentina
## 6 Armenia Armenia ARM Armenia
## 7 Australia Australia AUS Australia
## 8 Austria Austria AUT Austria
## 9 Azerbaijan Azerbaijan AZE Azerbaijan
## 10 Burundi Burundi BDI Burundi
## brk_group abbrev postal formal_en formal_fr note_adm0
## 1 Afg. AF Islamic State of Afghanistan
## 2 Ang. AO People's Republic of Angola
## 3 Alb. AL Republic of Albania
## 4 U.A.E. AE United Arab Emirates
## 5 Arg. AR Argentine Republic
## 6 Arm. ARM Republic of Armenia
## 7 Auz. AU Commonwealth of Australia
## 8 Aust. A Republic of Austria
## 9 Aze. AZ Republic of Azerbaijan
## 10 Bur. BI Republic of Burundi
## note_brk name_sort name_alt mapcolor7 mapcolor8 mapcolor9
## 1 Afghanistan 5 6 8
## 2 Angola 3 2 6
## 3 Albania 1 4 1
## 4 United Arab Emirates 2 1 3
## 5 Argentina 3 1 3
## 6 Armenia 3 1 2
## 7 Australia 1 2 2
## 8 Austria 3 1 3
## 9 Azerbaijan 1 6 5
## 10 Burundi 2 2 5
## mapcolor13 pop_est gdp_md_est pop_year lastcensus gdp_year
## 1 7 28400000 22270 NA 1979 NA
## 2 1 12799293 110300 NA 1970 NA
## 3 6 3639453 21810 NA 2001 NA
## 4 3 4798491 184300 NA 2010 NA
## 5 13 40913584 573900 NA 2010 NA
## 6 10 2967004 18770 NA 2001 NA
## 7 7 21262641 800200 NA 2006 NA
## 8 4 8210281 329500 NA 2011 NA
## 9 8 8238672 77610 NA 2009 NA
## 10 8 8988091 3102 NA 2008 NA
## economy income_grp wikipedia fips_10 iso_a2
## 1 7. Least developed region 5. Low income NA AF
## 2 7. Least developed region 3. Upper middle income NA AO
## 3 6. Developing region 4. Lower middle income NA AL
## 4 6. Developing region 2. High income: nonOECD NA AE
## 5 5. Emerging region: G20 3. Upper middle income NA AR
## 6 6. Developing region 4. Lower middle income NA AM
## 7 2. Developed region: nonG7 1. High income: OECD NA AU
## 8 2. Developed region: nonG7 1. High income: OECD NA AT
## 9 6. Developing region 3. Upper middle income NA AZ
## 10 7. Least developed region 5. Low income NA BI
## iso_a3 iso_n3 un_a3 wb_a2 wb_a3 woe_id adm0_a3_is adm0_a3_us adm0_a3_un
## 1 AFG 004 004 AF AFG NA AFG AFG NA
## 2 AGO 024 024 AO AGO NA AGO AGO NA
## 3 ALB 008 008 AL ALB NA ALB ALB NA
## 4 ARE 784 784 AE ARE NA ARE ARE NA
## 5 ARG 032 032 AR ARG NA ARG ARG NA
## 6 ARM 051 051 AM ARM NA ARM ARM NA
## 7 AUS 036 036 AU AUS NA AUS AUS NA
## 8 AUT 040 040 AT AUT NA AUT AUT NA
## 9 AZE 031 031 AZ AZE NA AZE AZE NA
## 10 BDI 108 108 BI BDI NA BDI BDI NA
## adm0_a3_wb continent region_un subregion
## 1 NA Asia Asia Southern Asia
## 2 NA Africa Africa Middle Africa
## 3 NA Europe Europe Southern Europe
## 4 NA Asia Asia Western Asia
## 5 NA South America Americas South America
## 6 NA Asia Asia Western Asia
## 7 NA Oceania Oceania Australia and New Zealand
## 8 NA Europe Europe Western Europe
## 9 NA Asia Asia Western Asia
## 10 NA Africa Africa Eastern Africa
## region_wb name_len long_len abbrev_len tiny homepart
## 1 South Asia 11 11 4 NA 1
## 2 Sub-Saharan Africa 6 6 4 NA 1
## 3 Europe & Central Asia 7 7 4 NA 1
## 4 Middle East & North Africa 20 20 6 NA 1
## 5 Latin America & Caribbean 9 9 4 NA 1
## 6 Europe & Central Asia 7 7 4 NA 1
## 7 East Asia & Pacific 9 9 4 NA 1
## 8 Europe & Central Asia 7 7 5 NA 1
## 9 Europe & Central Asia 10 10 4 NA 1
## 10 Sub-Saharan Africa 7 7 4 NA 1
## countryName indicatorName indicatorCode X1961 X1962
## 1 Afghanistan CO2 emissions (kt) EN.ATM.CO2E.KT 491.378 689.396
## 2 Angola CO2 emissions (kt) EN.ATM.CO2E.KT 454.708 1180.774
## 3 Albania CO2 emissions (kt) EN.ATM.CO2E.KT 2280.874 2464.224
## 4 United Arab Emirates CO2 emissions (kt) EN.ATM.CO2E.KT 11.001 18.335
## 5 Argentina CO2 emissions (kt) EN.ATM.CO2E.KT 51180.319 53695.881
## 6 Armenia CO2 emissions (kt) EN.ATM.CO2E.KT NA NA
## 7 Australia CO2 emissions (kt) EN.ATM.CO2E.KT 90589.568 94912.961
## 8 Austria CO2 emissions (kt) EN.ATM.CO2E.KT 31862.563 33905.082
## 9 Azerbaijan CO2 emissions (kt) EN.ATM.CO2E.KT NA NA
## 10 Burundi CO2 emissions (kt) EN.ATM.CO2E.KT NA 44.004
## X1963 X1964 X1965 X1966 X1967 X1968 X1969
## 1 707.731 839.743 1008.425 1092.766 1283.450 1224.778 942.419
## 2 1151.438 1224.778 1188.108 1554.808 993.757 1672.152 2786.920
## 3 2082.856 2016.850 2174.531 2552.232 2680.577 3072.946 3245.295
## 4 22.002 18.335 22.002 25.669 916.750 1243.113 20524.199
## 5 50083.886 55727.399 58866.351 63138.406 65543.958 69082.613 77329.696
## 6 NA NA NA NA NA NA NA
## 7 101029.517 108979.573 120966.996 120332.605 129265.417 134622.904 142257.598
## 8 36992.696 38943.540 38188.138 39258.902 39966.633 42350.183 44693.396
## 9 NA NA NA NA NA NA NA
## 10 47.671 47.671 36.670 47.671 47.671 55.005 73.340
## X1970 X1971 X1972 X1973 X1974 X1975 X1976
## 1 1672.152 1895.839 1532.806 1639.149 1917.841 2126.860 1987.514
## 2 3582.659 3410.310 4506.743 4880.777 4873.443 4415.068 3285.632
## 3 3744.007 4352.729 5643.513 5291.481 4345.395 4594.751 4950.450
## 4 15247.386 21184.259 23454.132 30630.451 31327.181 31070.491 39651.271
## 5 82734.854 88939.418 90156.862 94065.884 95569.354 94931.296 99786.404
## 6 NA NA NA NA NA NA NA
## 7 147618.752 152774.554 157486.649 170992.210 172356.334 175883.988 174244.839
## 8 50692.608 52155.741 56193.108 60164.469 57469.224 54392.611 58415.310
## 9 NA NA NA NA NA NA NA
## 10 62.339 73.340 73.340 73.340 91.675 77.007 88.008
## X1977 X1978 X1979 X1980 X1981 X1982 X1983
## 1 2390.884 2159.863 2240.537 1760.160 1983.847 2101.191 2522.896
## 2 3534.988 5412.492 5504.167 5346.486 5280.480 4649.756 5115.465
## 3 5720.520 6494.257 7587.023 5170.470 7341.334 7308.331 7631.027
## 4 38785.859 44814.407 36607.661 36904.688 36857.017 36871.685 35342.546
## 5 100791.162 102639.330 110703.063 108737.551 102041.609 103424.068 105213.564
## 6 NA NA NA NA NA NA NA
## 7 187787.070 202015.030 205069.641 220746.066 230360.940 234119.615 225003.453
## 8 56218.777 57483.892 61594.599 52306.088 56130.769 53868.230 51983.392
## 9 NA NA NA NA NA NA NA
## 10 99.009 102.676 110.010 146.680 157.681 157.681 205.352
## X1984 X1985 X1986 X1987 X1988 X1989 X1990
## 1 2830.924 3509.319 3142.619 3124.284 2867.594 2775.919 2676.910
## 2 5009.122 4701.094 4660.757 5815.862 5130.133 5009.122 4429.736
## 3 7825.378 7880.383 8056.399 7444.010 7326.666 8984.150 7488.014
## 4 46394.884 49926.205 47234.627 47693.002 48367.730 54487.953 52009.061
## 5 106522.683 100596.811 104212.473 114942.115 121473.042 117090.977 112613.570
## 6 NA NA NA NA NA NA NA
## 7 236594.840 241229.928 239964.813 256106.947 261145.405 277771.583 287331.452
## 8 54550.292 54700.639 54080.916 57744.249 53340.182 54117.586 60725.520
## 9 NA NA NA NA NA NA NA
## 10 220.020 231.021 242.022 267.691 256.690 300.694 304.361
## X1991 X1992 X1993 X1994 X1995 X1996 X1997
## 1 2493.560 1426.463 1375.125 1320.120 1268.782 1199.109 1114.768
## 2 4367.397 4418.735 5801.194 3890.687 11012.001 10491.287 7381.671
## 3 3971.361 2387.217 2343.213 1928.842 2086.523 2016.850 1543.807
## 4 57010.849 58136.618 65980.331 73130.981 70641.088 41059.399 41646.119
## 5 117021.304 121447.373 118609.115 123350.546 122547.473 129217.746 134677.909
## 6 NA 4052.035 2896.930 2966.603 3490.984 2607.237 3278.298
## 7 281530.258 294456.433 302116.796 303957.630 307433.946 329259.930 333623.660
## 8 65602.630 60542.170 60281.813 61103.221 61730.278 63024.729 63901.142
## 9 NA 57681.910 49365.154 42672.879 33479.710 31514.198 29809.043
## 10 344.698 319.029 337.364 341.031 333.697 330.030 315.362
## X1998 X1999 X2000 X2001 X2002 X2003 X2004
## 1 1056.096 832.409 781.071 645.392 359.366 583.053 733.400
## 2 7308.331 9156.499 9541.534 9732.218 12665.818 9064.824 18793.375
## 3 1752.826 2984.938 3021.608 3223.293 3751.341 4294.057 4165.712
## 4 81495.408 78374.791 112562.232 101414.552 84704.033 106841.712 113240.627
## 5 137673.848 145488.225 141076.824 132631.723 123266.205 133126.768 156170.196
## 6 3406.643 3058.278 3465.315 3542.322 3043.610 3428.645 3644.998
## 7 346912.868 325523.257 329604.628 324859.530 341001.664 346476.495 348757.369
## 8 65565.960 63798.466 63695.790 65716.307 67179.440 72316.907 71865.866
## 9 31675.546 28576.931 29508.349 28771.282 29614.692 30615.783 32089.917
## 10 300.694 293.360 300.694 216.353 220.020 165.015 198.018
## X2005 X2006 X2007 X2008 X2009 X2010
## 1 1015.759 1338.455 1991.181 3927.357 6523.593 8236.082
## 2 19156.408 22266.024 25151.953 26596.751 27836.197 30417.765
## 3 4253.720 3865.018 4055.702 4129.042 3879.686 4283.056
## 4 116148.558 123874.927 139404.672 158935.114 162602.114 167596.568
## 5 160951.964 174237.505 180416.400 190056.943 179638.996 180511.742
## 6 4360.063 4382.065 5067.794 5555.505 4352.729 4220.717
## 7 362684.635 371214.077 377235.291 387634.903 395093.581 373080.580
## 8 74238.415 71565.172 69141.285 68268.539 62261.993 66897.081
## 9 34337.788 39167.227 41426.099 45701.821 42628.875 45731.157
## 10 154.014 187.017 190.684 249.356 179.683 308.028
## geometry
## 1 MULTIPOLYGON (((3897019 413...
## 2 MULTIPOLYGON (((-294686.1 -...
## 3 MULTIPOLYGON (((142770.1 45...
## 4 MULTIPOLYGON (((3333836 276...
## 5 MULTIPOLYGON (((-5003690 -7...
## 6 MULTIPOLYGON (((2183104 454...
## 7 MULTIPOLYGON (((7426184 -79...
## 8 MULTIPOLYGON (((-163453.8 5...
## 9 MULTIPOLYGON (((2343041 441...
## 10 MULTIPOLYGON (((1148567 -49...
class(co2_wrld)
## [1] "sf" "data.frame"
Data cleaning befor plotting
# plot CO2 emissions in Europe
# getlongitude and latitude for plotting with ggplot
eu.centers <- st_centroid(co2_wrld) # center points
## Warning in st_centroid.sf(co2_wrld): st_centroid assumes attributes are constant
## over geometries of x
## Warning in st_centroid.sfc(st_geometry(x), of_largest_polygon =
## of_largest_polygon): st_centroid does not give correct centroids for longitude/
## latitude data
eu.spdf <- methods::as(co2_wrld, 'Spatial') # convert to spatial class object
eu.spdf@data$id <- row.names(eu.spdf@data) # create id column with numbers
eu.tidy <- broom::tidy(eu.spdf) # now it is just a regular dataframe
## Regions defined for each Polygons
eu.tidy <- dplyr::left_join(eu.tidy, eu.spdf@data, by='id')
#eu.tidy <- eu.tidy[!(eu.tidy$long < -50 | eu.tidy$lat > 73), ] # remove islands belonging to Europe but situated far away in the world (for better map display)
plotting with GGPLOT2
The plot is saved in file: CO2_emissions_2010.svg
# mapping with ggplot
g1998 <- ggplot(eu.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=X2010,
tooltip=sprintf("%s
%s",iso_a3,round(X2010, digits = 0)))) + # display country code and emissions by hovering
coord_fixed(xlim=c(-200,200),ylim=c(-75,100)) + # set coordinate extent manually
hrbrthemes::theme_ipsum() +
scale_fill_distiller(palette = "Spectral", limits=c(0, 9000000))+ # set limits for comparison of different maps
# alternatively viridis::scale_fill_viridis(option="plasma", direction = -1)+
labs(title='CO2 emissions 2010', subtitle='in world',
caption='Source: World Bank', fill="CO2 [kt]")+
ggsave("CO2_emissions_2010.svg",dpi = 500, width = 7, height = 9)
ggiraph(code=print(g1998))
CO2_emmision2_code.docx
### The necessary library are loaded
# load necessary packages
rm(list = ls())
library(sf)
library(sp)
library(dplyr)
library(rnaturalearth)
library(ggplot2)
library(broom)
library(ggiraph)
library(tidyr)
### Data preparation
# file variable should point to the file folder that contains co2_emissions.csv
file = "/media/priyan/Files/GreyNodes/Assignment11/co2_emissions.csv"
co2em <- read.csv(file, sep = ",", dec = ".", header = TRUE) # contains CO2 emissions in kt for the countries in the world for the last decades, downloaded from World Bank website, first few descriptive lines were removed and the file renamed```
### get World lat, lon
library(tidyverse)
# For spatial display we need borders of European countries, here I use spatial data provided by the R package rnaturalearth
world <- rnaturalearth::countries110 %>% st_as_sf()
world2 <- map_data("world")
countrycodes_wrld <- pull(world, iso_a3) # get the European country codes as template for filtering the emission data
co2em_wrld <- filter(co2em, countryCode %in% countrycodes_wrld)
### Join spatial and emission data
# join spatial and emission data and project crs
co2_wrld<- inner_join(world, co2em_wrld, by=c("iso_a3" = "countryCode"))
st_transform(co2_wrld, crs="+proj=laea +lon_0=18.984375")
class(co2_wrld)
### Data cleaning befor plotting
# plot CO2 emissions in Europe
# getlongitude and latitude for plotting with ggplot
eu.centers <- st_centroid(co2_wrld) # center points
eu.spdf <- methods::as(co2_wrld, 'Spatial') # convert to spatial class object
eu.spdf@data$id <- row.names(eu.spdf@data) # create id column with numbers
eu.tidy <- broom::tidy(eu.spdf) # now it is just a regular dataframe
eu.tidy <- dplyr::left_join(eu.tidy, eu.spdf@data, by='id')
### plotting with GGPLOT2
# mapping with ggplot
g1998 <- ggplot(eu.tidy) +
geom_polygon_interactive(
color='black',
aes(long, lat, group=group, fill=X2010,
tooltip=sprintf("%s
%s",iso_a3,round(X2010, digits = 0)))) + # display country code and emissions by hovering
coord_fixed(xlim=c(-200,200),ylim=c(-75,100)) + # set coordinate extent manually
hrbrthemes::theme_ipsum() +
scale_fill_distiller(palette = "Spectral", limits=c(0, 9000000))+ # set limits for comparison of different maps
# alternatively viridis::scale_fill_viridis(option="plasma", direction = -1)+
labs(title='CO2 emissions 2010', subtitle='in world',
caption='Source: World Bank', fill="CO2 [kt]")+
ggsave("CO2_emissions_2010.svg",dpi = 500, width = 7, height = 9)
ggiraph(code=print(g1998))
co2_emissions.csv
countryName,countryCode,indicatorName,indicatorCode,1961,1962,1963,1964,1965,1966,1967,1968,1969,1970,1971,1972,1973,1974,1975,1976,1977,1978,1979,1980,1981,1982,1983,1984,1985,1986,1987,1988,1989,1990,1991,1992,1993,1994,1995,1996,1997,1998,1999,2000,2001,2002,2003,2004,2005,2006,2007,2008,2009,2010
Afghanistan,AFG,CO2 emissions (kt),EN.ATM.CO2E.KT,491.378,689.396,707.731,839.743,1008.425,1092.766,1283.45,1224.778,942.419,1672.152,1895.839,1532.806,1639.149,1917.841,2126.86,1987.514,2390.884,2159.863,2240.537,1760.16,1983.847,2101.191,2522.896,2830.924,3509.319,3142.619,3124.284,2867.594,2775.919,2676.91,2493.56,1426.463,1375.125,1320.12,1268.782,1199.109,1114.768,1056.096,832.409,781.071,645.392,359.366,583.053,733.4,1015.759,1338.455,1991.181,3927.357,6523.593,8236.082
Albania,ALB,CO2 emissions (kt),EN.ATM.CO2E.KT,2280.874,2464.224,2082.856,2016.85,2174.531,2552.232,2680.577,3072.946,3245.295,3744.007,4352.729,5643.513,5291.481,4345.395,4594.751,4950.45,5720.52,6494.257,7587.023,5170.47,7341.334,7308.331,7631.027,7825.378,7880.383,8056.399,7444.01,7326.666,8984.15,7488.014,3971.361,2387.217,2343.213,1928.842,2086.523,2016.85,1543.807,1752.826,2984.938,3021.608,3223.293,3751.341,4294.057,4165.712,4253.72,3865.018,4055.702,4129.042,3879.686,4283.056
Algeria,DZA,CO2 emissions (kt),EN.ATM.CO2E.KT,6065.218,5669.182,5427.16,5650.847,6596.933,8430.433,8441.434,9061.157,11276.025,15075.037,18668.697,28349.577,38312.816,31906.567,32031.245,39170.894,41891.808,62529.684,45621.147,66519.38,46438.888,39269.903,52625.117,71103.13,72786.283,76277.267,84120.98,83948.631,80046.943,78895.505,81458.738,82261.811,82474.497,86350.516,94692.941,96566.778,88195.017,107080.067,92118.707,87930.993,84293.329,90853.592,92533.078,89493.135,107127.738,103963.117,112338.545,114619.419,124586.325,123475.224
American Samoa,ASM,CO2 emissions (kt),EN.ATM.CO2E.KT,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,
Andorra,AND,CO2 emissions (kt),EN.ATM.CO2E.KT,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,,407.037,425.372,458.375,484.044,513.38,524.381,524.381,531.715,535.382,564.718,575.719,546.383,539.049,539.049,517.047,517.047
Angola,AGO,CO2 emissions (kt),EN.ATM.CO2E.KT,454.708,1180.774,1151.438,1224.778,1188.108,1554.808,993.757,1672.152,2786.92,3582.659,3410.31,4506.743,4880.777,4873.443,4415.068,3285.632,3534.988,5412.492,5504.167,5346.486,5280.48,4649.756,5115.465,5009.122,4701.094,4660.757,5815.862,5130.133,5009.122,4429.736,4367.397,4418.735,5801.194,3890.687,11012.001,10491.287,7381.671,7308.331,9156.499,9541.534,9732.218,12665.818,9064.824,18793.375,19156.408,22266.024,25151.953,26596.751,27836.197,30417.765
Antigua and Barbuda,ATG,CO2 emissions...
SOLUTION.PDF

Answer To This Question Is Available To Download

Related Questions & Answers

More Questions ยป

Submit New Assignment

Copy and Paste Your Assignment Here