library(eurostat)
library(tidyverse)
library(vroom)
library(DT)
library(sf)
library(tmap)
library(kableExtra)
library(formattable)
library(gganimate)
library(plotly)
library(echarts4r)
library(gganimate)
# Load GDP

gdp<- get_eurostat("nama_10r_2gdp",time_format="num")
 
NUTS2016 <- readxl::read_xlsx("NUTS2016.xlsx") %>% 
  filter(NUTS!="3") %>% 
  mutate_if(is.character,as.factor) %>% 
  mutate(NUTS=as.factor(NUTS)) %>% 
  filter(!Country %in% c("UK","IS","NO","CH","LI","ME","MK","RS","TR","AL"))


release<- left_join (NUTS2016, gdp) 

Table with DT and formattable, GDP per capita in PPS 2018 as % EU27, 2018

temp<-release %>% 
  filter(time >= 2018 &  unit=="PPS_HAB_EU27_2020") %>% 
  select(Country,NUTS,geo,label,values) %>%  
  na.omit() %>% 
  group_by(NUTS) %>% 
  mutate(rank=round(rank(desc(values)))) %>% 
  ungroup() %>% 
  droplevels()


as.datatable(formattable(temp, list(values = color_bar("lightblue"))),  filter = "top", class = "stripe hover", extensions = "Buttons",
             options = list(  lengthMenu = list(c(20,50,200, -1), c("20","50","200", "All")),
                              pageLength = 50, autoWidth = TRUE,  dom = "Blfrtip", buttons = c("excel")),
             rownames= FALSE
)  

Maps with tmap

GDP per capita PPS % EU27, 2018

load(file="nuts_map.Rdata")

sf<- left_join (nuts, release) 

sf<- sf %>% 
  filter(!CNTR_CODE %in% c("UK","IS","NO","CH","LI"))

tmap_mode("view")
sf %>% filter(LEVL_CODE=="2" & time==2018  & unit=="PPS_HAB_EU27_2020")  %>% 
tm_shape() +
tm_fill("values", popup.vars = c("values","NUTS_ID","NUTS_NAME"), palette="RdBu", style="quantile",  title="GDP per capita in PPS as % EU average, 2018")+
  tm_borders()

GDP per capita EUR

sf %>% filter(LEVL_CODE=="2" & time==2018  & unit=="EUR_HAB")  %>% 
tm_shape() +
tm_fill("values", popup.vars = c("values","NUTS_ID","NUTS_NAME"), palette="RdBu", style="quantile",title="GDP per capita in EUR, 2018")+
  tm_borders()

Analytical Chart with echarts4r, showing GDP per capita amd GDP in PPS, 2018

temp1<-release %>% 
  filter(time==2018 &  unit%in% c("PPS_HAB_EU27_2020", "MIO_PPS_EU27_2020")) %>%
  spread(unit,values) %>% 
  na.omit() %>%
  droplevels()

my_scale <- function(x){
  scales::rescale(x, to = c(8, 30))
}
temp1 %>% filter(NUTS=="2") %>% 
  group_by(Country) %>% 
  e_charts(label) %>% #, timeline = TRUE 
  e_scatter(PPS_HAB_EU27_2020,MIO_PPS_EU27_2020,  scale = my_scale) %>% 
  e_datazoom(x_index = 0, type = "slider") %>% 
  e_datazoom(y_index = 0, type = "slider") %>% 
  e_tooltip(trigger= "axis") 

Same chart with ggplotly

p<- ggplot(temp1 %>% filter(NUTS=="2" ),aes(PPS_HAB_EU27_2020, Country,size= MIO_PPS_EU27_2020, colour=Country, label=geo))+
  geom_jitter()+
  scale_size(range = c(0.5, 8))+
  theme_minimal()+
  theme(legend.position = "none")+
  xlab("")+ylab("")+
  scale_colour_viridis_d()+
  scale_x_continuous(breaks = scales::pretty_breaks(n=5))+
  scale_y_discrete(limits = rev(levels(temp1$Country)))+
  theme(panel.grid.major.x = element_blank())

ggplotly(p, dynamicTicks = TRUE, tooltip=c("Country","label","geo","PPS_HAB_EU27_2020","MIO_PPS_EU27_2020")) 

Animated with gganimate, 2000-2018 evolution GDP per capita in PPS

temp1<-release %>% 
  filter(unit%in% c("PPS_HAB_EU27_2020", "MIO_PPS_EU27_2020")) %>%
  spread(unit,values) %>% 
  na.omit() %>%
  group_by(NUTS) %>% 
  droplevels()

ggplot(temp1 %>% filter(NUTS=="2"),aes(PPS_HAB_EU27_2020, Country,size= MIO_PPS_EU27_2020, colour=Country, label=geo))+
  geom_point()+
  scale_size(range = c(0.5, 8))+
  theme_minimal()+
  theme(legend.position = "none")+
  xlab("")+ylab("")+
  scale_colour_viridis_d()+
  scale_x_continuous(breaks = scales::pretty_breaks(n=5))+
  scale_y_discrete(limits = rev(levels(temp1$Country)))+
  theme(panel.grid.major.x = element_blank())+
labs(subtitle = "{closest_state}") +  
  transition_states(states = time, 
                    transition_length = 2,
                    state_length = 2) +
  shadow_wake(wake_length = 0.5)

Same chart with transitions and control using plotly

  p <-plot_ly(temp1,
  x = ~Country, y = ~ PPS_HAB_EU27_2020,
  color = ~Country, size = ~MIO_PPS_EU27_2020,
  frame = ~time,   text = ~label,
    hoverinfo = "geo",
     type = "scatter",
     mode = "markers"
) 

p <- p %>% 
  animation_opts(
    1000, easing = "elastic", redraw = FALSE
  )
p
## Time series one country
temp1<-release %>% 
  filter(unit%in% c("PPS_HAB_EU27_2020")) %>%
  spread(unit,values) %>% 
  na.omit() %>%
  group_by(NUTS) %>% 
  droplevels()

ggplot(data=temp1 %>% filter( Country =="SK" & NUTS %in% c("0","2")), aes(time, PPS_HAB_EU27_2020, colour=geo, linetype=NUTS))+
  geom_point(size=4)+
  geom_line( size=1.5)+
    theme_minimal()+
     theme(legend.position = "none")+
   xlab("")+ylab("")+
  scale_colour_brewer(palette ="Set3")+
  scale_x_continuous(breaks = c(2000,2006,2012,2018))+
  scale_y_continuous(breaks = scales::pretty_breaks(n=5))+
  theme(panel.grid.major.x = element_blank())+
  transition_reveal(along= time)