This is a demonstration by AAአድማሱ who learnt this from these free resources: R Graphics cookbook Url:https://r-graphics.org/ Maps C.R.A.N. Url:https://cran.r-project.org/web/packages/maps/
This is a demonstration by AAA, who is by no means a professional in R. This is a practice demonstration for means of purposes to communicate data in a meaningful innovative and creative medium of communication. This advanced technology allows for multi-sector collaboration on projects in real time.
As one software programmer describes R, “it blurs the line between user and developer”. R is also open-source and runs as an object-oriented functional language programming using vectors as data elements. Utilizes memory space for low cost high powered thorough output of complex data analysis and computations including for mapping and graphing.This is just one demo. There are many more ones of which called Quarto and Rmarkdown HTML which are interaction if uploaded as a shiny app to <shinyapps.io> or as a quartormarkdown on <github.com/git> or on one’s own server.
Furthermore one use of AI such as bing is for R script development and a short example is displayed here below.
Enjoy
U.S.A.; STATE; COUNTIES long and lat data for data analysis and vizualization. > Case Study with GeoMapping of long and lat data
##a Setup/initialization
Install/load the library R packages necessary for this demonstration:
#library(ggplot2) library(dplyr) library(Hmisc) library(MASS)
#library(data.table) library(maps) library(mapproj)
#check file location of directory with getwd()
#By AAA Batt
print(Sys.Date())
## [1] "2023-06-07"
#
#
##USA map data
usa_mp<-c()
usa_mp<- map_data("state")
May omit nas but check dim() of objs after in a table of is.na() for indexing for example.
usa_mp1<-c()
usa_mp1<- map_data("state")
vc_l<-dim(usa_mp)
###use na.omit()
usa_mp<- usa_mp1 %>% na.omit()
###check dim()
dim(usa_mp)
## [1] 3672 6
vc_l %>% as.matrix()
## [,1]
## [1,] 15537
## [2,] 6
as.matrix(dim(usa_mp))
## [,1]
## [1,] 3672
## [2,] 6
vc_l %>% str()
## int [1:2] 15537 6
usa_mp %>% dim() %>% str()
## int [1:2] 3672 6
usa_mp %>% ggplot( aes(x=long,y=lat,group=group))+geom_path()+geom_polygon(aes(fill="blue"))
usa_mp1 %>% ggplot( aes(x=long,y=lat,group=group))+geom_path()+geom_polygon(aes(fill="blue"))
tibble(usa_mp)
## # A tibble: 3,672 × 6
## long lat group order region subregion
## <dbl> <dbl> <dbl> <int> <chr> <chr>
## 1 -70.5 41.4 20 5926 massachusetts martha's vineyard
## 2 -70.5 41.4 20 5927 massachusetts martha's vineyard
## 3 -70.5 41.4 20 5928 massachusetts martha's vineyard
## 4 -70.5 41.4 20 5929 massachusetts martha's vineyard
## 5 -70.5 41.4 20 5930 massachusetts martha's vineyard
## 6 -70.6 41.3 20 5931 massachusetts martha's vineyard
## 7 -70.6 41.3 20 5932 massachusetts martha's vineyard
## 8 -70.7 41.3 20 5933 massachusetts martha's vineyard
## 9 -70.7 41.3 20 5934 massachusetts martha's vineyard
## 10 -70.7 41.3 20 5935 massachusetts martha's vineyard
## # ℹ 3,662 more rows
usa_mp %>% head(n =20, unique(subregion)) %>% tibble()
## # A tibble: 20 × 6
## long lat group order region subregion
## <dbl> <dbl> <dbl> <int> <chr> <chr>
## 1 -70.5 41.4 20 5926 massachusetts martha's vineyard
## 2 -70.5 41.4 20 5927 massachusetts martha's vineyard
## 3 -70.5 41.4 20 5928 massachusetts martha's vineyard
## 4 -70.5 41.4 20 5929 massachusetts martha's vineyard
## 5 -70.5 41.4 20 5930 massachusetts martha's vineyard
## 6 -70.6 41.3 20 5931 massachusetts martha's vineyard
## 7 -70.6 41.3 20 5932 massachusetts martha's vineyard
## 8 -70.7 41.3 20 5933 massachusetts martha's vineyard
## 9 -70.7 41.3 20 5934 massachusetts martha's vineyard
## 10 -70.7 41.3 20 5935 massachusetts martha's vineyard
## 11 -70.7 41.3 20 5936 massachusetts martha's vineyard
## 12 -70.8 41.3 20 5937 massachusetts martha's vineyard
## 13 -70.8 41.3 20 5938 massachusetts martha's vineyard
## 14 -70.8 41.3 20 5939 massachusetts martha's vineyard
## 15 -70.8 41.3 20 5940 massachusetts martha's vineyard
## 16 -70.8 41.3 20 5941 massachusetts martha's vineyard
## 17 -70.8 41.4 20 5942 massachusetts martha's vineyard
## 18 -70.8 41.4 20 5943 massachusetts martha's vineyard
## 19 -70.8 41.3 20 5944 massachusetts martha's vineyard
## 20 -70.7 41.4 20 5945 massachusetts martha's vineyard
#
##USA_mp<-map_data("state")
dim(usa_mp)
## [1] 3672 6
length(usa_mp$subregion)
## [1] 3672
length(usa_mp$region)
## [1] 3672
##unique func()
usaigrph<-c(NA)
usaigrph<- usa_mp %>% unique() %>% tibble()
#ggplot(usaigrph,aes(x=usa_mp$long,y=usa_mp$lat),group="subregion")+geom_polygon()
#plot(c(usaigrph))
#WA state map data
waf_mp<-map_data("state",region="washington")
waf_mp %>% head(10) %>% tibble()
## # A tibble: 10 × 6
## long lat group order region subregion
## <dbl> <dbl> <dbl> <int> <chr> <chr>
## 1 -123. 48.6 1 1 washington san juan island
## 2 -123. 48.6 1 2 washington san juan island
## 3 -123. 48.6 1 3 washington san juan island
## 4 -123. 48.6 1 4 washington san juan island
## 5 -123. 48.6 1 5 washington san juan island
## 6 -123. 48.5 1 6 washington san juan island
## 7 -123. 48.5 1 7 washington san juan island
## 8 -123. 48.5 1 8 washington san juan island
## 9 -123. 48.5 1 9 washington san juan island
## 10 -123. 48.5 1 10 washington san juan island
#usa_mp %>% filter(lat>100) %>% ggplot(aes(x=long,y=lat,group=group))+geom_path()
Import the data from the NOAA storm database which has the file name: <StormEvents_2014_20230330.csv>
#storm data#
df<-c()
df<-read.csv(file="StormEvents_2014_20230330.csv",header =T,stringsAsFactors = F )
df_t<-c()
df_t<-df
df_t<-tibble(df)
sm_5<-tapply(df_t$RANGE,as.factor(df_t$LOCATION),summary)
sm_5 %>% head(100) %>% boxplot(col = "blue",list(names))
# Load required libraries
library(leaflet)
library(dplyr)
library(maps)
library(mapproj)
library(ggplot2)
library(plotly)
##
## Attaching package: 'plotly'
## The following object is masked from 'package:MASS':
##
## select
## The following object is masked from 'package:Hmisc':
##
## subplot
## The following object is masked from 'package:ggplot2':
##
## last_plot
## The following object is masked from 'package:stats':
##
## filter
## The following object is masked from 'package:graphics':
##
## layout
#USA map ggplot 1
ggplot(usa_mp,aes(x=long,y=lat,group=group))+geom_path()
#USA map ggplot 2
ggplot(usa_mp,aes(x=long,y=lat,group=group))+geom_polygon()
#USA map ggplot 3
ggplot(usa_mp,aes(x=long,y=lat,group=group))+geom_polygon(aes(fill=region),show.legend = F)+geom_path()
#WA skeleton/border map
ggplot(waf_mp,aes(x=long,y=lat,group=group))+geom_path()
#WA filled map
ggplot(waf_mp,aes(x=long,y=lat,group=group))+geom_polygon()
#WA color map
ggplot(waf_mp,aes(x=long,y=lat,group=group))+geom_polygon(aes(fill=subregion))
#str
waf_mp %>% str()
## 'data.frame': 545 obs. of 6 variables:
## $ long : num -123 -123 -123 -123 -123 ...
## $ lat : num 48.6 48.6 48.6 48.6 48.6 ...
## $ group : num 1 1 1 1 1 1 1 1 1 1 ...
## $ order : int 1 2 3 4 5 6 7 8 9 10 ...
## $ region : chr "washington" "washington" "washington" "washington" ...
## $ subregion: chr "san juan island" "san juan island" "san juan island" "san juan island" ...
#WA bordered color map
ggplot(waf_mp,aes(x=long,y=lat,group=group))+geom_polygon(aes(fill=subregion,color=subregion),alpha=0.8)+geom_path()
#bordered color subregion map
ggplot(waf_mp,aes(x=long,y=lat,group=group))+geom_polygon(aes(fill=subregion,color=subregion),alpha=0.8)+geom_path(aes(x=long,y=lat,group=subregion,fill=subregion),inherit.aes = F,show.legend = T)
## Warning in geom_path(aes(x = long, y = lat, group = subregion, fill =
## subregion), : Ignoring unknown aesthetics: fill
Started with following prompt: “Write an R script using the packages maps and mapproj, and using the map_data function for an interactive plotly states_map of Washington State grouped by county area. Join lat and long from states_map to the LATITUDEs and LONGITUDEs, binned, from strm data frame; group aes mapping of latitude and longitude in joined data by region and county.” ###a. B/[i]/ng Code:
# Load the packages
library (maps)
library (mapproj)
library (plotly)
# Get the states map data
states_map <- map_data ("state")
# Filter for Washington State
wa_map <- states_map %>% filter (region == "washington")
# Get the county names and areas
wa_counties <- map("county", "washington", plot = T)
wa_counties$names %>% unique()
## [1] "washington,adams" "washington,asotin"
## [3] "washington,benton" "washington,chelan"
## [5] "washington,clallam" "washington,clark"
## [7] "washington,columbia" "washington,cowlitz"
## [9] "washington,douglas" "washington,ferry"
## [11] "washington,franklin" "washington,garfield"
## [13] "washington,grant" "washington,grays harbor"
## [15] "washington,island" "washington,jefferson"
## [17] "washington,king" "washington,kitsap"
## [19] "washington,kittitas" "washington,klickitat"
## [21] "washington,lewis" "washington,lincoln"
## [23] "washington,mason" "washington,okanogan"
## [25] "washington,pacific" "washington,pend oreille"
## [27] "washington,pierce:main" "washington,pierce:penrose"
## [29] "washington,san juan:lopez island" "washington,san juan:orcas island"
## [31] "washington,san juan:san juan island" "washington,skagit"
## [33] "washington,skamania" "washington,snohomish"
## [35] "washington,spokane" "washington,stevens"
## [37] "washington,thurston" "washington,wahkiakum"
## [39] "washington,walla walla" "washington,whatcom"
## [41] "washington,whitman" "washington,yakima"
wa_counties$x %>% unique() %>% summary()
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## -124.7 -122.7 -121.1 -120.9 -119.3 -116.9 1
wa_counties$x %>% hist(main = "Histogram of Longitude distribution for storms 2014, by AAA")
waf_mp %>% unique() %>% head()
## long lat group order region subregion
## 1 -123.0198 48.56963 1 1 washington san juan island
## 2 -123.0542 48.61547 1 2 washington san juan island
## 3 -123.0943 48.60974 1 3 washington san juan island
## 4 -123.0943 48.58109 1 4 washington san juan island
## 5 -123.1172 48.56390 1 5 washington san juan island
## 6 -123.1114 48.53526 1 6 washington san juan island
waf_mp %>% summary()
## long lat group order
## Min. :-124.7 Min. :45.53 Min. :1.000 Min. : 1.0
## 1st Qu.:-123.0 1st Qu.:46.94 1st Qu.:5.000 1st Qu.:141.0
## Median :-122.7 Median :47.62 Median :5.000 Median :277.0
## Mean :-122.5 Mean :47.49 Mean :4.631 Mean :276.6
## 3rd Qu.:-122.5 3rd Qu.:48.23 3rd Qu.:5.000 3rd Qu.:413.0
## Max. :-116.9 Max. :49.01 Max. :5.000 Max. :549.0
## region subregion
## Length:545 Length:545
## Class :character Class :character
## Mode :character Mode :character
##
##
##
wa_counties$y %>% unique() %>% summary()
## Min. 1st Qu. Median Mean 3rd Qu. Max. NA's
## 45.53 46.61 47.42 47.39 48.17 49.01 1
wa_counties$y %>% hist(main = "Histogram of Latitude distribution for storms 2014, by AAA")
This marks the end of the Bing code used in this project/demonstration. The rest of the code was removed. Included below is the full original R code from Bing from the prompt
##Load the packages library (maps)
#library (mapproj)
#library (plotly)
##Get the states map data
#states_map <- map_data ("state")
##Filter for Washington State
#wa_map <- states_map %>% filter (region == "washington")
#Get the county names and areas
#wa_counties <- map ("county", "washington", plot = FALSE) %>%
#data.frame () %>%
#mutate (county = tolower (sub ("washington,", "", ID)))
##Join the county names and areas to the map data
#wa_map <- wa_map %>% left_join (wa_counties, by = c ("subregion" = "county"))
##Load the strm data frame
#strm <- read.csv ("strm.csv")
##Bin the latitudes and longitudes
#strm <- strm %>%
#mutate (lat_bin = cut (LATITUDE, breaks = seq (45, 49, by = 0.5)),
#lon_bin = cut (LONGITUDE, breaks = seq (-125, -117, by = 0.5)))
##Join the latitudes and longitudes from strm to the map data
#wa_map <- wa_map %>% left_join (strm %>% select (lat_bin, lon_bin), by = c ("lat" = "lat_bin", "long" = "lon_bin"))
##Plot the interactive map using plotly
#p <- ggplot (wa_map, aes (x = long, y = lat)) + geom_polygon (aes (group = group, fill = area), color = "white") + coord_map ("albers", lat0 = 39, lat1 = 45) + # Use a different projection theme_void ()
#ggplotly (p) %>% layout (dragmode = "select")
##[Note:] I edited the code output that is above heavily after the output from Bing. Then I added this code manually for the final graphing aesthetics demo.
wa_cs<-c()
wa_cs <- map_data("county", "washington") %>%
data.frame ()
wa_cs$subregion<-as.factor(wa_cs$subregion)
wa_cs$region<-as.factor(wa_cs$region)
wa_cs %>% ggplot(aes(x=long,y=lat,group=subregion,alpha=2),show.legend=T)+
geom_path(aes(x=long,y=lat,group=subregion,color="black"),show.legend = T,inherit.aes = F)+
geom_polygon(aes(color=region,fill=(subregion)), show.legend = F)
citation()
## To cite R in publications use:
##
## R Core Team (2023). _R: A Language and Environment for Statistical
## Computing_. R Foundation for Statistical Computing, Vienna, Austria.
## <https://www.R-project.org/>.
##
## A BibTeX entry for LaTeX users is
##
## @Manual{,
## title = {R: A Language and Environment for Statistical Computing},
## author = {{R Core Team}},
## organization = {R Foundation for Statistical Computing},
## address = {Vienna, Austria},
## year = {2023},
## url = {https://www.R-project.org/},
## }
##
## We have invested a lot of time and effort in creating R, please cite it
## when using it for data analysis. See also 'citation("pkgname")' for
## citing R packages.
td<-Sys.Date()
print(td)
## [1] "2023-06-07"