author: “Farzaneh” date: “02/24/2023” output: html_document —

### Assignment #2  LA-558   Farzaneh-Faramarzi
### Farzaneh Faramarzi
### Professor Seeger
library(ggplot2)
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
library(maptools)
## Loading required package: sp
## Checking rgeos availability: FALSE
## Please note that 'maptools' will be retired during 2023,
## plan transition at your earliest convenience;
## some functionality will be moved to 'sp'.
##      Note: when rgeos is not available, polygon geometry     computations in maptools depend on gpclib,
##      which has a restricted licence. It is disabled by default;
##      to enable gpclib, type gpclibPermit()
library(tidyverse)
## ── Attaching core tidyverse packages ──────────────────────── tidyverse 2.0.0 ──
## ✔ forcats   1.0.0     ✔ stringr   1.5.0
## ✔ lubridate 1.9.2     ✔ tibble    3.1.8
## ✔ purrr     1.0.1     ✔ tidyr     1.3.0
## ✔ readr     2.1.4
## ── Conflicts ────────────────────────────────────────── tidyverse_conflicts() ──
## ✖ dplyr::filter() masks stats::filter()
## ✖ dplyr::lag()    masks stats::lag()
## ℹ Use the ]8;;http://conflicted.r-lib.org/conflicted package]8;; to force all conflicts to become errors
library(maps)
## 
## Attaching package: 'maps'
## 
## The following object is masked from 'package:purrr':
## 
##     map
library(mapdata)
# Map 1

#install.packages(c("gpclib","maptools"))
library("gpclib")
## General Polygon Clipper Library for R (version 1.5-6)
##  Type 'class ? gpc.poly' for help
setwd("C:/Spring 2023/CRP 558/LA-558/GisData/London")
London_shp = readShapePoly("LDNSuicides.shp")
## Warning: shapelib support is provided by GDAL through the sf and terra packages
## among others
London_csv<-read.csv("london.csv")
plot(London_shp)

View(London_csv)
max(London_csv$E)
## [1] 179.8
count(London_csv)
##    n
## 1 32
London_shp$ID1=row.names(London_shp)
London_csv$ID2=row.names(London_csv)
shp.csv=merge(London_shp, London_csv, by.x = "ID1", by.y = "ID2")
s = spplot(shp.csv, zcol="logSMR",col=NA,main="London Map - Boroughs of the London")
s

# Map 2
setwd("C:/Spring 2023/CRP 558/LA-558/GisData")
Ames <- read.csv("Ameshousing.csv")
View(Ames)
head(Ames)
##   SalePrice Bedrooms LotArea LivingArea GarageArea Neighborhood
## 1    270000        4   11792       2283        632      Gilbert
## 2    377500        3   14892       1746        758      Gilbert
## 3    337500        3   12456       1718        786      NridgHt
## 4    462000        4   14257       2772        754      NridgHt
## 5    489900        2   14803       2084       1220      NridgHt
## 6    555000        2   15431       2402        672      NridgHt
summarize(Ames)
## data frame with 0 columns and 1 row
##number of houses
dim(Ames)
## [1] 168   6
#or
nrow(Ames)
## [1] 168
##names of variables
str(Ames) 
## 'data.frame':    168 obs. of  6 variables:
##  $ SalePrice   : int  270000 377500 337500 462000 489900 555000 256300 335000 451950 610000 ...
##  $ Bedrooms    : int  4 3 3 4 2 2 3 3 3 2 ...
##  $ LotArea     : int  11792 14892 12456 14257 14803 15431 14230 11308 13478 13693 ...
##  $ LivingArea  : int  2283 1746 1718 2772 2084 2402 1600 2184 2296 2674 ...
##  $ GarageArea  : int  632 758 786 754 1220 672 890 836 842 762 ...
##  $ Neighborhood: chr  "Gilbert" "Gilbert" "NridgHt" "NridgHt" ...
#or 
colnames(Ames)
## [1] "SalePrice"    "Bedrooms"     "LotArea"      "LivingArea"   "GarageArea"  
## [6] "Neighborhood"
#or
names(Ames)
## [1] "SalePrice"    "Bedrooms"     "LotArea"      "LivingArea"   "GarageArea"  
## [6] "Neighborhood"
## some statistics
meanarea <- mean(Ames$LivingArea)
meanarea
## [1] 1700.107
stdarea <- sd(Ames$LivingArea)
stdarea
## [1] 419.0155
medarea <- median(Ames$LivingArea)
medarea
## [1] 1629.5
mean(Ames$SalePrice)
## [1] 249338
sd(Ames$SalePrice/sqrt(length(Ames$SalePrice)))
## [1] 6809.082
## Mapping
library(ggplot2)
f = ggplot(Ames, aes(x = LivingArea,  y = SalePrice))+
  geom_point(col="green", size = 4)+
  geom_smooth(col="red", width=0.5)+
  labs(x = "Living Area", y = "House Sale Price", title = "House Sale Price vs Living Area" )
## Warning in geom_smooth(col = "red", width = 0.5): Ignoring unknown parameters:
## `width`
f
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'

f + coord_flip()
## `geom_smooth()` using method = 'loess' and formula = 'y ~ x'