Weltkarte - first R MarkDown document

## Linking to GEOS 3.14.1, GDAL 3.12.1, PROJ 9.7.1; sf_use_s2() is TRUE
## Loading required package: ggplot2
## 
## Attaching package: 'plotly'
## 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
## udunits database from C:/Users/ig/AppData/Local/R/win-library/4.5/units/share/udunits/udunits2.xml

Tabelle der Hauptstädte

## Reading 'ne_110m_populated_places.zip' from naturalearth...

Plot Basis

## No trace type specified:
##   Based on info supplied, a 'scatter' trace seems appropriate.
##   Read more about this trace type -> https://plotly.com/r/reference/#scatter
## No scatter mode specifed:
##   Setting the mode to markers
##   Read more about this attribute -> https://plotly.com/r/reference/#scatter-mode

Grafik ergänzen & Weltkarte erstellen

Version 1

Version 2

fig_base <- plot_ly()  |> 
  layout(
    title = list(
      text = "Hauptstaedte der Welt",
      font= list(family = "Arial", size = 18, color = "black"), y = 0.99),
    geo = list(scope = 'world', resolution = 110)
    )

fig_cities <- fig_base |> 
  add_trace(data=cities[cities$ADM0CAP==1,], type="scattergeo",
    mode = "markers", lon = ~LONGITUDE, lat = ~LATITUDE)

fig_cities

Version 3

fig_base <- plot_ly()  |> 
  layout(
    title = list(
      text = "Hauptstaedte der Welt",
      font= list(family = "Arial", size = 24, color = "black"), y = 0.99),
    geo = list(scope = 'world', resolution = 110, projection = list(type = 'orthographic'),
      visible = T, showcountries = T, countrycolor = "#4dac26", 
      showland = TRUE, landcolor = toRGB("#b8e186"),
      coastlinecolor = "#d01c8b",
      showocean = TRUE,
      oceancolor = toRGB("#f1b6da")
      )
  )

fig_cities <- fig_base |> 
  add_trace(data=cities[cities$ADM0CAP==1,], type="scattergeo",
    mode = "markers", lon = ~LONGITUDE, lat = ~LATITUDE)

fig_cities

Version 4

fig_base <- plot_ly()  |> 
  layout(
    title = list(
      text = "Hauptstädte der Welt",
      font= list(family = "Arial", size = 24, color = "black"), y = 0.99),
    geo = list(scope = 'world', resolution = 110, projection = list(type = 'robinson'),
      visible = T, showcountries = T, countrycolor = "#dadad9", 
      showland = TRUE, landcolor = toRGB("#f1f1f0"),
      coastlinecolor = "#80CDEC", showocean = TRUE, oceancolor = toRGB("#0077B6"),
      lonaxis = list(showgrid = T,
                     gridcolor = toRGB("Black", alpha=0.1),
                     dtick=30),
      lataxis = list(showgrid = T,
                     gridcolor = toRGB("Black", alpha=0.1),
                     dtick=30)
    )
  )

fig_cities <- fig_base |> 
  add_trace(data=cities[cities$ADM0CAP==1,], type="scattergeo",
    mode = "markers", lon = ~LONGITUDE, lat = ~LATITUDE)

fig_cities

Kartengestaltung, Was können wir anpassen

Unterdrückung von trace_0 mit

fig_cities <- fig_base |>
  add_trace(
    data=cities[cities$ADM0CAP==1,], type="scattergeo",
    mode = "markers", lon = ~LONGITUDE, lat = ~LATITUDE,
    text = ~sprintf("%s (%s)", NAME, SOV_A3),
    hovertemplate = paste('%{text}<extra></extra>'),
    marker=list(size=(sqrt(cities$POP_MAX)/500),
                color="#c994c7", 
                line=list(color='#dd1c77', width = 1, opacity = 0))
  )

fig_cities
## Coordinate Reference System:
##   User input: WGS 84 
##   wkt:
## GEOGCRS["WGS 84",
##     DATUM["World Geodetic System 1984",
##         ELLIPSOID["WGS 84",6378137,298.257223563,
##             LENGTHUNIT["metre",1]]],
##     PRIMEM["Greenwich",0,
##         ANGLEUNIT["degree",0.0174532925199433]],
##     CS[ellipsoidal,2],
##         AXIS["latitude",north,
##             ORDER[1],
##             ANGLEUNIT["degree",0.0174532925199433]],
##         AXIS["longitude",east,
##             ORDER[2],
##             ANGLEUNIT["degree",0.0174532925199433]],
##     ID["EPSG",4326]]
## Spherical geometry (s2) switched off
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## 2.417e+09 4.619e+10 1.850e+11 8.326e+11 6.219e+11 1.702e+13
##      Min.   1st Qu.    Median      Mean   3rd Qu.      Max. 
## 3.600e-04 2.480e+01 6.929e+01 1.171e+02 1.249e+02 1.219e+03
country_data = countries  |> 
  st_drop_geometry() |> 
  rename(COUNTRY = admin, CODE = adm0_a3, VALUE = mapcolor7, POP2019=pop_est, AREA=area, POPDENS=popdensity) |> 
  select(COUNTRY, CODE, VALUE, POP2019, AREA, POPDENS) |> 
  arrange(COUNTRY)

country_data$POP2019Mio <- round(country_data$POP2019/10^6, digits=2)
DT::datatable(country_data, options = list(pageLength = 5))
geojson_countries = geojsonsf::sf_geojson(countries) |> 
  jsonlite::fromJSON(simplifyVector = F,  
                     simplifyDataFrame = F, 
                     simplifyMatrix = F)
fig_countries <- fig_cities |>
  add_trace(
    data=country_data,
    type="choropleth",
    geojson=geojson_countries,
    featureidkey = "properties.adm0_a3",
    locations=~CODE,
    z = ~VALUE
  )

fig_countries
save.image('01_Data/weltkarte_data.rdata')