library(tidyverse)
# install.packages("eurostat")
library(eurostat)
library(sf)
library(countrycode)
To cite Federica’s work please use:
Gazzelloni F. (2023), Data Visualization: Eurosat hlth_hlye data
<- search_eurostat("Healthy life years by sex")
id <- id[1,]
id
id# save(id,file="id.RData")
<- get_eurostat("hlth_hlye",unit="PC")
data # save(data,file="data.RData")
<- data%>%
df filter(indic_he=="HLY_PC_0")%>%
mutate(year=year(time))%>%
select(geo, year,sex,values)%>%
mutate(sex=case_when(sex=="F"~"Female",
=="M"~"Male",
sexTRUE~"Both"))
%>%head df
<- unique(df$geo) my_countries_abbr
# countrycode::codelist%>%names
<- countrycode::codelist %>%
my_countries count(country.name.en, iso2c) %>%
filter(iso2c %in% my_countries_abbr)%>%
rename(region=country.name.en)
<- unique(my_countries$region) my_countries_names
<- df%>%
df_countries inner_join(my_countries,by=c("geo"="iso2c"))
%>%head df_countries
<- map_data("world") %>%
df_geometry filter(region %in% my_countries_names) %>%
group_by(group) %>%
::st_as_sf(coords = c(1, 2), crs = 4326) %>%
sfsummarise(geometry = st_combine(geometry)) %>%
st_cast("POLYGON")
%>% head df_geometry
<- rnaturalearth::ne_countries(returnclass = "sf")
polygons
ggplot()+
geom_sf(data=polygons)+
geom_sf(data=df_geometry,aes(geometry=geometry),color="red")
<- map_data("world") %>%
df_coords filter(region %in% my_countries_names)%>%
select(region,group)
<- df_geometry%>%
df_full inner_join(df_coords,by="group") %>%
merge(df_countries,by="region")
<- df_full%>%
austria filter(region=="Austria",year==2020)
<- rnaturalearth::ne_countries(scale = 110,
polygons type = 'map_units',
returnclass = 'sf')
ggplot()+
geom_sf(data=polygons)+
geom_sf(data=df_geometry,aes(geometry=geometry),fill="beige")+
geom_sf(data=austria,aes(geometry=geometry,fill=values))+
coord_sf()
Zooming in
source: https://www.r-bloggers.com/2019/04/zooming-in-on-maps-with-sf-and-ggplot2/
# polygons%>%View
30]<- "England"
my_countries_names[31]<- "N. Ireland"
my_countries_names[32]<- "Scotland"
my_countries_names[33]<- "Wales"
my_countries_names[<- polygons[polygons$name %in% my_countries_names,]
eu ggplot() + geom_sf(data = eu) + theme_bw()
<- df_full%>%
eu_qualy filter(year==2020)
<- ggplot()+
map geom_sf(data=eu)+
geom_sf(data=df_geometry,aes(geometry=geometry),fill="beige")+
geom_sf(data=eu_qualy,aes(geometry=geometry,fill=values))+
coord_sf()+
facet_wrap(~sex)
map
+
map scale_fill_gradient(low = "white",high = "#a60845") +
labs(title="Healthy life years by sex (2020) %",
caption="DataSource: Eurosat - Healthy life years by sex\n#30DayChartChallenge day18 Eurosat | DataViz: Federica Gazzelloni") +
::theme_map(base_size = 12, base_family = "Roboto Condensed")+
ggthemestheme(strip.background = element_rect(color = "#a60845",fill="#a60845"),
strip.text = element_text(color="white"),
legend.position = "bottom",
plot.title = element_text(color="#a60845",size=20),
plot.caption = element_text(color="#a60845",size=12))
ggsave("map.png",width = 7,height = 5)