• R/O
  • SSH

标签
No Tags

Frequently used words (click to add to your profile)

javac++androidlinuxc#windowsobjective-ccocoa誰得qtpythonphprubygameguibathyscaphec計画中(planning stage)翻訳omegatframeworktwitterdomtestvb.netdirectxゲームエンジンbtronarduinopreviewer

File Info

Rev. c198afa4ce92a530891334fddc93b6506436d579
大小 14,416 字节
时间 2021-03-18 05:21:13
作者 Lorenzo Isella
Log Message

Improvements to the bar plot and to the report.

Content

---
output: word_document
---


```{r, scoreboard, echo=FALSE, eval=TRUE}

options(tidyverse.quiet = TRUE)
suppressWarnings(suppressMessages(library(janitor)))
suppressWarnings(suppressMessages(library(viridis)))
suppressWarnings(suppressMessages(library(scales)))
suppressWarnings(suppressMessages(library(treemap)))

library(tidyverse, warn.conflicts = FALSE)
library(janitor)
library(viridis)
library(scales)
library(treemap)

iso_map <- tibble(iso3=c("AUT", "BEL", "BGR", "CYP", "CZE", "DEU", "DNK", "ESP", "EST", "FIN",
"FRA", 
"GRC", "HRV", "HUN", "IRL", "ITA", "LTU", "LUX", "LVA", "MLT",
"NLD", "POL", "PRT",
"ROM", "SVK", "SVN", "SWE"),
country=c("Austria", "Belgium", "Bulgaria", "Cyprus", "Czech Republic", "Germany",
  "Denmark", "Spain", "Estonia", "Finland", "France", "Greece",
  "Croatia", "Hungary", "Ireland","Italy", "Lituania", "Luxembourg",
  "Latvia", "Malta", "Netherlands", "Poland", "Portugal",
  "Romania", "Slovakia", "Slovenia", "Sweden")
)


# Suppress summarise info
options(dplyr.summarise.inform = FALSE)

'%!in%' <- function(x,y)!('%in%'(x,y))


my_ggplot_theme2 <- function(legend_coord){
    theme_bw()+

    theme(## legend.title = element_text(vjust=1,lineheight=1, size=14 ),

        legend.title = element_text(vjust = 1,lineheight=1,
                                    size=14, margin = margin(t = 4.5)),
        legend.spacing.y = grid::unit(3, "pt"),
        legend.text.align = 0.5,
          
          panel.grid.minor = element_blank(),
          plot.title = element_text(lineheight=.8, size=24, face="bold",
          vjust=1),legend.text = element_text(vjust=.4,lineheight=1,size = 14 ),
          axis.title.x = element_text(size = 20, vjust=1),
                axis.title.y = element_text(size = 20, angle=90, vjust=1),
                axis.text.x = element_text(size=15, colour="black", vjust=1),
          axis.text.y = element_text(size=15, colour="black", hjust=1),
                          legend.position = legend_coord,
           strip.background = element_rect(colour = 'blue',
                           fill = 'white', size = 1, linetype=1),
                strip.text.x = element_text(colour = 'red', angle = 0,
                                     size = 12, hjust = 0.5,
                    vjust = 0.5, face = 'bold'),
                   strip.text.y = element_text(colour = 'red', angle = 0,
                    size = 12, hjust = 0.5,
                    vjust = 0.5, face = 'bold'),
     
          )
}




round_preserve_sum <- function(x, digits = 0) {
  up <- 10^digits
  x  <-  x * up
  y <-  floor(x)
  indices <-  tail(order(x-y), round(sum(x, na.rm=T)) - sum(y, na.rm=T))
  y[indices] <-  y[indices] + 1
  y / up
}


##a function to move a row in a data frame.

move_row <- function(df, ini_pos, fin_pos){

ll <- nrow(df)
    
row_pick <- slice(df, ini_pos)

    if (fin_pos=="last"){

           res <- df %>%
        slice(-ini_pos) %>% 
        add_row(row_pick, .before = ll)    
 
        
} else{
    
    res <- df %>%
        slice(-ini_pos) %>% 
        add_row(row_pick, .before = fin_pos)    
}

    return(res)
}





MS <- "FRA"

ms3 <- iso_map %>%
    filter(iso3==MS) %>%
    pull(country) %>%
    as.character




scoreboard <- readRDS("../scoreboard.RDS")

scoreboard_ms <- scoreboard %>%
    filter(member_state==MS)

year_focus <- scoreboard_ms %>%
    pull(year) %>%
    max()


ini_focus <- year_focus-9


cases <- scoreboard_ms %>%
    filter(year==max(year),
           ## duration_end>=max(year),
           amount_spent_aid_element_in_eur_million>0
           )%>% 
    select(member_state,case_no, year,amount_spent_aid_element_in_eur_million,
           procedure_name) %>%
    ## filter(grepl("SA.", case_no)) %>%
    distinct(case_no,.keep_all =T )

n_cases <- nrow(cases)

stat_cases <- cases %>%
    tabyl(procedure_name) %>%
    mutate(percent=round(percent*100,1))


old_cases <- scoreboard %>%
    filter(year>=ini_focus, year!= year_focus,  member_state==MS)

new_cases_list <- setdiff(cases$case_no, old_cases$case_no)

cases_new <- scoreboard_ms %>%
    filter(case_no %in% new_cases_list ,
           ## duration_end>=max(year),
           amount_spent_aid_element_in_eur_million>0
           )%>% 
    select(member_state,case_no, year,amount_spent_aid_element_in_eur_million,
           procedure_name) %>%
    ## filter(grepl("SA.", case_no)) %>%
    distinct(case_no,.keep_all =T )

stat_cases_new <- cases_new %>%
    tabyl(procedure_name) %>%
    mutate(percent=round(percent*100,1))




stat_cases2 <- scoreboard %>%
    filter(year>=ini_focus, member_state==MS)  %>%
    group_by(year, procedure_name) %>%
    summarise(expenditure=sum(amount_spent_aid_element_in_eur_million/1e3)) %>%
    ungroup 


stat_cases3 <- scoreboard %>%
    filter(year>=ini_focus, member_state==MS)  %>%
    mutate(procedure2=if_else(procedure_name=="Non (G)BER", procedure_name, "(G)BER")) %>% 
    group_by( procedure2) %>%
    summarise(expenditure=sum(amount_spent_aid_element_in_eur_million/1e3)) %>%
    ungroup  %>%
    mutate(expenditure=round(expenditure, 2))



top5 <- cases  %>%
    group_by(case_no) %>%
    summarise(expenditure=sum(amount_spent_aid_element_in_eur_million)) %>%
    ungroup %>% 
    arrange(desc(expenditure)) %>%
    mutate(share=expenditure/sum(expenditure)) %>%
    slice(1:5) %>%
    mutate(cum_share=cumsum(share),
           cum_expenditure=cumsum(expenditure))


top5_share <- top5 %>%
    pull(cum_share) %>%
    max() %>%
    "*"(100) %>%
    round(1)


top5_expenditure <- top5 %>%
    pull(cum_expenditure) %>%
    max()




total_expenditure <- scoreboard_ms %>%
    filter(year>=ini_focus) %>% 
    pull(amount_spent_aid_element_in_eur_million) %>%
    sum(na.rm=T) %>% "/"(1e3) %>%
    round(2)

total_expenditure_last <- scoreboard_ms %>%
    filter(year==year_focus) %>% 
    pull(amount_spent_aid_element_in_eur_million) %>%
    sum(na.rm=T) %>% "/"(1e3) %>%
    round(2)


cofinancing <- scoreboard_ms %>%
    filter( year==max(year),
           ## duration_end>=max(year),
           amount_spent_aid_element_in_eur_million>0
           ) %>%
    mutate(expenditure=amount_spent_aid_element_in_eur_million*
               co_financed)





cofinancing_expenditure <- cofinancing %>%
    pull(expenditure) %>%
    sum %>%
    round(0)


share_cofinancing <- round(cofinancing_expenditure/1e3/total_expenditure_last
                           *100,0)


cofinancing_objective <- cofinancing %>%
    group_by(scoreboard_objective) %>%
    summarise(expenditure_objective=sum(expenditure)) %>%
    ungroup %>%
    arrange(desc(expenditure_objective)) %>%
    mutate(share=expenditure_objective/sum(expenditure_objective)*100) %>%
    mutate(share=round_preserve_sum(share,0))




expenditure_by_objective <- scoreboard_ms %>%
    filter(year==max(year),
           ## duration_end>=max(year),
           amount_spent_aid_element_in_eur_million>0
           )%>%
    mutate(scoreboard_objective=recode(scoreboard_objective,
             "Other"="Other policy objectives"       )) %>% 
    group_by(scoreboard_objective) %>%
    summarise(expenditure_objective=sum(amount_spent_aid_element_in_eur_million)) %>%
    ungroup %>%
    arrange(desc(expenditure_objective)) %>%    
    mutate(objectives_reduced=if_else(scoreboard_objective %in% scoreboard_objective[1:4], scoreboard_objective, "Other policy objectives" )) %>%
    group_by(objectives_reduced) %>%
    summarise(expenditure2=sum(expenditure_objective)) %>%
    ungroup %>%
    arrange(desc(expenditure2)) %>%
    mutate(share=expenditure2/sum(expenditure2)) %>%
    mutate(share2=round_preserve_sum(share*100,0))  %>%
    mutate(share_label=paste(as.character(share2), "%", sep="")) %>%
    move_row(which(.$objectives_reduced=="Other policy objectives"), nrow(.)) %>%
    ## mutate(objectives_reduced=wrapper1(objectives_reduced, 25)) %>% 
    mutate(objectives_reduced=factor(objectives_reduced)) %>%
    mutate(objectives_reduced=fct_inorder(objectives_reduced))

expenditure_gber <- scoreboard_ms %>%
    filter(year==max(year),
           ## duration_end>=max(year),
           amount_spent_aid_element_in_eur_million>0
           )%>%
    filter(!is.na(all_objective_names_gber_only)) %>% 
    group_by(all_objective_names_gber_only) %>%
    summarise(expenditure=sum(amount_spent_aid_element_in_eur_million)) %>%
    ungroup %>%
    arrange(desc(expenditure)) %>%
    mutate(share=expenditure/sum(expenditure)) %>%
    mutate(share2=round_preserve_sum(share*100,0))


expenditure_aid <- scoreboard_ms %>%
    filter(year==max(year),
           ## duration_end>=max(year),
           amount_spent_aid_element_in_eur_million>0
           )%>%
    filter(!is.na(aid_instrument_name))  %>%
    group_by(aid_instrument_name) %>%
    summarise(expenditure=sum(amount_spent_aid_element_in_eur_million)) %>%
    ungroup %>%
    arrange(desc(expenditure)) %>%
    mutate(share=expenditure/sum(expenditure)) %>%
    mutate(share2=round_preserve_sum(share*100,0)) %>%
    mutate(expenditure2=round(expenditure,0))

```



