Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
library(tidyverse) library(giscoR) library(ggiraph) germany_districts <- gisco_get_nuts( year = "2021", nuts_level = 3, epsg = 3035, country = 'Germany' ) |> as_tibble() |> janitor::clean_names() germany_states <- gisco_get_nuts( year = "2021", nuts_level = 1, epsg = 3035, country = 'Germany' ) |> as_tibble() |> janitor::clean_names() ggplt <- germany_districts |> ggplot(aes(geometry = geometry)) + geom_sf( data = germany_states, aes(fill = nuts_name), color = 'black', linewidth = 0.5 ) + geom_sf_interactive( fill = NA, aes( data_id = nuts_id, tooltip = glue::glue('{nuts_name}') ), linewidth = 0.1 ) + theme_void() + theme( legend.position = 'none' ) girafe(ggobj = ggplt)
library(sf) state_nmbrs <- map_dbl( germany_districts$geometry, (x) { map_lgl( germany_states$geometry, (y) st_within(x, y) |> as.logical() ) |> which() } ) germany_districts_w_state <- germany_districts |> mutate( state = germany_states$nuts_name[state_nmbrs] ) ggplt <- germany_districts_w_state |> ggplot(aes(geometry = geometry)) + geom_sf( data = germany_states, aes(fill = nuts_name), color = 'black', linewidth = 0.5 ) + geom_sf_interactive( fill = NA, aes( data_id = nuts_id, tooltip = glue::glue('{nuts_name}<br>{state}') ), linewidth = 0.1 ) + theme_void() + theme( legend.position = 'none' ) girafe(ggobj = ggplt)
Nicer tooltip and nicer colors
make_nice_label <- function(nuts_name, state) { nuts_name_label <- htmltools::span( nuts_name, style = htmltools::css( fontweight = 600, font_family = 'Source Sans Pro', font_size = '32px' ) ) state_label <- htmltools::span( state, style = htmltools::css( font_family = 'Source Sans Pro', font_size = '20px' ) ) glue::glue('{nuts_name_label}<br>{state_label}') } ggplt <- germany_districts_w_state |> mutate( nice_label = map2_chr( nuts_name, state, make_nice_label ) ) |> ggplot(aes(geometry = geometry)) + geom_sf( data = germany_states, aes(fill = nuts_name), color = 'black', linewidth = 0.5 ) + geom_sf_interactive( fill = NA, aes( data_id = nuts_id, tooltip = nice_label ), linewidth = 0.1 ) + theme_void() + theme( legend.position = 'none' ) + scale_fill_manual( values = c("#A0CBE8FF", "#F28E2BFF", "#FFBE7DFF", "#59A14FFF", "#8CD17DFF", "#B6992DFF", "#F1CE63FF", "#499894FF", "#86BCB6FF", "#E15759FF", "#FF9D9AFF", "#79706EFF", "#BAB0ACFF", "#D37295FF", "#FABFD2FF", "#B07AA1FF", "#D4A6C8FF", "#9D7660FF", "#D7B5A6FF") ) girafe(ggobj = ggplt)
R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you’re looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don’t.
Continue reading: Three Ways to Include Images in Your ggplots
Analysis and Future Implications
The text includes R programming code for generating a visual representation – a geospatial map – of Germany’s districts (NUTS 3 level) and states (NUTS 1 level). This visualization uses library functions from the tidyverse, giscoR, ggiraph, and sf packages of R. The significant features added in this visualization are customized labels and color schemes, and an interactive element where viewers can see specific district and state details during mouse hover operations.
Long-term implications of leveraging such data visualization techniques are wide-ranging. Use cases can span from public administration and planning to business and market analysis. For example, having an interactive, detailed map could help authorities to manage infrastructure projects more effectively. Businesses could use such visualizations to analyze aspects such as market distribution or logistics planning.
Future Developments
While displaying district and state names on mouse hover are useful, future implementations can include more specific details like population, economic indicators, transportation facilities, and other relevant data. Such displays can provide users with a vast array of information about each location.
Interactive maps can also incorporate time-series data so that users can see how certain factors have changed over time in each area. For instance, a map might reveal how the population or average income in a specific district has grown over the years.
Actionable Advice
Overall, it’s essential to realize the potential uses of such interactive programming functions and consider implementing them in future data analysis processes. Key advice for leveraging these insights includes:
- Include More Detailed Information: Consider including more precise data in your geom_sf_interactive feature, such as demographic statistics or economic indicators for enhanced analytical depth.
- Implement Time Series Data: Introduce time-series data into your map to offer viewers an insight into how each area has developed over time.
- Consider Color Selection Carefully: Colors play a significant role in data visualization. Choose colors that will ensure the data is clear and easy to understand.
- Test Your Visualization: Always test your visualization on a variety of devices to ensure interactivity is working correctly.