Zaktualizuj 'projekt.R'

This commit is contained in:
Patryk Łukasiewicz 2023-05-05 00:01:05 +02:00
parent 5d44e8de2b
commit e8cd163012

102
projekt.R
View File

@ -4,11 +4,28 @@ library(dplyr) # Data manipulate
library(shinythemes) library(shinythemes)
library(plotly) library(plotly)
library(sf)
library(rnaturalearth)
library(ggspatial)
library(ggrepel)
options(scipen=999)
CO_data <- read.csv("./data.csv", header= TRUE) CO_data <- read.csv("./data.csv", header= TRUE)
col_names = colnames(CO_data) CO_data2 <- CO_data[,-c(1,2,3)]
col_names = colnames(CO_data2)
countries <- unique(CO_data['country']) countries <- unique(CO_data['country'])
years <- unique(sort(CO_data$year))
world <- ne_countries(scale = 'medium', returnclass = 'sf')
country_list <- unique(sort(world$name))
CO_data_filtered <- subset(CO_data, country %in% country_list)
only_co2_and_year <- CO_data[,c('year', 'country', 'co2')]
ui <- navbarPage( ui <- navbarPage(
titlePanel(title=div(img(src="https://siw.amu.edu.pl/__data/assets/file/0004/162751/logo_wersja-podstawowa_granat_1.jpg", width = 50, height = 50), 'explore CO2 data')), titlePanel(title=div(img(src="https://siw.amu.edu.pl/__data/assets/file/0004/162751/logo_wersja-podstawowa_granat_1.jpg", width = 50, height = 50), 'explore CO2 data')),
@ -36,8 +53,38 @@ ui <- navbarPage(
'GDP', 'GDP',
plotlyOutput('gdp') plotlyOutput('gdp')
), ),
tabPanel("Map: CO2 by year",
sidebarLayout(
sidebarPanel(
selectInput('year',
'Select year',
selected = '2011',
choices = years
)
),
mainPanel(
plotlyOutput('map'),
),
)
),
tabPanel("Map: statistics in year 2011",
sidebarLayout(
sidebarPanel(
selectInput('category2',
'Select category',
selected = 'population',
choices = col_names
)
),
mainPanel(
plotlyOutput('map2'),
),
)
),
tabPanel( tabPanel(
'TOP Production', 'Biggest CO2 Production',
fluidRow( fluidRow(
column(6,plotlyOutput(outputId="the_most_1")), column(6,plotlyOutput(outputId="the_most_1")),
column(6,plotlyOutput(outputId="the_most_2")), column(6,plotlyOutput(outputId="the_most_2")),
@ -45,7 +92,7 @@ ui <- navbarPage(
) )
), ),
tabPanel( tabPanel(
'Smallest production', 'Smallest CO2 production',
fluidRow( fluidRow(
column(6,plotlyOutput(outputId="the_least_1")), column(6,plotlyOutput(outputId="the_least_1")),
column(6,plotlyOutput(outputId="the_least_2")), column(6,plotlyOutput(outputId="the_least_2")),
@ -77,83 +124,100 @@ server <- function(input, output, session) {
CO_data %>% CO_data %>%
filter(country == input$country) %>% filter(country == input$country) %>%
ggplot(aes(x = year, y = get(input$category))) + ggplot(aes(x = year, y = get(input$category))) +
ylab(input$category) +
geom_line() geom_line()
}) })
output$gdp <- renderPlotly({ output$gdp <- renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year == 2011) %>% filter(year == 2011) %>%
ggplot(aes(x = gdp, y = co2, label = country)) + ggplot(aes(x = gdp, y = co2, label = country)) +
geom_line() + geom_line() +
geom_point() + geom_point() +
ylim(0,10000) +
ggtitle('Placement of countries by CO2 and GDP production') ggtitle('Placement of countries by CO2 and GDP production')
}) })
output$map = renderPlotly({
countries_data <- filter(only_co2_and_year, year==input$year)
data <- merge(world, countries_data, by.y="country", by.x="name")
ggplot(data = data) +
geom_sf(aes(fill = co2, label = name)) +
scale_fill_viridis_c(option = "plasma", trans = "sqrt") # colorblind-friendly palette
})
output$map2 = renderPlotly({
countries_data <- filter(CO_data, year==2011)
data2 <- merge(world, countries_data, by.y="country", by.x="name")
ggplot(data = data2) +
geom_sf(aes(fill = get(input$category2), label = input$category2)) +
labs(title=input$category2) +
scale_fill_discrete(labels = input$category2) +
scale_fill_viridis_c(option = "plasma", trans = "sqrt") # colorblind-friendly palette
})
output$the_most_1 = renderPlotly({ output$the_most_1 = renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year==2011) %>% filter(year==2011) %>%
slice_max(n=7, order_by = co2_per_gdp) %>% slice_max(n=7, order_by = co2_per_gdp) %>%
ggplot(aes(x=country, y=co2_per_gdp, fill=country)) + ggplot(aes(x=country, y=co2_per_gdp, fill=country)) +
xlab('Country') + xlab('Country') +
ylab('CO2 per GDP') + ylab('CO2 per GDP') +
ggtitle('The TOP production per GDP') + ggtitle('the biggest CO2 production per GDP') +
theme(axis.text.x = element_blank()) + theme(axis.text.x = element_blank()) +
geom_bar(stat='identity') geom_bar(stat='identity')
}) })
output$the_most_2 = renderPlotly({ output$the_most_2 = renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year==2011) %>% filter(year==2011) %>%
slice_max(n=7, order_by = co2_per_capita) %>% slice_max(n=7, order_by = co2_per_capita) %>%
ggplot(aes(x=country, y=co2_per_capita, fill=country), custom) + ggplot(aes(x=country, y=co2_per_capita, fill=country), custom) +
xlab('Country') + xlab('Country') +
ylab('CO2 per capita') + ylab('CO2 per capita') +
ggtitle('The TOP production per capita') + ggtitle('the biggest CO2 production per capita') +
theme(axis.text.x = element_blank()) + theme(axis.text.x = element_blank()) +
geom_bar(stat='identity') geom_bar(stat='identity')
}) })
output$the_most_3 = renderPlotly({ output$the_most_3 = renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year==2011) %>% filter(year==2011) %>%
slice_max(n=7, order_by = co2_per_capita) %>% slice_max(n=7, order_by = co2) %>%
ggplot(aes(x=country, y=co2_per_capita, fill=country)) + ggplot(aes(x=country, y=co2, fill=country)) +
xlab('Country') + xlab('Country') +
ylab('CO2 overall') + ylab('CO2 overall') +
ggtitle('The TOP production overall') + ggtitle('the biggest CO2 production overall') +
theme(axis.text.x = element_blank()) + theme(axis.text.x = element_blank()) +
geom_bar(stat='identity') geom_bar(stat='identity')
}) })
output$the_least_1 = renderPlotly({ output$the_least_1 = renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year==2011) %>% filter(year==2011) %>%
slice_min(n=7, order_by = co2_per_gdp) %>% slice_min(n=7, order_by = co2_per_gdp) %>%
ggplot(aes(x=country, y=co2_per_gdp, fill=country)) + ggplot(aes(x=country, y=co2_per_gdp, fill=country)) +
xlab('Country') + xlab('Country') +
ylab('CO2 per GDP') + ylab('CO2 per GDP') +
ggtitle('The smalest production per GDP') + ggtitle('the smallest CO2 production per GDP') +
theme(axis.text.x = element_blank()) + theme(axis.text.x = element_blank()) +
geom_bar(stat='identity') geom_bar(stat='identity')
}) })
output$the_least_2 = renderPlotly({ output$the_least_2 = renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year==2011) %>% filter(year==2011) %>%
slice_min(n=7, order_by = co2_per_capita) %>% slice_min(n=7, order_by = co2_per_capita) %>%
ggplot(aes(x=country, y=co2_per_capita, fill=country)) + ggplot(aes(x=country, y=co2_per_capita, fill=country)) +
xlab('Country') + xlab('Country') +
ylab('CO2 per capita') + ylab('CO2 per capita') +
ggtitle('The smalest production per capita') + ggtitle('the smallest CO2 production per capita') +
theme(axis.text.x = element_blank()) + theme(axis.text.x = element_blank()) +
geom_bar(stat='identity') geom_bar(stat='identity')
}) })
output$the_least_3 = renderPlotly({ output$the_least_3 = renderPlotly({
CO_data %>% CO_data_filtered %>%
filter(year==2011) %>% filter(year==2011) %>%
slice_min(n=7, order_by = co2) %>% slice_min(n=7, order_by = co2) %>%
ggplot(aes(x=country, y=co2, fill=country)) + ggplot(aes(x=country, y=co2, fill=country)) +
xlab('Country') + xlab('Country') +
ylab('CO2 overall') + ylab('CO2 overall') +
ggtitle('The smalest production overall') + ggtitle('the smallest CO2 production overall') +
theme(axis.text.x = element_blank()) + theme(axis.text.x = element_blank()) +
geom_bar(stat='identity') geom_bar(stat='identity')
}) })
} }
shinyApp(ui = ui, server = server) shinyApp(ui = ui, server = server)