# install.packages("ggplot2", dependencies=TRUE, type="win.binary") # install.packages("ggmap", dependencies=TRUE, type="win.binary") # install.packages("rworldmap") # install.packages("rworldxtra") # install.packages("devtools", dependencies=TRUE, type="win.binary") # devtools::install_github("dkahle/ggmap") library(ggplot2) library(ggmap) library(rworldmap) library(rworldxtra) ################################################ BALICEK GGMAP ################################################ # zdroje map: stamen, google, osm a cloudmade # k dnesnimu dni pouze stamen nevyzaduje api key # priklad pouziti ethiopia1 <- get_stamenmap(c(left = 30, bottom = 0, right = 50, top = 20), zoom = 5, maptype = "toner-lite") # note: vsimnete si, ze vysledkem get_map/get_stamenmap je specialni typ objektu - rastrovy objekt str(ethiopia1) # prvni radek hexadecimalni kody barev ggmap(ethiopia1) ethiopia2 <- get_map(c(left = 30, bottom = 0, right = 50, top = 20), zoom = 5, maptype = "watercolor", source = "stamen") ggmap(ethiopia2) # pozor u vetsich map - zejmena pokud chceme detailnejsi typ mapy ! # priklad, zobrazeni mapy Chicaga bbox <- c(left = -87.936287, bottom = 41.679835, right = -87.447052, top = 42.000835) ggmap(get_stamenmap(bbox, zoom = 11)) ggmap(get_stamenmap(bbox, zoom = 12)) # ruzne mapove typy ggmap(get_stamenmap(bbox, maptype = "terrain", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "terrain-background", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "terrain-lines", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-2010", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-2011", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-background", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-hybrid", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-labels", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-lines", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "toner-lite", zoom = 11)) ggmap(get_stamenmap(bbox, maptype = "watercolor", zoom = 11)) # # chceme zobrazit lokality: Dolni Vestonice, Brno a Prahu # Note: pokud se Vam nechce rucne prevadet jednotky, napr.: https://prevodyonline.eu/cz/souradnice.html # Souradnice lat <- c(48.883, 49.200, 50.083) long <- c(16.633, 16.617, 14.417) df <- as.data.frame(cbind(long,lat)) naleziste <- get_map(c(left = 11, bottom = 48, right = 20, top = 51.5), zoom = 5, maptype = "terrain-background", source = "stamen") ggmap(naleziste) # note: zoom - ovlivnuje detailnost mapy - cim vetsi, tim detailnejsi # pridame body ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) # nechceme legendu # Note: protoze byl pouzit shape 24 coz je trojuhelnicek s vyplni, je mozne pouzit parametr aes fill # nechceme legendu windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) # chceme body spojit useckou # moznost 1 funkce geom_line windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_line(aes(x = long, y = lat), col = "tomato", data = df) # moznost 2 funkce geom_path ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_path(data = df, aes(x = long, y = lat), col = "darkblue") # POZOR:geom_line spojuje data podle toho, jak jsou zadana, tzn. zadame-li body v poradi 1, 3, 2 spoji je # v tomto poradi, zatimco geom_path spojuje data podle osy x (podle poradi na ose x)! # note: vsimnete si jak jsou data spojena - maji V tvar, pokud bychom chteli jenom dva body pr. druhy a treti df2 <- df[2:3,] windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_path(data = df2, aes(x = long, y = lat), col = "gold", size = 2, lineend = "round", linetype = 2) # size - velikost cary # lineend = "round" - konce cary chceme zaoblene # linetype - typ čáry # je mozne tuto caru jakkoliv upravovat - pr. udelame z ni sipku windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_path(data = df2, aes(x = long, y = lat), col = "gold", size = 2, lineend = "round", arrow = arrow()) # sipku muzeme take upravovat windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8),fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_path(data = df2, aes(x = long, y = lat), col = "gold", size = 1, lineend = "round", arrow = arrow(angle = 15, type = "closed")) # sipka na obou stranach primky (parametr ends) windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "red", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_path(data = df2, aes(x = long, y = lat), col = "gold", size = 1, lineend = "round", arrow = arrow(angle = 15, ends = "both", type = "closed")) # Note: dalsi argumenty a nastaveni napr. zde: http://ggplot2.tidyverse.org/reference/geom_path.html # popiseme body pomoci funkce geom_text windows() ggmap(naleziste) + geom_point(data = df, aes(x = long, y = lat, alpha = 0.8), fill = "darkgreen", size = 5, shape = 24) + guides(fill=FALSE, alpha=FALSE, size=FALSE) + geom_path(data = df, aes(x = long, y = lat), col = "green", size = 1, lineend = "round", arrow = arrow(angle = 15, ends = "both", type = "closed")) + geom_text(data = df, aes(x = long, y = lat, label = c("Dolni Vestonice", "Brno", "Praha")), size = 3, vjust = 0, hjust = -0.5, col = "darkgreen") ################# ANALYZA DAT ################ data_vzorky <- read.table("vzorky_maps.txt",header = T, sep = "\t") str(data_vzorky) attach(data_vzorky) levels(typ) # nejprve ziskame mapu Houstonu bbox <- c(left = -95.40, bottom = 29.73, right = -95.34, top = 29.79) ggmap(get_stamenmap(bbox, zoom = 13)) HoustonMap_2 <- get_map(bbox, zoom = 13, maptype = "terrain-lines", source = "stamen") ggmap(HoustonMap_2) # do sam. okna zobrazime mapu Houstonu + pouzijeme funkci geom_point windows() ggmap(HoustonMap_2) + geom_point(data = data_vzorky, aes(x = lon, y = lat, alpha = typ), shape = 19, col = "darkblue") # samotne vykresleni bodu timto zpusobem neni prilis prehledne # vykreslime misto bodu "ctverce rozlozeni" # note: funkce stat_bin2d: rozdeli rovinu (vykreslovanou plochu) na obdelniky, vypocita pocet pripadu v kazdem obdelniku a potom (ve vychozim nastaveni) # mapuje pocet pripadu na obdelnik. Jedná se o uzitecnou alternativu k funkci geom_point v pritomnosti tzv. overplottingu (mnoho bodu, casto pres sebe a # tak velmi neprehledne zobrazeni). windows() ggmap(HoustonMap_2) + stat_bin2d(aes(x = lon, y = lat, colour = typ, fill = typ), size = 0.5, bins = 30, alpha = 0.5, data = data_vzorky) # nyni chceme zobrazit denzitu rozlozeni (kde se vyskytovalo nejvice nalezu - jedno jakeho typu) # k tomu pouzijeme funkci stat_density2d windows() ggmap(HoustonMap_2) + stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), size = 2, bins = 5, data = data_vzorky, geom = "polygon") windows() ggmap(HoustonMap_2) + stat_density2d(aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), size = 2, bins = 8, data = data_vzorky, geom = "polygon") # note: parametr bins určuje kolik "vrstev" bude vykreslovano, resp. zjemnuje krok prechodu # parametr geom urcuje typ geometrickeho objektu ktery vykreslujeme # parametr fill = ..level.. je to same jako fill = stat(level) zalezi na verzi ggplot2 # tzn. chceme aesthetics vykreslit na zaklade vypoctu cetnosti vyskytu v tomto pripade nalezu # pokud nejsme spokojeni s timto zobrazenim, muzeme vyuzit pro barvy napr. balicek RColorBrewer a pomoci nej # vytvorit heat mapu (note, zaroven zvysime bins na 25 abychom zjemnili prechod) # install.packages("RColorBrewer") library(RColorBrewer) library(ggplot2) ggmap(HoustonMap_2) + stat_density_2d(data = data_vzorky, aes(x = lon, y = lat, fill = ..level..), alpha = 0.2, bins = 25, geom = "polygon") + scale_fill_gradientn(colors = brewer.pal(7, "YlOrRd")) # chceme prizpusobit i alpha statistice vyskytu (note, umistujeme alpha do aes) ggmap(HoustonMap_2) + stat_density_2d(data = data_vzorky, aes(x = lon, y = lat, fill = ..level.., alpha = ..level..), bins = 25, geom = "polygon") + scale_fill_gradientn(colors = brewer.pal(7, "BuGn")) # Note: funkce brewer.pal() - prvni argument rika kolik odlisnych barev v palete chceme pouzit a druhy argument # nazev palety kterou chceme pouzit pro detailnejsi popis: https://www.nceas.ucsb.edu/~frazier/RSpatialGuides/colorPaletteCheatsheet.pdf # Pripomenuti, nezapominame citovat balicky, kterou pouzivame navic citation("ggmap") citation("RColorBrewer") ## DOMACI UKOL 10 # A) Zobrazte na mape Australska mesta: Canberra, Sydney, Melbourne, Brisbane a Perth. # A mesta spojte jednou linii.