---
title: "Country Focus on `r ms3` in the Year `r year_focus`."
---



# Case and Procedural Information
The total number of active measures corresponded to `r n_cases` in
`r year_focus` of which `r stat_cases$n[2]` GBER (X), `r stat_cases$n[3]`
notified (N) and `r stat_cases$n[1]` BER.

In `r year_focus`, the share of GBER measures in `r ms3` reached 
`r stat_cases$percent[2]`% of the total, with 
`r stat_cases_new$percent[1]`% of all newly implemented measured falling
under GBER.

# State Aid Spending - Overview
Between `r ini_focus` and  `r year_focus` `r ms3` spent 
`r total_expenditure` billion EUR for non-agricultural State aid, of
which around `r stat_cases3$expenditure[2]` billion EUR under notified
measures and around `r stat_cases3$expenditure[1]` billion EUR under
BER and the 2008 and 2014 GBER.



```{r, echo=FALSE,fig.height = 6, fig.width = 12}


my_pal <- viridis(length(stat_cases2 %>% pull(procedure_name) %>%
                        unique)+1)[1:3]



ggplot(data = stat_cases2, aes(x  = year, y=expenditure,
                                      fill=procedure_name)) +
    geom_bar(position=position_dodge2(preserve="single"), stat="identity", alpha=1, color="black")+
    ## scale_fill_viridis("Vehicle Brand\nOrigin",breaks=mybreaks, labels= mylabels, discrete=T)+
    ##     scale_colour_viridis("Vehicle Brand\nOrigin",breaks=mybreaks, labels= mylabels, discrete=T)+

    scale_fill_manual(NULL, ## labels=c("Inward Stocks","Outward Stocks" ),
                         values=my_pal)+


    ## coord_cartesian(ylim = c(0, 20)) +
    my_ggplot_theme2("top")+
## theme(axis.text.x = element_text(size=15,angle=90, colour="black", vjust=1))+

    labs(title=paste("State Aid Spending in Billion EUR ",ini_focus, "-",
                     year_focus, sep=""))+
    scale_x_continuous(breaks=seq(ini_focus, year_focus))+
## coord_cartesian(ylim=c(0,8000))+ 
##     scale_y_continuous(sec.axis = sec_axis(~./norm_in, labels=mypercentlatex,
##                      name="Percentage of\nTotal Extra EU28",
##                                            breaks=seq(0, 1, by=0.2)))+
## scale_y_continuous(labels=mypercentlatex)+
    
    xlab(NULL)+
 ylab("State Aid Expenditure")
```


