From c3d100ece39b742093d23c1bc05b5ee8a27bf019 Mon Sep 17 00:00:00 2001 From: lewy Date: Wed, 14 Jun 2023 10:58:06 +0200 Subject: [PATCH] Final changes --- projekt_1_housing_new.r | 25 +++++++---------- projekt_2_housing_new.R | 2 ++ webapp.R | 62 ++++++++++++++++++++++++----------------- 3 files changed, 48 insertions(+), 41 deletions(-) diff --git a/projekt_1_housing_new.r b/projekt_1_housing_new.r index 7ed4be4..d547a44 100644 --- a/projekt_1_housing_new.r +++ b/projekt_1_housing_new.r @@ -2,9 +2,8 @@ library(dplyr) # install.packages("ggplot2") library(ggplot2) library(plotly) -countries = c( 'PL', 'DE', 'CZ', 'NL', 'RO', 'XD') - -countries <- unique(map_df$geo) +countries = c( 'PL', 'DE', 'CZ', 'NL', 'RO') +countries = unique(SHP_28$geo) df = read.csv(".//data//prc_hicp_aind_page_linear.csv") df[,c("geo", "TIME_PERIOD", "OBS_VALUE")] @@ -51,7 +50,7 @@ ggplot(year_country_gdp, aes(x = TIME_PERIOD, y = OBS_VALUE, color = geo, label plot <- ggplot(year_country_gdp, aes(x = TIME_PERIOD, y = OBS_VALUE, color = geo, text = paste("Kraj: ", geo, "
", "Rok: ", TIME_PERIOD, "
", - "Warto?? wska?nika: ", OBS_VALUE))) + + "Wartość wskaźnika: ", OBS_VALUE))) + geom_line(aes(group = geo)) + geom_point() + labs(x = "Rok", y = 'PKB per capita w PPP [PPS_EU27_2020=100]') + @@ -86,15 +85,15 @@ ggplot(house_price_index, aes(x = TIME_PERIOD, y = OBS_VALUE, color = geo, label slice(n() - 1), aes(label = geo, hjust = -1, size = 4))+ scale_x_continuous(breaks=seq(2010,2024,2)) - labs(x = "Rok", y = 'Indeks Cen nieruchomo?ci [cena z 2015 roku = 100]') + labs(x = "Rok", y = 'Indeks Cen nieruchomości [cena z 2015 roku = 100]') plot <- ggplot(house_price_index, aes(x = TIME_PERIOD, y = OBS_VALUE, color = geo, text = paste("Kraj: ", geo, "
", "Rok: ", TIME_PERIOD, "
", - "Warto?? wska?nika: ", OBS_VALUE))) + + "Wartość wskaźnika: ", OBS_VALUE))) + geom_line() + geom_point() + scale_x_continuous(breaks = seq(2010, 2024, 2)) + - labs(x = "Rok", y = 'Indeks Cen nieruchomo?ci [cena z 2015 roku = 100]') + labs(x = "Rok", y = 'Indeks Cen nieruchomości [cena z 2015 roku = 100]') plotly_plot <- ggplotly(plot, tooltip = "text") for (i in 1:length(plotly_plot$x$data)) { if (plotly_plot$x$data[[i]]$type == "scatter") { @@ -133,7 +132,7 @@ ggplot(hicp_index, aes(x = TIME_PERIOD, y = OBS_VALUE, color = geo, label = geo) plot <- ggplot(hicp_index, aes(x = TIME_PERIOD, y = OBS_VALUE, color = geo, text = paste("Kraj: ", geo, "
", "Rok: ", TIME_PERIOD, "
", - "Warto?? wska?nika: ", OBS_VALUE))) + + "Wartość wskaźnika: ", OBS_VALUE))) + geom_line() + geom_point() + labs(x = "Rok", y = 'Indeks inflacji konsumenckiej HICP [2015 = 100]') + @@ -174,12 +173,8 @@ merged_df %>% group_by(geo) %>% str() str(merged_df) -unfiltered_df <- merged_df merged_df <- filter(merged_df, geo %in% countries) - - - ggplot(merged_df, aes(x = TIME_PERIOD, y = house_prices_wo_hicp, color = geo)) + geom_line(linetype="dotted", size=1) + geom_point(aes(x=TIME_PERIOD, y=house_prices_wo_hicp)) + @@ -190,12 +185,12 @@ ggplot(merged_df, aes(x = TIME_PERIOD, y = house_prices_wo_hicp, color = geo)) + stat_function(fun=function(x) 100*(1.04)^(x-2015), aes(colour = "4% Compounding")) + stat_function(fun=function(x) 100*(1.07)^(x-2015), aes(colour = "7% Compounding")) + scale_x_continuous(breaks=seq(2010,2024,2)) + - labs(x = "Year", y = 'Indeks cen nieruchomo?ci zdyskontowany o warto?? inflacji [2015 = 100]') + labs(x = "Year", y = 'Indeks cen nieruchomości zdyskontowany o wartość inflacji [2015 = 100]') final_plot <- ggplot(merged_df, aes(x = TIME_PERIOD, y = house_prices_wo_hicp, color = geo, text = paste("Kraj: ", geo, "
", "Rok: ", TIME_PERIOD, "
", - "Cena nieruchomo?ci: ", house_prices_wo_hicp))) + + "Cena nieruchomości: ", house_prices_wo_hicp))) + geom_line(aes(group = geo), linetype = "dotted", size = 1) + geom_point() + geom_text(data = merged_df %>% group_by(geo) %>% slice(n()), @@ -203,7 +198,7 @@ final_plot <- ggplot(merged_df, aes(x = TIME_PERIOD, y = house_prices_wo_hicp, c stat_function(fun = function(x) 100*(1.04)^(x-2015), aes(colour = "4% Compounding"), inherit.aes = FALSE) + stat_function(fun = function(x) 100*(1.07)^(x-2015), aes(colour = "7% Compounding"), inherit.aes = FALSE) + scale_x_continuous(breaks = seq(2010, 2024, 2)) + - labs(x = "Rok", y = "Indeks cen nieruchomoĹ›ci zdyskontowany o wartość inflacji [2015 = 100]") + labs(x = "Rok", y = "Indeks cen nieruchomości zdyskontowany o wartość inflacji [2015 = 100]") final_plot diff --git a/projekt_2_housing_new.R b/projekt_2_housing_new.R index e03be51..b88a35d 100644 --- a/projekt_2_housing_new.R +++ b/projekt_2_housing_new.R @@ -40,6 +40,8 @@ SHP_28 %>% # Join datasets mapdata_new <- left_join(SHP_28, map_df, by="geo", relationship = "many-to-many") +merged_df <- left_join(SHP_28, merged_df, by="geo", relationship = "many-to-many") +merged_df # Delete Greece (null values) mapdata_new <- mapdata_new[-9,] diff --git a/webapp.R b/webapp.R index 2139ac7..3168f24 100644 --- a/webapp.R +++ b/webapp.R @@ -12,12 +12,18 @@ ui <- fluidPage( sliderInput("range", "Compound interest:",min = 0, max = 10, value = c(4,8)),textOutput("Compound interest slider"), # Checkboxes + # tags$head(tags$style(HTML(".checkbox {margin-left:15px}"))), checkboxGroupInput("countries", "Chosen countries:", - choiceNames = unique(map_df$geo), - choiceValues = unique(map_df$geo), - selected = c("PL", "DE", "CZ") + choiceNames = map_df$region, + choiceValues = map_df$geo, + selected = c("PL", "DE", "CZ"), + inline = TRUE, + width = "75%" ), + + + width=3 ), @@ -26,7 +32,7 @@ ui <- fluidPage( h1("Real house prices index"), tabsetPanel( - tabPanel("Plot", plotOutput("final_plot")), + tabPanel("Plot", plotlyOutput("final_plot")), tabPanel("Map", leafletOutput("mymap")), tabPanel("Table", dataTableOutput('table')) ), @@ -43,38 +49,42 @@ ui <- fluidPage( # Backend server <- function(input, output, session) { + + output$table <- renderDataTable({ + merged_df[, !names(merged_df) %in% c("OBS_VALUE.x", "OBS_VALUE.y", "geometry")] + }, options = list(pageLength = 10)) - # UĹĽywanie wektora indeksu do indeksowania innych danych - output$table <- renderDataTable(merged_df[,!names(merged_df) %in% c("OBS_VALUE.x", "OBS_VALUE.y")]) # Plot module - output$final_plot <- renderPlot({ - - # final_plot <- - ggplot(filter(merged_df, geo %in% input$countries), aes(x = TIME_PERIOD, y = house_prices_wo_hicp, color = geo, - text = paste("Kraj: ", geo, "
", "Rok: ", TIME_PERIOD, "
", - "Cena nieruchomości: ", house_prices_wo_hicp))) + + output$final_plot <- renderPlotly({ + final_plot <- ggplot(filter(merged_df, geo %in% input$countries), + aes(x = TIME_PERIOD, y = house_prices_wo_hicp, color = geo, + text = paste("Kraj: ", geo, "
", "Rok: ", TIME_PERIOD, "
", + "Cena nieruchomości: ", house_prices_wo_hicp))) + geom_line(aes(group = geo), linetype = "dotted", size = 1) + geom_point() + - geom_text(data = merged_df %>% group_by(geo) %>% slice(n()), + geom_text(data = filter(merged_df, geo %in% input$countries) %>% group_by(geo) %>% slice(n()), aes(label = "", hjust = -0.2, size = 4)) + - stat_function(fun = function(x) 100*(1+input$range[1]/100)^(x-2015), aes(colour = "4% Compounding"), inherit.aes = FALSE) + - stat_function(fun = function(x) 100*(1+input$range[2]/100)^(x-2015), aes(colour = "7% Compounding"), inherit.aes = FALSE) + + stat_function(fun = function(x) 100*(1+input$range[1]/100)^(x-2015), aes(colour = paste0(as.character(input$range[1]), "% Compounding")), inherit.aes = FALSE) + + stat_function(fun = function(x) 100*(1+input$range[2]/100)^(x-2015), aes(colour = paste0(as.character(input$range[2]), "% Compounding")), inherit.aes = FALSE) + scale_x_continuous(breaks = seq(2010, 2024, 2)) + - # labs(x = "Rok", y = "Indeks cen nieruchomości zdyskontowany o wartość inflacji [2015 = 100]") + - theme( axis.title = element_blank() ) + labs(x = "Rok", y = "Indeks cen nieruchomości zdyskontowany o wartość inflacji [2015 = 100]", + color = "Countries") + + theme(axis.title = element_blank()) - # ggplotly(final_plot, tooltip = "text") + plotly_plot <- ggplotly(final_plot, tooltip = "text") - #for (i in 1:length(plotly_plot$x$data)) { - # if (plotly_plot$x$data[[i]]$name == "4% Compounding" || plotly_plot$x$data[[i]]$name == "7% Compounding") { - # plotly_plot$x$data[[i]]$hoverinfo <- "name+y" - # } - #} + for (i in 1:length(plotly_plot$x$data)) { + if (plotly_plot$x$data[[i]]$name == paste0(as.character(input$range[1]), "% Compounding") || plotly_plot$x$data[[i]]$name == paste0(as.character(input$range[1]), "% Compounding")) { + plotly_plot$x$data[[i]]$hoverinfo <- "name+y" + } + } - #plotly_plot - - }) + plotly_plot %>% layout(showlegend = TRUE, legend = list(title = list(text = "Countries"))) + }) + + + # Map module