Data visualization con ggplot2
Contributo dell’Agricoltura, del settore Servizi e dell’Industria al Prodotto interno lordo (Top 10 - Countries)
Fabio Paderi - 15/02/2018
Carichiamo e prepariamo i dati
library(reshape2)
library(ggplot2)
library(dplyr)
library(knitr)
library(kableExtra)
library(plotly)
pil <- read.csv("~/Desktop/Total GDP 2015 Top 10.csv", header = TRUE)
names(pil) <-c("Paese", "2010","2011","2012","2013","2014","2015")
pil_long <- melt(pil, id = "Paese")
names(pil_long) <- c("Paese", "Anno", "pil_usd_trillion")
p = pil_long %>%
group_by(Paese) %>%
summarise(media_pil_2010_2015 = mean(pil_usd_trillion, na.rm = TRUE)) %>%
arrange(desc(media_pil_2010_2015))
kable(p) %>%
kable_styling(bootstrap_options = "striped", full_width = F, position = "left")
| Paese | media_pil_2010_2015 |
|---|---|
| United States | 16.433333 |
| China | 8.783333 |
| Japan | 5.166667 |
| Germany | 3.616667 |
| France | 2.708333 |
| United Kingdom | 2.695000 |
| Brazil | 2.323333 |
| India | 1.886667 |
| Canada | 1.731667 |
| Australia | 1.403333 |
Visualizziamo l’andamento del PIL
a = ggplot(pil_long, aes(x = Anno, y = pil_usd_trillion, group = Paese)) +
geom_line(aes(col = Paese)) +
geom_point(aes(col = Paese), size = 2) +
theme_minimal() +
scale_color_manual(values = RColorBrewer::brewer.pal(10, "Set3") ) +
xlab("Year") +
ylab("PIL (in trillion USD)") +
ggtitle("Prodotto Interno Lordo (PIL) - Top 10 Countries")
ggplotly(a)
Contributo dell’Agricoltura dal 1960
agri <-read.csv("~/Desktop/Agriculture - Top 10 Country.csv")
agri_long <-melt(agri, id ="Country")
names(agri_long) <-c("Paese", "Anno", "Agri_Perc")
agri_long$Anno <- as.numeric(gsub("X", "", agri_long$Anno))
b = ggplot(agri_long, aes(x = Anno, y = Agri_Perc, group = Paese)) +
geom_line(aes(col = Paese)) +
geom_point(aes(col = Paese), size = 2) +
scale_color_manual(values = RColorBrewer::brewer.pal(10, "Set3") ) +
theme_minimal() +
scale_x_continuous(breaks= seq(1960, 2015, 5)) +
xlab("Anno") +
ylab("Contributo in % dell'Agricoltura al PIL") +
ggtitle("Contributo in % dell'Agricoltura al PIL - Top 10 Countries")
ggplotly(b)
Contributo del settore Servizi dal 1960
servizi <-read.csv("~/Desktop/Services - Top 10 Country.csv")
serv_long <-melt(servizi, id ="Country")
names(serv_long) <-c("Paese", "Anno", "Serv_Perc")
serv_long$Anno <- as.numeric(gsub("X", "", serv_long$Anno))
d = ggplot(serv_long, aes(x = Anno, y = Serv_Perc, group = Paese)) +
geom_line(aes(col = Paese)) +
geom_point(aes(col = Paese), size = 2) +
scale_color_manual(values = RColorBrewer::brewer.pal(10, "Set3") ) +
theme_minimal() +
scale_x_continuous(breaks= seq(1960, 2015, 5)) +
xlab("Anno") +
ylab("Contributo in % del settore Servizi al PIL") +
ggtitle("Contributo in % del settore Servizi al PIL - Top 10 Countries")
ggplotly(d)
Contributo del settore Industriale dal 1960
industria <-read.csv("~/Desktop/Industry - Top 10 Country.csv")
ind_long <- melt(industria, id ="Country")
names(ind_long) <-c("Paese", "Anno", "Ind_Perc")
ind_long$Anno <- as.numeric(gsub("X", "", ind_long$Anno))
g <- ggplot(ind_long, aes(x=Anno, y=Ind_Perc, group=Paese)) +
geom_line(aes(colour=Paese)) +
geom_point(aes(colour=Paese),size =2) +
scale_color_manual(values = RColorBrewer::brewer.pal(10, "Set3") ) +
theme_minimal() +
scale_x_continuous(breaks= seq(1960, 2015, 5)) +
xlab("Anno") +
ylab("Contributo in % dell'Industria al PIL") +
ggtitle("Contributo in % dell'Industria al PIL - Top 10 Countries")
ggplotly(g)