### Recent Question/Assignment

MATH2270/MATH2237 Assignment 3
Interactive Storytelling
Student Details
https://theconversation.com/developing-countries-can-prosper-without-increasingemissions-84044 Visualisation URL https://iramix.shinyapps.io/CombinedCO2WorldApp/
Code
library(shiny)
)
)
2)
)
)
)
# new labels ggplot
(-CAIT_Country_GHG_Emissions.xlsx-, sheet = 3, header = TRUE)
#Change the name of country column to COUNTRY
\$Country
Total.CO2.Emissions.Excluding.Land.Use.Change.and.Forestry..MtCO2. -
\$Total.CO2.Emissions.Excluding.Land.Use.Change.and.Forestry..Mt
library(plotly library(rgeos) library(maptools library(ggmap) library(broom) library(dplyr) library(ggplot library(maps) library(mapdata library(gdata) library( library(ggthemes library(scales library(ggrepel)

co2\$COUNTRY -co2

#Change the name to co2 co2\$ as.numeric(co2
CO2.)
co2 -
Total.CO2.Emissions.Excluding.Land.Use.Change.and.Forestry..MtCO2.
(co2)
co2[,-1] co2[,-2]
('https://raw.githubusercontent.com/plotly/datasets/master/2014_world _gdp_with_codes.csv')
# Drop GDP
(map) map[,-2] left_join(map, co2)
\$COUNTRY -wdata\$Country
\$CO2 - as.numeric(wdata\$CO2) left_join(map, wdata)
# Define UI for application that draws a histogram fluidPage(
# Application title titlePanel(-Find out who Polutes the most, when and possible reasons-),
# Sidebar with a slider input for number of bins
sidebarPanel(
# tags\$style(type=-text/css-, -select { max-width: 140px; }-),
# tags\$style(type=-text/css-, -.span4 { max-width: 190px; }-), # tags\$style(type=-text/css-, -.well { max-width: 180px; }-)
# ),
h3(-Select Year-),
#### enter the year for the map sliderInput(-Year-, -Year-, min = 1960, max = 2013,
co2\$ co2\$

names

co2 co2 -

names map -

final -

wdata wdata wdata

final2 -

ui -

value = 2000, sep = --, animate = animationOptions(interval =
1300, loop = FALSE)),
helpText(-Select Year to see CO2 Emissions across the world-), br(), br(), br(), br(), br(), br(), br(), br(), br(),
h3(-Select Country-), # Select Country name here selectizeInput(-name-, label = -Country Name(s) of Interest-, choices = unique(wdata\$Country), multiple = T, options = list(maxItems = 4, placeholder = 'Select at least one Country'),
selected = -Australia-),
helpText(-Choose Maximum 4 countries to compare-),
br(),
h3(-Select Measure-),
selectInput(-measure-, -Enter unique Measure to see trend-, c(-Population-,-CO2-, -GDP_PPP-,-GDP_USD-,-EnergyUse-), selected = -Population-),
helpText(-Choose a metric to plot against years in the timeline-),
br(),
h3(-About this App-), helpText(-If developing countries are the higher poluters, let's find out how do they compare to Developed countries when they were developing. Select Year, Countries and Measures to see which countries polute the most in different 60 years of Data. Click the Play button to see the changes. Enter different countries to compare against eachother and select other metrics to see if they have also a relationship with Emissions.-), helpText( a(-See the full article-,
href=-https://theconversation.com/developing-countries-can-prosper-withoutincreasing-emissions-84044-)
)
), #width=4,
# Show a plot of the generated distribution mainPanel( #width = 8,
h3(textOutput(-selected_year-)),
plotlyOutput(-mapPlot-, height = 500),
tabsetPanel(type = -tabs-,
tabPanel(-See Timeline Comparision-, plotlyOutput(-trendPlot-, height = 500)),
#tabPanel(-Relative-, verbatimTextOutput(-summary-)), tabPanel(-CO2 vs GDP-, plotlyOutput(-scatterPlot-, height = 500))
#plotlyOutput(-trendPlot-)
# fluidRow(
# splitLayout(cellWidths = c(-50%-, -50%-), plotlyOutput(-trendPlot-), plotlyOutput(-scatterPlot-))
)
))
############################################################
# Define server logic server - function(input, output, session) {
output\$selected_year - renderText({ paste(-Year-, input\$Year)
})
## First get the Map output\$mapPlot - renderPlotly({
# generate dataframe based on input\$Year from ui.R FinalByYear - final2 % % filter(Year==input\$Year)
# light grey boundaries
l - list(color = toRGB(-grey-), width = 0.5)
# specify map projection/options g - list( showframe = FALSE, showcoastlines = FALSE,
projection = list(type = 'Mercator')
)
z = ~CO2, color = ~CO2, colors = 'Blues',
text = ~COUNTRY, locations = ~CODE, marker = list(line = l)
) % %
colorbar(title = 'CO2') % %
href=-http://datasets.wri.org/dataset/cait-unfccc-annex-i-ghg-emissions-
-CAIT_Country_GHG_Emissions.csv-)
!=-World-)
!=-European Union (28)-)
!=-European Union (15)-)
CO2)
({
)
\$Country == input\$name, ] \$measure]
{ input\$name[-length(input\$name)], collapse = ', j_names_comma, -, and -,
name, collapse = ' and ')
\$measure)
TitleMeasure, - for -, j_names, sep=--)
finalbyCountry) + y = finalmeasure, color = Country)) +
#geom_line(aes(x=Year, y=mean(finalmeasure, color=-black-))) +
TitleMeasure, title = graph_title) +
, l = 70, c = 150) +
-horizontal-, legend.position = -bottom-) + comma) +
layout(
title = 'Global CO2 br Source: a
data- CAIT Climate Data /a ', geo = g
)
})
#### Line graph

