---
title: "Untitled"
output:
flexdashboard::flex_dashboard:
orientation: columns
social: menu
source_code: embed
---
```{r setup, include=FALSE}
library(flexdashboard)
library(ggplot2)
library(dplyr)
library(plotly)
library(tidyr)
library(readr)
```
```{r}
setwd("E:/Masters course content/Semester 2/Data visualization/Assignment 3/CLimate change")
data1 <- read.csv("climate-change.csv")
data1 <- na.omit(data1)
```
tab
================================================================================
Column {data-width=500}
--------------------------------------------------------------------------------
### Chart A
```{r}
library("lubridate")
data1 <- data1 %>%
mutate(Date = as.Date(Date, format = "%d/%m/%Y")) %>%
filter(Date >= as.Date("1990-01-01") & Date <= as.Date("2023-12-31")) %>%
group_by(Year = lubridate::year(Date)) %>%
summarise(Mean_Temperature = mean(monthly_sea_surface_temperature_anomaly))
```
```{r}
# Create an interactive chart
temperature_chart <- ggplot(data1, aes(x = Year, y = Mean_Temperature)) +
geom_line() +
labs(title = "Global Warming: Monthly Temperature Anomaly", x = "Month", y = "Temperature Anomaly")
# Convert the ggplot chart to a plotly chart for interactivity
temperature_chart <- ggplotly(temperature_chart)
```
```{r}
temperature_chart
```
Column {data-width=500}
-----------------------------------------------------------------------
### Chart B
```{r include=FALSE}
data2 <- read.csv("co-emissions-per-capita.csv")
data2 <- na.omit(data2)
# Convert the 'Year' column to numeric (if it's not already)
data2$Year <- as.numeric(data2$Year)
# Filter the data for years between 1990 and 2023
filtered_data <- data2[data2$Year >= 1990 & data2$Year <= 2023, ]
names(filtered_data)[3] <- "Co2Emission"
```
```{r include=FALSE}
# Calculate the mean CO2 emissions for each country over the years 1990-2021
mean_emissions_by_country <- filtered_data %>%
group_by(Entity) %>%
summarise(Mean_Emissions = mean(Co2Emission))
top_10_emitters <- mean_emissions_by_country %>%
arrange(desc(Mean_Emissions)) %>%
slice_head(n = 10)
# View the result
top_10_emitters
```
```{r}
# Create an interactive line chart for the top 10 countries with a y-axis range of 0-100
line_chart <- filtered_data %>%
plot_ly(
x = ~Year,
y = ~Co2Emission,
color = ~Entity,
type = "scatter",
mode = "lines",
line = list(width = 2)
) %>%
layout(
title = "Top 10 Countries - CO2 Emissions per Capita Over Time (1990-2021)",
xaxis = list(title = "Year"),
yaxis = list(title = "CO2 Emissions (per capita)", range = c(0, 100)),
legend = list(orientation = "h")
)
# Display the line chart
line_chart
```
```{r}
# List of countries to include
included_countries <- c(
"Qatar", "Kuwait", "Curacao", "United Arab Emirates", "Bahrain",
"Trinidad and Tobago", "Luxembourg", "United States", "Brunei", "Australia"
)
filtered_data <- filtered_data %>%
filter(Entity == included_countries)
filtered_data
```
```{r}
# Filter the data to include only the specified countries
filtered_data <- filtered_data %>%
filter(Entity %in% included_countries)
# Create an interactive line chart for the specified countries
line_chart <- filtered_data %>%
plot_ly(
x = ~Year,
y = ~Co2Emission,
color = ~Entity,
type = "scatter",
mode = "lines",
line = list(width = 2)
) %>%
layout(
title = "Top 10 Countries - CO2 Emissions per Capita Over Time (1990-2021)",
xaxis = list(title = "Year"),
yaxis = list(title = "CO2 Emissions (per capita)"),
legend = list(orientation = "h")
)
# Display the line chart
line_chart
```
tab
================================================================================
Column {data-width=500}
-----------------------------------------------------------------------
### Chart C
```{r include=FALSE}
data3 <- read.csv("global.csv", encoding = "UTF-8")
data3 <- na.omit(data3)
```
```{r}
bar_chart <- data3 %>%
plot_ly(
x = ~Year,
y = ~`Gas.production...TWh`,
type = 'bar',
name = 'GasProduction_TWh'
) %>%
add_trace(
x = ~Year,
y = ~`Coal.production...TWh`,
type = 'bar',
name = 'CoalProduction_TWh'
) %>%
add_trace(
x = ~Year,
y = ~`Primary.energy.consumption...TWh`,
type = 'bar',
name = 'PrimaryEnergyConsumption_TWh'
) %>%
layout(
title = "Changes in Energy Production and Consumption Over Time",
xaxis = list(title = "Year"),
yaxis = list(title = "Production/Consumption - TWh"),
barmode = 'group' # 'group' displays bars side by side, 'stack' would stack them
)
# Display the interactive bar chart
bar_chart
```