• 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. ed5767b1bcdbbcecfbfb136bd5fcd8cd6fc9197b
大小 17,456 字节
时间 2017-06-09 07:54:27
作者 Lorenzo Isella
Log Message

Nice code with pipe operator and several plots including colored boxplots.

Content

rm(list=ls())
library(ggplot2)
library(dplyr)
library(grid)
library(magrittr)
library(tikzDevice)
library(gridExtra)
library(scales)
library(reshape2)
library(viridis)
library(ggthemes)


source("/home/lorenzo/myprojects-hg/R-codes/stat_lib.R")

options( scipen = 16 )


df <- read.csv("data2015.csv", header=T,na.strings = c("", 0)) %>% to_char %>% filter(Last.available.year %in% c(2015,2016) ) %>% select(one_of(c("Number.of.employees.Last.avail..yr", "Number.of.employees.Year...1",
                             "Sales.th.EUR.Last.avail..yr",
                  "Sales.th.EUR.Year...1", "Cash.flow.th.EUR.Last.avail..yr",
                                            "Cash.flow.th.EUR.Year...1",
                                            "Last.available.year",
                                            "Country.ISO.Code"  )))  %>% set_colnames(c("n_emp_last","n_emp_minus1", "sales_last",
          "sales_minus1", "cash_last", "cash_minus1",
           "last_year", "country"))



df$n_emp_2015 <- NA

df$n_emp_2015[df$last_year==2015] <- df$n_emp_last[df$last_year==2015] 
df$n_emp_2015[df$last_year==2016] <- df$n_emp_minus1[df$last_year==2016] 



df$sales_2015 <- NA

df$sales_2015[df$last_year==2015] <- df$sales_last[df$last_year==2015] 
df$sales_2015[df$last_year==2016] <- df$sales_minus1[df$last_year==2016] 


df$cash_2015 <- NA

df$cash_2015[df$last_year==2015] <- df$cash_last[df$last_year==2015] 
df$cash_2015[df$last_year==2016] <- df$cash_minus1[df$last_year==2016] 

df <- df %>% select(one_of(c("country","n_emp_2015", "sales_2015", "cash_2015")))

df <- df %>% mutate(ratio=cash_2015/sales_2015)

df <- df %>% mutate(year_ben=2015)

df1 <- df


#### some density plots

gpl <- ggplot(df, aes(n_emp_2015)) +

    
    
