tab

Column

Chart A

Column

Chart B

                 Entity Year Co2Emission
1             Australia 1999    18.26981
2             Australia 2009    18.79262
3             Australia 2019    16.41968
4               Bahrain 1992    19.61040
5               Bahrain 2002    21.20451
6               Bahrain 2012    22.13889
7                Brunei 1994    16.06412
8                Brunei 2004    13.51872
9                Brunei 2014    21.19420
10              Curacao 1996    28.92202
11              Curacao 2006    35.19682
12              Curacao 2016    33.96805
13               Kuwait 1993    29.70968
14               Kuwait 2003    28.37200
15               Kuwait 2013    22.95978
16           Luxembourg 1996    22.26904
17           Luxembourg 2006    25.23350
18           Luxembourg 2016    15.58097
19                Qatar 1998    54.59815
20                Qatar 2008    44.49021
21                Qatar 2018    34.42022
22  Trinidad and Tobago 1991    13.29551
23  Trinidad and Tobago 2001    19.84898
24  Trinidad and Tobago 2011    32.87282
25  Trinidad and Tobago 2021    23.67748
26 United Arab Emirates 1997    26.61962
27 United Arab Emirates 2007    22.69235
28 United Arab Emirates 2017    23.61590
29        United States 1999    20.81202
30        United States 2009    17.77233
31        United States 2019    15.73089

tab

Column

Chart C

---
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
```