In `r year_focus`, State aid spending for the 5 biggest State aid
 measures in `r ms3` absorbed `r top5_share`% of the total spending
 (around `r total_expenditure_last` billion EUR).
 
 Finally, the amount of co-financed in `r ms3` corresponded to
 `r cofinancing_expenditure` million EUR (around
 `r share_cofinancing`% of the total non-agricultural spending)and
 was mostly concentrated in
 `r cofinancing_objective$scoreboard_objective[1]`
 (`r cofinancing_objective$share[1]`%),
  `r cofinancing_objective$scoreboard_objective[2]`
 (`r cofinancing_objective$share[2]`%) and
  `r cofinancing_objective$scoreboard_objective[3]`
 (`r cofinancing_objective$share[3]`%).


 
# State Aid Spending - Top Objectives and Instruments


```{r, echo=FALSE,fig.height = 6, fig.width = 12}
ggplot(data = expenditure_by_objective,
              aes(x  = share, y=fct_rev(objectives_reduced))) +
    geom_bar(position="dodge", stat="identity", alpha=1, color="black") +
    geom_text(aes(label=share_label), position=position_dodge(width=0.9),
              hjust=-.1,
              vjust=-0.1,
              size=5)+
    coord_cartesian(xlim = c(0, 1)) +
    my_ggplot_theme2("top")+
    scale_x_continuous(labels = label_percent())+
    scale_y_discrete(labels = wrap_format(20))+    
    xlab("Share of State Aid Expenditure")+
    ylab(NULL)


```


 Around `r sum(expenditure_by_objective$share2[1:2])`% of State aid
 spending in `r ms3` was concentrated in two main policy objectives. Around
