class: center, middle, inverse, title-slide .title[ # Продвинутая графика ] .subtitle[ ## Визуализация и анализ географических данных на языке R ] .author[ ### Тимофей Самсонов ] .institute[ ### МГУ имени Ломоносова, Географический факультет ] .date[ ### 2023-10-10 ] --- ## Источники данных - __Евростат__ (https://ec.europa.eu/eurostat/web/main/home), - __NASA POWER__ (https://power.larc.nasa.gov/) - __USDA NRCS Soil Data Access__ (https://sdmdataaccess.nrcs.usda.gov/) ```r library(tidyverse) library(eurostat) library(nasapower) library(soilDB) ``` > Пакет soilDB лучше устанавливать из консоли командой `install.packages('soilDB', dependencies = TRUE)`. Указание параметра `dependencies = TRUE` обеспечит установку других пакетов, от которых он зависит. --- ## Коды Евростата Например, [таблица по продуктам питания, напиткам и табаку](https://ec.europa.eu/eurostat/databrowser/view/tet00034/default/table?lang=en) имеет код __ext_lt_mainagri__: <img src="img/eurostat.png" width="907" /> --- ## Загрузка данных Евростата .code-small[ ```r tables = c('ext_lt_mainagri', 'ext_lt_mainrawm', 'ext_lt_mainmine', 'ext_lt_mainchem','ext_lt_mainmanu', 'ext_lt_mainmach') countries = ISOcodes::ISO_3166_1$Alpha_2 trades = lapply(tables, function(X) { # прочтем несколько таблиц в список get_eurostat(X) |> filter(partner %in% countries) |> label_eurostat() }) |> bind_rows() |> select(-geo) |> # убираем столбец с территорией торговли, т.к. там только Евросоюз filter(str_detect(indic_et, 'Exports in|Imports in')) |> pivot_wider(names_from = indic_et, values_from = values) |> rename(export = `Exports in million of ECU/EURO`, # дадим им краткие названия import = `Imports in million of ECU/EURO`) |> mutate(partner = as.factor(partner)) trades # посмотрим, что получилось ## # A tibble: 25,710 × 5 ## sitc06 partner time export import ## <chr> <fct> <date> <dbl> <dbl> ## 1 Food, drinks and tobacco Andorra 2022-01-01 340. 0.8 ## 2 Food, drinks and tobacco United Arab Emirates 2022-01-01 3113. 64.3 ## 3 Food, drinks and tobacco Afghanistan 2022-01-01 67.2 15.2 ## 4 Food, drinks and tobacco Antigua and Barbuda 2022-01-01 17.2 0.7 ## 5 Food, drinks and tobacco Anguilla (UK) 2022-01-01 1.3 0 ## 6 Food, drinks and tobacco Albania 2022-01-01 734. 234. ## 7 Food, drinks and tobacco Armenia 2022-01-01 171. 18.6 ## 8 Food, drinks and tobacco Angola 2022-01-01 642. 50.4 ## 9 Food, drinks and tobacco Antarctica 2022-01-01 0.3 0 ## 10 Food, drinks and tobacco Argentina 2022-01-01 191. 6230. ## # ℹ 25,700 more rows ``` ] --- ## Загрузка данных NASA POWER ```r daily_single_ag <- get_power( community = "ag", lonlat = c(60.59, 56.84), pars = c("RH2M", "T2M"), dates = c("1995-04-01", "1995-04-30"), temporal_api = "daily" ) as_tibble(daily_single_ag) # посмотрим, что получилось ## # A tibble: 30 × 9 ## LON LAT YEAR MM DD DOY YYYYMMDD RH2M T2M ## <dbl> <dbl> <dbl> <int> <int> <int> <date> <dbl> <dbl> ## 1 60.6 56.8 1995 4 1 91 1995-04-01 81.7 5.65 ## 2 60.6 56.8 1995 4 2 92 1995-04-02 81.9 7.78 ## 3 60.6 56.8 1995 4 3 93 1995-04-03 78 8.31 ## 4 60.6 56.8 1995 4 4 94 1995-04-04 81.2 7.71 ## 5 60.6 56.8 1995 4 5 95 1995-04-05 92.4 5.1 ## 6 60.6 56.8 1995 4 6 96 1995-04-06 90.2 6.31 ## 7 60.6 56.8 1995 4 7 97 1995-04-07 83.4 3.38 ## 8 60.6 56.8 1995 4 8 98 1995-04-08 79 2.46 ## 9 60.6 56.8 1995 4 9 99 1995-04-09 81.2 3.49 ## 10 60.6 56.8 1995 4 10 100 1995-04-10 76.9 4.4 ## # ℹ 20 more rows ``` --- ## Загрузка данных NASA POWER ```r interannual_sse <- get_power( community = "ag", lonlat = c(60.59, 56.84), dates = 1995:2015, temporal_api = "climatology", pars = c("CLRSKY_SFC_SW_DWN", "ALLSKY_SFC_SW_DWN") ) as_tibble(interannual_sse) # посмотрим, что получилось ## # A tibble: 2 × 16 ## LON LAT PARAMETER JAN FEB MAR APR MAY JUN JUL AUG SEP ## <dbl> <dbl> <chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> ## 1 60.6 56.8 ALLSKY_SFC_… 2.59 6.03 11.0 14.8 18.7 20.0 19.4 14.8 9.78 ## 2 60.6 56.8 CLRSKY_SFC_… 3.37 7.41 13.8 20.2 26.0 28.4 26.4 21.2 15 ## # ℹ 4 more variables: OCT <dbl>, NOV <dbl>, DEC <dbl>, ANN <dbl> ``` --- ## Загрузка данных Soil Data Access Результирющий объект представляет собой список со множеством таблиц, которые характеризуют как почвенную серию в целом, так и отдельные ее разрезы: ```r soils = c('wilkes', 'chewacla', 'congaree') series = fetchOSD(soils, extended = TRUE) str(series) ## List of 14 ## $ SPC :Formal class 'SoilProfileCollection' [package "aqp"] with 9 slots ## .. ..@ idcol : chr "id" ## .. ..@ hzidcol : chr "hzID" ## .. ..@ depthcols : chr [1:2] "top" "bottom" ## .. ..@ metadata :List of 8 ## .. .. ..$ aqp_df_class : chr "data.frame" ## .. .. ..$ aqp_group_by : chr "" ## .. .. ..$ aqp_hzdesgn : chr "hzname" ## .. .. ..$ aqp_hztexcl : chr "texture_class" ## .. .. ..$ depth_units : chr "cm" ## .. .. ..$ stringsAsFactors: logi FALSE ## .. .. ..$ original.order : int [1:22] 1 2 3 4 5 6 7 8 9 10 ... ## .. .. ..$ origin : chr "OSD via Soilweb / fetchOSD" ## .. ..@ horizons :'data.frame': 22 obs. of 21 variables: ## .. .. ..$ id : chr [1:22] "CHEWACLA" "CHEWACLA" "CHEWACLA" "CHEWACLA" ... ## .. .. ..$ top : int [1:22] 0 10 36 66 97 119 152 0 20 46 ... ## .. .. ..$ bottom : int [1:22] 10 36 66 97 119 152 203 20 46 56 ... ## .. .. ..$ hzname : chr [1:22] "Ap" "Bw1" "Bw2" "Bw3" ... ## .. .. ..$ soil_color : chr [1:22] "#7E5A3BFF" "#7A5C37FF" "#7A5C37FF" "#7E5A3BFF" ... ## .. .. ..$ hue : chr [1:22] "7.5YR" "10YR" "10YR" "7.5YR" ... ## .. .. ..$ value : int [1:22] 4 4 4 4 5 5 4 4 4 3 ... ## .. .. ..$ chroma : int [1:22] 4 4 4 4 8 1 4 4 3 3 ... ## .. .. ..$ dry_hue : chr [1:22] "7.5YR" "10YR" "10YR" "7.5YR" ... ## .. .. ..$ dry_value : int [1:22] 6 6 6 6 6 6 6 6 6 5 ... ## .. .. ..$ dry_chroma : int [1:22] 4 4 4 4 7 1 4 4 3 3 ... ## .. .. ..$ texture_class : Ord.factor w/ 21 levels "coarse sand"<..: 13 18 17 13 17 17 13 13 13 NA ... ## .. .. ..$ cf_class : logi [1:22] NA NA NA NA NA NA ... ## .. .. ..$ pH : logi [1:22] NA NA NA NA NA NA ... ## .. .. ..$ pH_class : Ord.factor w/ 12 levels "ultra acid"<"extremely acid"<..: 3 3 3 3 3 3 3 4 NA NA ... ## .. .. ..$ distinctness : chr [1:22] "clear" "gradual" "gradual" "gradual" ... ## .. .. ..$ topography : chr [1:22] "smooth" "wavy" "wavy" "wavy" ... ## .. .. ..$ dry_color_estimated : logi [1:22] TRUE TRUE TRUE TRUE TRUE TRUE ... ## .. .. ..$ moist_color_estimated: logi [1:22] FALSE FALSE FALSE FALSE FALSE FALSE ... ## .. .. ..$ narrative : chr [1:22] "Ap--0 to 4 inches; brown (7.5YR 4/4) loam; weak medium granular structure; friable; common very fine, fine, and"| __truncated__ "Bw1--4 to 14 inches; dark yellowish brown (10YR 4/4) silty clay loam; weak medium subangular blocky structure; "| __truncated__ "Bw2--14 to 26 inches; dark yellowish brown (10YR 4/4) clay loam; weak medium subangular blocky structure; friab"| __truncated__ "Bw3--26 to 38 inches; brown (7.5YR 4/4) loam; weak medium subangular blocky structure; friable; common fine roo"| __truncated__ ... ## .. .. ..$ hzID : chr [1:22] "1" "2" "3" "4" ... ## .. ..@ site :'data.frame': 3 obs. of 33 variables: ## .. .. ..$ id : chr [1:3] "CHEWACLA" "CONGAREE" "WILKES" ## .. .. ..$ soiltaxclasslastupdated: chr [1:3] "2010-02-11 00:00:00+00" "2002-07-18 00:00:00+00" "1997-06-06 00:00:00+00" ## .. .. ..$ mlraoffice : chr [1:3] "raleigh, nc" "raleigh, nc" "raleigh, nc" ## .. .. ..$ series_status : chr [1:3] "established" "established" "established" ## .. .. ..$ family : chr [1:3] "fine-loamy, mixed, active, thermic fluvaquentic dystrudepts" "fine-loamy, mixed, active, nonacid, thermic oxyaquic udifluvents" "loamy, mixed, active, thermic, shallow typic hapludalfs" ## .. .. ..$ soilorder : chr [1:3] "inceptisols" "entisols" "alfisols" ## .. .. ..$ suborder : chr [1:3] "udepts" "fluvents" "udalfs" ## .. .. ..$ greatgroup : chr [1:3] "dystrudepts" "udifluvents" "hapludalfs" ## .. .. ..$ subgroup : chr [1:3] "fluvaquentic dystrudepts" "oxyaquic udifluvents" "typic hapludalfs" ## .. .. ..$ tax_partsize : chr [1:3] "fine-loamy" "fine-loamy" "loamy" ## .. .. ..$ tax_partsizemod : logi [1:3] NA NA NA ## .. .. ..$ tax_ceactcl : chr [1:3] "active" "active" "active" ## .. .. ..$ tax_reaction : chr [1:3] NA "nonacid" NA ## .. .. ..$ tax_tempcl : chr [1:3] "thermic" "thermic" "thermic" ## .. .. ..$ originyear : logi [1:3] NA NA NA ## .. .. ..$ establishedyear : int [1:3] 1937 1904 1916 ## .. .. ..$ descriptiondateinitial : chr [1:3] "2010-02-11 00:00:00+00" "2002-07-18 00:00:00+00" "2007-09-06 00:00:00+00" ## .. .. ..$ descriptiondateupdated : chr [1:3] "2010-02-11 00:00:00+00" "2002-07-18 00:00:00+00" "2021-01-27 16:02:50+00" ## .. .. ..$ benchmarksoilflag : int [1:3] 1 0 0 ## .. .. ..$ statsgoflag : int [1:3] 1 1 1 ## .. .. ..$ objwlupdated : chr [1:3] "2012-08-01 14:06:37+00" "2012-08-01 14:06:37+00" "2021-01-27 16:02:50+00" ## .. .. ..$ recwlupdated : chr [1:3] "2012-08-01 14:06:37+00" "2012-08-01 14:06:37+00" "2021-01-27 16:02:50+00" ## .. .. ..$ typelocstareaiidref : int [1:3] 6691 6706 6691 ## .. .. ..$ typelocstareatypeiidref: int [1:3] 3 3 3 ## .. .. ..$ soilseriesiid : int [1:3] 24628 1057 23800 ## .. .. ..$ soilseriesdbiidref : int [1:3] 122 122 122 ## .. .. ..$ grpiidref : int [1:3] 15801 15801 15801 ## .. .. ..$ tax_minclass : chr [1:3] "mixed" "mixed" "mixed" ## .. .. ..$ subgroup_mod : chr [1:3] "fluvaquentic" "oxyaquic" "typic" ## .. .. ..$ greatgroup_mod : chr [1:3] "dystr" "udi" "hapl" ## .. .. ..$ drainagecl : chr [1:3] "somewhat poorly" "well to moderately well" "well" ## .. .. ..$ ac : int [1:3] 1256820 216072 614614 ## .. .. ..$ n_polygons : int [1:3] 39164 10384 34815 ## .. ..@ sp :Formal class 'SpatialPoints' [package "sp"] with 3 slots ## .. .. .. ..@ coords : num [1, 1] 0 ## .. .. .. ..@ bbox : logi [1, 1] NA ## .. .. .. ..@ proj4string:Formal class 'CRS' [package "sp"] with 1 slot ## .. .. .. .. .. ..@ projargs: chr NA ## .. ..@ diagnostic :'data.frame': 0 obs. of 0 variables ## .. ..@ restrictions:'data.frame': 0 obs. of 0 variables ## $ competing :'data.frame': 1 obs. of 3 variables: ## ..$ series : chr "CHEWACLA" ## ..$ competing: chr "OAKBORO" ## ..$ family : chr "fine-loamy, mixed, active, thermic fluvaquentic dystrudepts" ## $ geog_assoc_soils:'data.frame': 22 obs. of 2 variables: ## ..$ series: chr [1:22] "CONGAREE" "CONGAREE" "CONGAREE" "CONGAREE" ... ## ..$ gas : chr [1:22] "ALTAVISTA" "AUGUSTA" "BUNCOMBE" "CARTECAY" ... ## $ geomcomp :'data.frame': 3 obs. of 9 variables: ## ..$ series : chr [1:3] "CHEWACLA" "CONGAREE" "WILKES" ## ..$ Interfluve : num [1:3] 1 0 0.178 ## ..$ Crest : num [1:3] 0 0 0.027 ## ..$ Head Slope : int [1:3] 0 0 0 ## ..$ Nose Slope : int [1:3] 0 0 0 ## ..$ Side Slope : num [1:3] 0 0 0.795 ## ..$ Base Slope : int [1:3] 0 1 0 ## ..$ n : int [1:3] 3 1 185 ## ..$ shannon_entropy: num [1:3] 0 0 0.365 ## $ hillpos :'data.frame': 3 obs. of 8 variables: ## ..$ series : chr [1:3] "CHEWACLA" "CONGAREE" "WILKES" ## ..$ Toeslope : num [1:3] 0.963 0.786 0 ## ..$ Footslope : num [1:3] 0.0366 0.2143 0 ## ..$ Backslope : num [1:3] 0 0 0.637 ## ..$ Shoulder : num [1:3] 0 0 0.225 ## ..$ Summit : num [1:3] 0 0 0.139 ## ..$ n : int [1:3] 82 14 245 ## ..$ shannon_entropy: num [1:3] 0.0975 0.3228 0.5573 ## $ mtnpos : logi FALSE ## $ terrace :'data.frame': 2 obs. of 5 variables: ## ..$ series : chr [1:2] "CHEWACLA" "CONGAREE" ## ..$ Tread : num [1:2] 0.977 1 ## ..$ Riser : num [1:2] 0.023 0 ## ..$ n : int [1:2] 87 36 ## ..$ shannon_entropy: num [1:2] 0.068 0 ## $ flats :'data.frame': 2 obs. of 7 variables: ## ..$ series : chr [1:2] "CHEWACLA" "CONGAREE" ## ..$ Dip : num [1:2] 0.1455 0.0667 ## ..$ Talf : num [1:2] 0.855 0.867 ## ..$ Flat : int [1:2] 0 0 ## ..$ Rise : num [1:2] 0 0.0667 ## ..$ n : int [1:2] 55 15 ## ..$ shannon_entropy: num [1:2] 0.258 0.301 ## $ pmkind :'data.frame': 6 obs. of 5 variables: ## ..$ series: chr [1:6] "CHEWACLA" "CHEWACLA" "CONGAREE" "CONGAREE" ... ## ..$ pmkind: chr [1:6] "Alluvium" "Residuum" "Alluvium" "Fluviomarine deposits" ... ## ..$ n : int [1:6] 206 1 72 13 1 262 ## ..$ total : int [1:6] 207 207 86 86 86 262 ## ..$ P : num [1:6] 0.9952 0.0048 0.8372 0.1512 0.0116 ... ## $ pmorigin :'data.frame': 24 obs. of 5 variables: ## ..$ series : chr [1:24] "CHEWACLA" "CHEWACLA" "CHEWACLA" "CHEWACLA" ... ## ..$ pmorigin: chr [1:24] "Igneous and metamorphic rock" "Sedimentary rock" "Mixed" "Granite and gneiss" ... ## ..$ n : int [1:24] 29 11 2 2 1 1 1 1 1 1 ... ## ..$ total : int [1:24] 51 51 51 51 51 51 51 51 51 51 ... ## ..$ P : num [1:24] 0.5686 0.2157 0.0392 0.0392 0.0196 ... ## $ mlra :'data.frame': 19 obs. of 4 variables: ## ..$ series : chr [1:19] "CHEWACLA" "CHEWACLA" "CHEWACLA" "CHEWACLA" ... ## ..$ mlra : chr [1:19] "129" "135A" "136" "133A" ... ## ..$ area_ac : int [1:19] 6166 3878 927251 128381 78428 60214 29004 13536 9495 2126 ... ## ..$ membership: num [1:19] 0.005 0.003 0.738 0.102 0.062 0.048 0.023 0.011 0.008 0.01 ... ## $ climate.annual :'data.frame': 24 obs. of 12 variables: ## ..$ series : chr [1:24] "CHEWACLA" "CHEWACLA" "CHEWACLA" "CHEWACLA" ... ## ..$ climate_var: chr [1:24] "Elevation (m)" "Effective Precipitation (mm)" "Frost-Free Days" "Mean Annual Air Temperature (degrees C)" ... ## ..$ minimum : num [1:24] 0 128.8 177 11.3 986 ... ## ..$ q01 : num [1:24] 7 216.4 188 12.9 1069 ... ## ..$ q05 : num [1:24] 34 247.4 196 13.5 1093 ... ## ..$ q25 : num [1:24] 125 312.2 208 14.9 1136 ... ## ..$ q50 : num [1:24] 192 349.8 218 15.7 1178 ... ## ..$ q75 : num [1:24] 248 441.2 227 16.4 1276 ... ## ..$ q95 : num [1:24] 348 568.8 235 17.3 1392 ... ## ..$ q99 : num [1:24] 480 740.8 243 17.8 1575 ... ## ..$ maximum : num [1:24] 919 1382.8 300 19.7 2202 ... ## ..$ n : int [1:24] 32689 32689 32689 32689 32689 32689 32689 32689 8392 8392 ... ## $ climate.monthly :'data.frame': 72 obs. of 14 variables: ## ..$ series : chr [1:72] "CHEWACLA" "CHEWACLA" "CHEWACLA" "CHEWACLA" ... ## ..$ climate_var: chr [1:72] "ppt1" "ppt2" "ppt3" "ppt4" ... ## ..$ minimum : num [1:72] 61 64 81 63 60 78 83 74 68 53 ... ## ..$ q01 : num [1:72] 74 71 92 71 69 83 92 85 81 72 ... ## ..$ q05 : num [1:72] 82 73 98 75 72 89 100 89 86 76 ... ## ..$ q25 : num [1:72] 93 83 105 83 81 96 108 96 91 82 ... ## ..$ q50 : num [1:72] 105 107 120 88 92 101 116 102 98 86 ... ## ..$ q75 : num [1:72] 115 121 128 95 101 106 123 110 105 91 ... ## ..$ q95 : num [1:72] 131 135 137 111 111 117 133 129 118 100 ... ## ..$ q99 : num [1:72] 149 144 147 122 125 133 143 142 137 109 ... ## ..$ maximum : num [1:72] 248 227 239 150 167 198 220 207 192 158 ... ## ..$ n : int [1:72] 32689 32689 32689 32689 32689 32689 32689 32689 32689 32689 ... ## ..$ month : Factor w/ 12 levels "1","2","3","4",..: 1 2 3 4 5 6 7 8 9 10 ... ## ..$ variable : Factor w/ 2 levels "Potential ET (mm)",..: 2 2 2 2 2 2 2 2 2 2 ... ## $ soilweb.metadata:'data.frame': 18 obs. of 2 variables: ## ..$ product : chr [1:18] "block diagram archive" "cached sketches" "component pedons" "KSSL snapshot" ... ## ..$ last_update: chr [1:18] "2019-12-17" "2021-10-07" "2020-12-08" "2020-03-18" ... ``` --- ## Быстрый график ggplot .pull-left[ Суммарный экспорт через `qplot()`: ```r trades_total = trades |> group_by(time) |> summarise(export = sum(export), import = sum(import)) qplot(time, export, data = trades_total, geom = c('point', 'line')) ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/qplot-out-1.png)<!-- --> ] --- ## Базовый шаблон ggplot Базовый шаблон __ggplot__: ```r ggplot(data = <DATA>) + <GEOM_FUNCTION>(mapping = aes(<MAPPINGS>)) ``` где: - `DATA` — источник данных (фрейм, тиббл) - `GEOM_FUNCTION` — функция, отвечающая за геометрический тип графика (точки, линии, гистограммы и т.д.) - `MAPPINGS` — перечень соответствий между переменными данных (содержащихся в `DATA`) и графическими переменными (координатами, размерами, цветами и т.д.) --- ## Геометрические типы .pull-left[ Точки `geom_point()`: .code-small[ ```r ggplot(data = trades_total) + geom_point(mapping = aes(x = time, y = export)) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-8-1.png)<!-- --> ] ] .pull-right[ Линии `geom_line()`: .code-small[ ```r ggplot(data = trades_total) + geom_line(mapping = aes(x = time, y = export)) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-9-1.png)<!-- --> ] ] --- ## Геометрические типы .pull-left[ Ступенчатые кривые`geom_step()`: .code-small[ ```r ggplot(data = trades_total) + geom_step(mapping = aes(x = time, y = export)) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-10-1.png)<!-- --> ] ] .pull-right[ Совмещение геометрий: .code-small[ ```r ggplot(data = trades_total) + geom_line(mapping = aes(x = time, y = export)) + geom_point(mapping = aes(x = time, y = export)) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-11-1.png)<!-- --> ] ] --- ## Геометрические типы .pull-left[ Вынос общих данных: .code-small[ ```r ggplot(data = trades_total, mapping = aes(x = time, y = export)) + geom_line() + geom_point() ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-12-1.png)<!-- --> ] ] .pull-right[ Заливка `geom_area()`: .code-small[ ```r ggplot(data = trades_total, mapping = aes(x = time, y = export)) + geom_area(alpha = 0.5) + # полигон с прозрачностью 0,5 geom_line() + geom_point() ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-13-1.png)<!-- --> ] ] --- ## Геометрические типы ```r df = trades |> filter(sitc06 == 'Machinery and transport equipment', time == as.Date('2022-01-01'), export > 10000) ``` .pull-left[ Столбчатая диаграмма: ```r ggplot( df, mapping = aes( x = partner, y = export ) ) + geom_col() ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/barplot-out-1.png)<!-- --> ] --- ## Геометрические типы .pull-left[ Перестановка осей через `coord_flip()`: ```r ggplot(df, mapping = aes(x = partner, y = export)) + geom_col() + coord_flip() ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/flip-out-1.png)<!-- --> ] --- ## Графические переменные Функция `aes(...)` отображает переменные данных на графические переменные ```r # один цвет для графика (параметр за пределами aes) ggplot(trades_total) + geom_line(mapping = aes(x = time, y = export), color = 'blue') ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-15-1.png)<!-- --> --- ## Графические переменные ```r trade_russia = trades |> filter(partner == 'Russia') ggplot(trade_russia) + # у каждой группы данных свой цвет geom_line(mapping = aes(x = time, y = export, color = sitc06)) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-16-1.png)<!-- --> --- ## Графические переменные ```r ggplot(trade_russia, # а теперь и с точками mapping = aes(x = time, y = export, color = sitc06)) + geom_line() + geom_point() ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-17-1.png)<!-- --> --- ## Графические переменные Аналогичным образом работает параметр формы значка: ```r ggplot(trades_total) + # один значок для графика geom_point(mapping = aes(x = time, y = export), shape = 15) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-18-1.png)<!-- --> --- ## Графические переменные У каждой группы данных свой значок ```r ggplot(trade_russia) + geom_point(mapping = aes(x = time, y = export, shape = sitc06)) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-19-1.png)<!-- --> --- ## Графические переменные Управление размером через `size` и `linewidth`: ```r ggplot(trades_total, mapping = aes(x = time, y = export)) + geom_point(size = 5) + geom_line(linewidth = 2) ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-20-1.png)<!-- --> --- ## Графические переменные ```r ggplot(trade_russia, aes(x = time, y = export)) + geom_point(aes(shape = sitc06)) + geom_line(aes(group = sitc06)) # Группировка данных ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-21-1.png)<!-- --> --- ## Графические переменные ```r ggplot(df, mapping = aes(x = partner, y = export)) + geom_col(fill = 'plum4', color = 'black', linewidth = 0.2) + # цвет coord_flip() ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-22-1.png)<!-- --> --- ## Графические переменные Управление цветом: .code-small[ ```r df1 = trades |> filter(time == as.Date('2022-01-01')) |> group_by(partner) |> filter(sum(export) > 50000) df1 |> ggplot(mapping = aes(x = partner, y = export, fill = sitc06)) + # цвет geom_col(color = 'black', size = 0.2) + coord_flip() ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-23-1.png)<!-- --> ] --- ## Графические переменные .code-small[ ```r df1 |> ggplot(mapping = aes(x = partner, y = export, fill = sitc06)) + geom_col(color = 'black', size = 0.2, position = 'fill') + # проценты coord_flip() ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-24-1.png)<!-- --> ] --- ## Графические переменные Группировка по соседству: .code-small[ ```r filter(trade_russia, time >= as.Date('2013-01-01')) |> ggplot(mapping = aes(x = time, y = export, fill = sitc06)) + geom_col(color = 'black', size = 0.2, position = 'dodge') ``` ![](06_AdvGraphics_files/figure-html/unnamed-chunk-25-1.png)<!-- --> ] --- ## Системы координат .pull-left[ Перестановка осей через `coord_flip()`: ```r trades_type = trades |> group_by(sitc06, time) |> summarise(export = sum(export), import = sum(import)) ggplot(trades_type) + geom_point(mapping = aes( x = export, y = import, color = sitc06, size = time ), alpha = 0.5 ) + coord_flip() ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/flip2-out-1.png)<!-- --> ] --- ## Системы координат .left-40[ Логарифмирование: ```r ggplot( trades_type, mapping = aes( x = export, y = import, color = sitc06, size = time ) ) + geom_point(alpha = 0.5) + scale_x_log10() + scale_y_log10() ``` ] .right-60[ ![](06_AdvGraphics_files/figure-html/log-out-1.png)<!-- --> ] --- ## Системы координат .left-40[ Полярная система координат: ```r ggplot( df, mapping = aes( x = partner, y = export, fill = partner ) ) + geom_col() + coord_polar() ``` ] .right-60[ ![](06_AdvGraphics_files/figure-html/log2-out-1.png)<!-- --> ] --- ## Системы координат .left-40[ Преобразование квадратного корня: ```r ggplot( df, mapping = aes( x = partner, y = export, fill = partner ) ) + geom_col() + coord_polar() + scale_y_sqrt() ``` ] .right-60[ ![](06_AdvGraphics_files/figure-html/log3-out-1.png)<!-- --> ] --- ## Системы координат .pull-left[ Классическая секторная диаграмма: ```r ggplot( df, mapping = aes( x = '', y = export, fill = partner ) ) + geom_col( color = 'black', linewidth = 0.2 ) + coord_polar(theta = 'y') ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/polar2-out-1.png)<!-- --> ] --- ## Названия осей и легенды .pull-left[ .code-small[ ```r options(scipen = 999) ggplot(trades_type) + geom_point( mapping = aes( x = export, y = import, color = sitc06, size = time ), alpha = 0.5 ) + labs(color = "Вид продукции", size = 'Год') + ggtitle('Соотношение импорта и экспорта в странах Евросоюза (млн долл. США)', subtitle = 'Данные по ключевым партнерам') + xlab('Экспорт') + ylab('Импорт') ``` ] ] .pull-right[ ![](06_AdvGraphics_files/figure-html/axes-out-1.png)<!-- --> ] --- ## Разметка осей .left-40[ .code-small[ ```r brks = seq(0, 500000, 100000) ggplot( trades_type, mapping = aes( x = export, y = import, color = sitc06, size = time ) ) + geom_point(alpha = 0.5) + scale_x_log10( breaks = brks, labels = brks / 1000 ) + scale_y_log10( breaks = brks, labels = brks / 1000 ) ``` ] ] .right-60[ ![](06_AdvGraphics_files/figure-html/axes2-out-1.png)<!-- --> ] --- ## Разметка осей .left-40[ Диапазоны по осям и выделение ```r ggplot( trades_type, mapping = aes( x = export, y = import, color = sitc06, size = time ) ) + geom_point(alpha = 0.5) + lims(color = 'Food, drinks and tobacco') + xlim(0, 75000) + ylim(30000, 75000) ``` ] .right-60[ ![](06_AdvGraphics_files/figure-html/ranges-out-1.png)<!-- --> ] --- ## Подписи и аннотации .pull-left[ Подписи: ```r ggplot( data = trades_total, mapping = aes(x = time, y = export)) + geom_area(alpha = 0.5) + # полигон с прозрачностью 0,5 geom_line() + geom_point() + geom_text( # добавляем подписи aes(label = floor(export / 1000)), vjust = 0, nudge_y = 40000 ) ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/labels-out-1.png)<!-- --> ] --- ## Подписи и аннотации .left-40[ Подписи с фоновой плашкой: ```r ggplot(df, mapping = aes(x = partner, y = export)) + geom_col(fill = 'plum4', color = 'black', size = 0.2) + coord_flip() + geom_label(aes(y = export / 2, label = floor(export / 1000))) # добавляем подписи ``` ] .right-60[ ![](06_AdvGraphics_files/figure-html/labels2-out-1.png)<!-- --> ] --- ## Подписи и аннотации .pull-left[ __Аннотации__ представляют собой объекты, размещаемые на графике вручную: ```r ggplot(trades_total, mapping = aes(x = time, y = export)) + geom_area(alpha = 0.5) + # полигон с прозрачностью 0,5 geom_line() + geom_point() + geom_text(aes(label = floor(export / 1000)), vjust = 0, nudge_y = 40000) + annotate("text", x = as.Date('2009-01-01'), y = 550000, label = "Это провал", color = 'red') ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/anno-out-1.png)<!-- --> ] --- ## Подписи и аннотации .left-40[ Аннотирование регионов: ```r ggplot(trades_type, mapping = aes( x = export, y = import, color = sitc06, size = time )) + annotate("rect", xmin = 100000, xmax = 500000, ymin = 50000, ymax = 300000, alpha = .2, color = 'black', size = 0.1) + geom_point(alpha = 0.5) + annotate("text", x = 175000, y = 190000, label = "Chemicals", color = 'coral') ``` ] .right-60[ ![](06_AdvGraphics_files/figure-html/anno2-out-1.png)<!-- --> ] --- ## Фасеты ```r brks = c(0, 50, 100, 150, 200) trades |> filter(sitc06 == 'Machinery and transport equipment', time > as.Date('2014-01-01'), import > 30000) |> ggplot(mapping = aes(x = partner, y = import)) + geom_col() + scale_y_continuous(breaks = brks * 1e3, labels = brks) + ggtitle('Импорт продукции машиностроения (мдрд долл. США)', subtitle = 'Данные по ключевым партнерам') + coord_flip() + facet_wrap(~time) ``` --- ## Фасеты ![](06_AdvGraphics_files/figure-html/facets-out-1.png)<!-- --> --- ## Темы .pull-left[ ```r ggplot(data = trades_total, mapping = aes(x = time, y = export)) + geom_area(alpha = 0.5) + # полигон с прозрачностью 0,5 geom_line() + geom_point() + geom_text(aes(label = floor(export / 1000)), vjust = 0, nudge_y = 40000) + theme_bw() ``` ] .pull-right[ ![](06_AdvGraphics_files/figure-html/themes-out-1.png)<!-- --> ]