wdata - wdata % % filter(Country wdata - wdata % % filter(Country wdata - wdata % % filter(Country
wdata\$CO2 - as.numeric(wdata\$

output\$trendPlot - renderPlotly if (length(input\$name) 1) {
print(-Please select at least one country-
} else {
finalbyCountry - wdata[wdata finalmeasure - finalbyCountry[,input

# Graph title if (length(input\$name) 2)
j_names_comma - paste(
')
j_names - paste0( input\$name[length(input\$name)])
} else {
j_names - paste(input\$
}
TitleMeasure - paste(input graph_title - paste(

ggideal_point - ggplot( geom_line(aes(x = Year,
labs(x = -Year-, y = scale_colour_hue(-Country- ggthemes::theme_few() + theme(legend.direction = scale_y_continuous(labels=
geom_vline(xintercept = input\$Year, linetype=-dotted-, color =
',
-black-, size=0.5)
# Convert ggplot object to plotly gg - plotly_build(ggideal_point)
# Use Plotly syntax to further edit the plot:
gg\$layout\$annotations - NULL # Remove the existing annotations (the legend label) gg\$layout\$annotations - list()
gg\$layout\$showlegend - FALSE # remove the legend
gg\$layout\$margin\$r - 170 # increase the size of the right margin to accommodate more room for the annotation labels gg
}
})
##### ScatterPlot
output\$scatterPlot - renderPlotly({
if (length(input\$name) 1) {
print(-Please select at least one country-) } else {
#wdatabyCountry - wdata[wdata\$Country == input\$name, ] #wdataByYear - wdata % % filter(Year==input\$Year) wdataByYear - wdata[wdata\$Year == input\$Year, ]
##wdataByYearbyCountry - wdataByYear[wdataByYear\$Country == input\$name, ]
wdataByYearbyCountry - wdataByYear[wdataByYear\$Country %in% input\$name, ]
# Graph title
if (length(input\$name) 2) {
j_names_comma - paste(input\$name[-length(input\$name)], collapse =
')
j_names - paste0(j_names_comma, -, and -, input\$name[length(input\$name)])
} else {
j_names - paste(input\$name, collapse = ' and ')
}
inputyear - paste(input\$year)
graph_title2 - paste(-GDP vs CO2 for -, j_names, sep=--)
+
CO2,
GDP_USD, y=CO2,
)) +
t - ggplot(wdataByYear, aes(label=Country, label2=Year)) scale_size_continuous() +
geom_point(data=wdataByYear, mapping=aes(x=GDP_USD, y= size=Population), colour=-grey50-) +
geom_point(data=wdataByYearbyCountry, mapping=aes(x= size=Population, colour=Country)) +
# geom_label_repel(aes(wdataByYearbyCountry\$Country), size=3) + labs(title = graph_title2, x = -GDP (Usd)-, y = -CO2-) + scale_x_continuous(labels=comma, limits = c(0, 12800000 scale_colour_hue(-Country-, l = 70, c = 150) + ggthemes::theme_few()
tt - plotly_build(t)
tt - ggplotly(tt,tooltip = c(-Country-)) tt
# Use Plotly syntax to further edit the plot:
tt\$layout\$annotations - NULL # Remove the existing annotations (the legend label) tt\$layout\$annotations - list()
tt\$layout\$showlegend - FALSE # remove the legend
tt\$layout\$margin\$r - 170 # increase the size of the right margin to accommodate more room for the annotation labels tt
}
})
}
# Run the application shinyApp(ui = ui, server = server, options=list(
#width=-100%-,
height=-100%-) #options = list(height=1080)
)