#Number of apartments and price in Belgrade



library(readxl)

df <- read_xlsx("C:/Users/vjovanovic/Desktop/R Udemy/Vezbe sa javnim podacima/stanovi.xlsx")

str(df)
## Classes 'tbl_df', 'tbl' and 'data.frame':    3270 obs. of  8 variables:
##  $ idindikator: chr  "050502IND01" "050502IND01" "050502IND01" "050502IND01" ...
##  $ mes        : chr  "00" "00" "00" "00" ...
##  $ god        : chr  "2011" "2011" "2018" "2018" ...
##  $ IDVrPod    : chr  "1" "2" "1" "2" ...
##  $ nVrPod     : chr  "Broj završenih stanova - ukupno" "Površina završenih stanova - ukupno, m2" "Broj završenih stanova - ukupno" "Površina završenih stanova - ukupno, m2" ...
##  $ IDTer      : chr  "70017" "70017" "70017" "70017" ...
##  $ nTer       : chr  "Aleksandrovac" "Aleksandrovac" "Aleksandrovac" "Aleksandrovac" ...
##  $ vrednost   : chr  "7" "883" "16" "1671" ...
#Now we must change character to factor or numeric variable

df$nTer <- as.factor(df$nTer)
df$vrednost <- as.numeric(df$vrednost)
df$god <- as.factor(df$god)
df$IDVrPod <- as.factor(df$IDVrPod)

head(df)
## # A tibble: 6 x 8
##   idindikator mes   god   IDVrPod nVrPod             IDTer nTer    vrednost
##   <chr>       <chr> <fct> <fct>   <chr>              <chr> <fct>      <dbl>
## 1 050502IND01 00    2011  1       Broj završenih st~ 70017 Aleksa~        7
## 2 050502IND01 00    2011  2       Površina završeni~ 70017 Aleksa~      883
## 3 050502IND01 00    2018  1       Broj završenih st~ 70017 Aleksa~       16
## 4 050502IND01 00    2018  2       Površina završeni~ 70017 Aleksa~     1671
## 5 050502IND01 00    2017  1       Broj završenih st~ 70017 Aleksa~        6
## 6 050502IND01 00    2017  2       Površina završeni~ 70017 Aleksa~      982
#Now we will select only variables for the Republic of Serbia, not for particular cities.

library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
df1 <- filter(df, nTer=="Beogradski region")
head(df1)
## # A tibble: 6 x 8
##   idindikator mes   god   IDVrPod nVrPod            IDTer nTer     vrednost
##   <chr>       <chr> <fct> <fct>   <chr>             <chr> <fct>       <dbl>
## 1 050502IND01 00    2011  1       Broj završenih s~ RS11  Beograd~     6416
## 2 050502IND01 00    2011  2       Površina završen~ RS11  Beograd~   379681
## 3 050502IND01 00    2012  1       Broj završenih s~ RS11  Beograd~     8096
## 4 050502IND01 00    2012  2       Površina završen~ RS11  Beograd~   486739
## 5 050502IND01 00    2013  1       Broj završenih s~ RS11  Beograd~     7596
## 6 050502IND01 00    2013  2       Površina završen~ RS11  Beograd~   484348
#We must cast the variable vrednost


library(reshape)
## 
## Attaching package: 'reshape'
## The following object is masked from 'package:dplyr':
## 
##     rename
recasteddf <- cast(df1, idindikator + god~nVrPod,sum)
## Using vrednost as value column.  Use the value argument to cast to override this choice
head(recasteddf)
##   idindikator  god Broj završenih stanova - ukupno
## 1 050502IND01 2011                            6416
## 2 050502IND01 2012                            8096
## 3 050502IND01 2013                            7596
## 4 050502IND01 2014                            4755
## 5 050502IND01 2015                            4014
## 6 050502IND01 2016                            3167
##   Površina završenih stanova - ukupno, m2
## 1                                  379681
## 2                                  486739
## 3                                  484348
## 4                                  301457
## 5                                  245786
## 6                                  223856
str(recasteddf)
## List of 4
##  $ idindikator                            : chr [1:8] "050502IND01" "050502IND01" "050502IND01" "050502IND01" ...
##  $ god                                    : Factor w/ 8 levels "2011","2012",..: 1 2 3 4 5 6 7 8
##  $ Broj završenih stanova - ukupno        : num [1:8] 6416 8096 7596 4755 4014 ...
##  $ Površina završenih stanova - ukupno, m2: num [1:8] 379681 486739 484348 301457 245786 ...
##  - attr(*, "row.names")= int [1:8] 1 2 3 4 5 6 7 8
##  - attr(*, "idvars")= chr [1:2] "idindikator" "god"
##  - attr(*, "rdimnames")=List of 2
##   ..$ :'data.frame': 8 obs. of  2 variables:
##   .. ..$ idindikator: chr [1:8] "050502IND01" "050502IND01" "050502IND01" "050502IND01" ...
##   .. ..$ god        : Factor w/ 8 levels "2011","2012",..: 1 2 3 4 5 6 7 8
##   ..$ :'data.frame': 2 obs. of  1 variable:
##   .. ..$ nVrPod: chr [1:2] "Broj završenih stanova - ukupno" "Površina završenih stanova - ukupno, m2"
#Now we will plot a graph

library(ggplot2)

p <- ggplot(recasteddf, aes(god, `Broj završenih stanova - ukupno`)) + geom_point(alpha=0.5, size=3) + theme_bw()
p

g<-p+geom_col(alpha=0.5, size=4, fill="blue")  + xlab("Year") + ylab("Number of new appartments in Belgrade") + ggtitle("The Growth of appartments in last 30 years in Belgrade") 

print(g)

j <- ggplot(recasteddf, aes(god, `Površina završenih stanova - ukupno, m²`)) + geom_point(alpha=0.5, size=3) + theme_bw()

j

k<-j+geom_col(alpha=0.5, size=4, fill="blue") + xlab("Year") + ylab("Area of new appartments in Belgrade") + ggtitle("The Area of new appartments in last 30 years in Belgrade") 
k