geom_density(alpha=0.4, fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+
scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+



xlab("Number of Employees")+
 ylab("P(Number of Employees)")



fname <- paste("employment_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)








gpl <- ggplot(df, aes(y=n_emp_2015, x=country)) +

    
    
geom_boxplot()+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+



xlab(NULL)+
 ylab("Number of Employees")



fname <- paste("employment_2015_beneficiaries_box.pdf")
ggsave(fname, gpl, width=10,height=5)





##########################################################à



gpl <- ggplot(df, aes(sales_2015)) +

    
    
geom_density(alpha=0.4, fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+
scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+



xlab("Sales (thousands Euro)")+
 ylab("P(Sales)")



fname <- paste("sales_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)



#########################################################

gpl <- ggplot(df, aes(y=sales_2015, country)) +

    
    
geom_boxplot()+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+



ylab("Sales (thousands Euro)")
 ## xlab(NULL)



fname <- paste("sales_2015_beneficiaries_box.pdf")
ggsave(fname, gpl, width=10,height=5)



############################################################à

gpl <- ggplot(df, aes(cash_2015)) +

    
    
geom_density(alpha=0.4, fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+
scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+



xlab("Cash flow (thousands Euro)")+
 ylab("P(Cash flow)")



fname <- paste("cash_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)




gpl <- ggplot(df, aes(y=cash_2015, x=country)) +

    
    
geom_boxplot()+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+



xlab(NULL)+
 ylab("Cash flow (thousand Euro)")



fname <- paste("cash_2015_beneficiaries_box.pdf")
ggsave(fname, gpl, width=10,height=5)




#######################################################################à


gpl <- ggplot(df, aes(ratio)) +

    
    
geom_density(alpha=0.4, fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+
scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

coord_cartesian(xlim=c(-20,20))+

xlab("Cash flow/Sales")+
 ylab("P(Cash flow/Sales)")



fname <- paste("cash_over_sales_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)





gpl <- ggplot(df, aes(y=ratio, x=country)) +

    
    
geom_boxplot()+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

## coord_cartesian(xlim=c(-20,20))+

xlab("Cash flow/Sales")+
 ylab("P(Cash flow/Sales)")



fname <- paste("cash_over_sales_2015_beneficiaries_box.pdf")
ggsave(fname, gpl, width=10,height=5)




####################################################################


sales_agg <- aggregate(sales_2015~country, data=df, mean, na.rm=T)
emp_agg <- aggregate(n_emp_2015~country, data=df, mean, na.rm=T)
cash_agg <- aggregate(cash_2015~country, data=df, mean, na.rm=T)
ratio_agg <- aggregate(ratio~country, data=df, mean, na.rm=T)

count_ben <- table(df$country) %>% as.data.frame %>% set_colnames(c("country", "number")) %>% mutate(year_ben=2015)


##################################

ms <- sort(sales_agg$sales_2015, decreasing=T, index.return=T)

sales_agg$country <- factor(sales_agg$country, levels=sales_agg$country[ms$ix])


gpl <- ggplot(sales_agg, aes(x=country, y=sales_2015)) +
    
geom_bar(alpha=0.4,stat="identity", fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

xlab("Country")+
 ylab("Average Sales (thosands Euro)")



fname <- paste("average_sales_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)

###########################################################################



ms <- sort(emp_agg$n_emp_2015, decreasing=T, index.return=T)

emp_agg$country <- factor(emp_agg$country, levels=emp_agg$country[ms$ix])


gpl <- ggplot(emp_agg, aes(x=country, y=n_emp_2015)) +
    
geom_bar(alpha=0.4,stat="identity", fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

xlab("Country")+
 ylab("Average number of Employees")



fname <- paste("average_employees_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)



###########################################################################



ms <- sort(cash_agg$cash_2015, decreasing=T, index.return=T)

cash_agg$country <- factor(cash_agg$country, levels=cash_agg$country[ms$ix])


gpl <- ggplot(cash_agg, aes(x=country, y=cash_2015)) +
    
geom_bar(alpha=0.4,stat="identity", fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

xlab("Country")+
 ylab("Average Cash Flow (thousands Euro)")



fname <- paste("average_cash_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)




###########################################################################



ms <- sort(count_ben$number, decreasing=T, index.return=T)

count_ben$country <- factor(count_ben$country, levels=count_ben$country[ms$ix])


gpl <- ggplot(count_ben, aes(x=country, y=number)) +
    
geom_bar(alpha=0.4,stat="identity", fill="blue")+


scale_y_continuous(breaks=pretty_breaks(n=5))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

xlab("Country")+
 ylab("Number of Beneficiaries")



fname <- paste("number_beneficiaries_2015_beneficiaries.pdf")
ggsave(fname, gpl, width=10,height=5)



#######################################################################



df <- read.csv("data2016.csv", header=T,na.strings = c("", 0)) %>% to_char %>% filter(Last.avail..year %in% c(2015,2016) ) %>% select(one_of(c("Number.of.employees.Last.avail..yr", "Number.of.employees.Year...1",
                             "Sales.th.EUR.Last.avail..yr",
                  "Sales.th.EUR.Year...1", "Cash.flow.th.EUR.Last.avail..yr",
                                            "Cash.flow.th.EUR.Year...1",
                                            "Last.avail..year",
                                            "Country.ISO.Code"  )))  %>% set_colnames(c("n_emp_last","n_emp_minus1", "sales_last",
          "sales_minus1", "cash_last", "cash_minus1",
           "last_year", "country"))



df$n_emp_2015 <- NA

df$n_emp_2015[df$last_year==2015] <- df$n_emp_last[df$last_year==2015] 
df$n_emp_2015[df$last_year==2016] <- df$n_emp_minus1[df$last_year==2016] 



df$sales_2015 <- NA

df$sales_2015[df$last_year==2015] <- df$sales_last[df$last_year==2015] 
df$sales_2015[df$last_year==2016] <- df$sales_minus1[df$last_year==2016] 


df$cash_2015 <- NA

df$cash_2015[df$last_year==2015] <- df$cash_last[df$last_year==2015] 
df$cash_2015[df$last_year==2016] <- df$cash_minus1[df$last_year==2016] 

df <- df %>% select(one_of(c("country","n_emp_2015", "sales_2015", "cash_2015")))

df <- df %>% mutate(ratio=cash_2015/sales_2015)

df <- df %>% mutate(year_ben=2016)

count_ben2 <- table(df$country) %>% as.data.frame %>% set_colnames(c("country", "number")) %>% mutate(year_ben=2016)


count_all <- rbind(count_ben, count_ben2)




df_tot <- rbind(df1,df) %>% {.$year_ben <- as.factor(.$year_ben)
                                                         .}

#####################################################


lbls <- levels(df_tot$year_ben)


gpl <- ggplot(df_tot, aes(y=n_emp_2015, x=country, colour=year_ben)) +

    
    
geom_boxplot(position=position_dodge(.95))+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 and 2016 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

    scale_color_gdocs(NULL, labels=c("2015", "2016")
                     ,na.value =  "grey85"## , formatter=comma
                       )+


xlab(NULL)+
 ylab("Number of Employees")



fname <- paste("employment_2015_2016_beneficiaries_box.pdf")
ggsave(fname, gpl, width=15,height=5)



gpl <- gpl+ coord_cartesian(ylim=c(0,400))

fname <- paste("employment_2015_2016_beneficiaries_box_restricted.pdf")
ggsave(fname, gpl, width=15,height=5)







gpl <- ggplot(df_tot, aes(y=sales_2015, x=country, colour=year_ben)) +

    
    
geom_boxplot(position=position_dodge(.95))+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 and 2016 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

    scale_color_gdocs(NULL, labels=c("2015", "2016")
                     ,na.value =  "grey85"## , formatter=comma
                       )+


xlab(NULL)+
 ylab("Sales (thousands Euro)")






fname <- paste("sales_2015_2016_beneficiaries_box.pdf")
ggsave(fname, gpl, width=15,height=5)






gpl <- ggplot(df_tot, aes(y=cash_2015, x=country, colour=year_ben)) +

    
    
geom_boxplot(position=position_dodge(.95))+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 and 2016 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

    scale_color_gdocs(NULL, labels=c("2015", "2016")
                     ,na.value =  "grey85"## , formatter=comma
                       )+


xlab(NULL)+
 ylab("Cash flow (thousands Euro)")






fname <- paste("cash_2015_2016_beneficiaries_box.pdf")
ggsave(fname, gpl, width=15,height=5)




gpl <- ggplot(df_tot, aes(y=ratio, x=country, colour=year_ben)) +

    
    
geom_boxplot(position=position_dodge(.95))+


scale_y_continuous(breaks=pretty_breaks(n=5))+
## scale_x_continuous(breaks=pretty_breaks(n=8))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 and 2016 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

    scale_color_gdocs(NULL, labels=c("2015", "2016")
                     ,na.value =  "grey85"## , formatter=comma
                       )+


xlab(NULL)+
 ylab("Cash flow/Sales")






fname <- paste("cash_over_sales_2015_2016_beneficiaries_box.pdf")
ggsave(fname, gpl, width=15,height=5)


gpl <- gpl + coord_cartesian(ylim=c(-200,50))

fname <- paste("cash_over_sales_2015_2016_beneficiaries_box_restricted.pdf")
ggsave(fname, gpl, width=15,height=5)




count_all$year_ben <- as.factor(count_all$year_ben)

gpl <- ggplot(count_all, aes(x=country, y=number, fill=year_ben)) +
    
geom_bar(stat="identity", position= "dodge" ## position_dodge(1)
         )+


scale_y_continuous(breaks=pretty_breaks(n=5))+

my_ggplot_theme(c(0.13,0.8))+
theme(legend.position = 'right')+
    
labs(title="2015 and 2016 Beneficiaries")+
theme(plot.title = element_text(lineheight=.8, size=24, face="bold", vjust=1))+
theme(legend.text = element_text(vjust=1,lineheight=1 ))+
theme(legend.title = element_text(colour="black", size=16, face="bold"))+

    scale_fill_gdocs(NULL, labels=c("2015", "2016")
                     ,na.value =  "grey85"## , formatter=comma
                       )+

    
xlab("Country")+
 ylab("Number of Beneficiaries")



fname <- paste("number_beneficiaries_2015_2016_beneficiaries.pdf")
ggsave(fname, gpl, width=17,height=5)






print("So far so good")