`r expenditure_by_objective$share2[1]`% was directed towards
"`r expenditure_by_objective$objectives_reduced[1]`" while
`r expenditure_by_objective$share2[2]`%  to
"`r expenditure_by_objective$objectives_reduced[2]`".

`r ms3` devoted around
`r expenditure_by_objective$share2[3]`% towards
"`r expenditure_by_objective$objectives_reduced[3]`" and
`r expenditure_by_objective$share2[4]`% to
"`r expenditure_by_objective$objectives_reduced[4]`".


```{r, echo=FALSE,fig.height = 8, fig.width = 12}

treemap(expenditure_gber,index= "all_objective_names_gber_only", "expenditure", title="", draw=T)

```


The top 4 key articles absorbe about
`r sum(expenditure_gber$share2[1:4])`%
of the total GBER spending.
The most widely used is
"`r expenditure_gber$all_objective_names_gber_only[1]`", 
(`r expenditure_gber$share2[1]`%),
followed by
"`r expenditure_gber$all_objective_names_gber_only[2]`", 
(`r expenditure_gber$share2[2]`%),
"`r expenditure_gber$all_objective_names_gber_only[3]`", 
(`r expenditure_gber$share2[3]`%),
"`r expenditure_gber$all_objective_names_gber_only[4]`", 
(`r expenditure_gber$share2[4]`%).

In terms of State aid instruments, `r ms3` privileged the use of 
"`r expenditure_aid$aid_instrument_name[1]`" (around 
`r expenditure_aid$expenditure2[1]` million EUR, 
`r expenditure_aid$share2[1]`% of total State aid spending), 
followed by
"`r expenditure_aid$aid_instrument_name[2]`" 
(`r expenditure_aid$expenditure2[2]` million EUR, 
`r expenditure_aid$share2[2]`% of total State aid spending), 
and 
"`r expenditure_aid$aid_instrument_name[3]`" (around 
`r expenditure_aid$expenditure2[3]` million EUR, 
`r expenditure_aid$share2[3]`% of total State aid spending).