Informe salut
if (!require(rmarkdown, quietly = TRUE)) {
install.packages('rmarkdown', repos='http://cran.rediris.es')
}
## Warning: package 'rmarkdown' was built under R version 3.2.5
if (!require(knitr, quietly = TRUE)) {
install.packages('knitr', repos='http://cran.rediris.es')
}
## Warning: package 'knitr' was built under R version 3.2.5
if (!require(plotly, quietly = TRUE)) {
install.packages('plotly', repos='http://cran.rediris.es')
}
## Warning: package 'plotly' was built under R version 3.2.5
## Warning: package 'ggplot2' was built under R version 3.2.5
if (!require(readr)) {
install.packages('readr', repos='http://cran.rediris.es')
}
## Warning: package 'readr' was built under R version 3.2.5
if (!require(xtable)) {
install.packages('xtable', repos='http://cran.rediris.es')
}
## Warning: package 'xtable' was built under R version 3.2.5
if (!require(stargazer)) {
install.packages('stargazer', repos='http://cran.rediris.es')
}
## Warning: package 'stargazer' was built under R version 3.2.3
if (suppressPackageStartupMessages(!require(googleVis, quietly = TRUE))) {
install.packages('googleVis', repos='http://cran.rediris.es')
}
## Warning: package 'googleVis' was built under R version 3.2.5
if (!require("DT", quietly = T)) {
install.packages('DT', repos = 'http://cran.rstudio.com')
}
## Warning: package 'DT' was built under R version 3.2.5
if (!require("webshot", quietly = T)) {
install.packages('webshot', repos = 'http://cran.rstudio.com')
}
## Warning: package 'webshot' was built under R version 3.2.5
if (!require("shiny", quietly = T)) {
install.packages('shiny', repos = 'http://cran.rstudio.com')
}
## Warning: package 'shiny' was built under R version 3.2.5
require(knitr)
getwd()
## [1] "D:/Documents"
datafile <- "InformeSalut2014_2010.csv"
download.file(url="https://seeds4c.org/tiki-download_file.php?fileId=453", destfile=datafile)
# Take 3 at reading the file, this time setting the locale properly to indicate that the dataset came with the decimal mark also as a comma, besides the field delimiter. Field delimiter also comes surrounded by quotation marks, therefore there is no confusion with this format.
# We will finally detect all numbers as numbers and not as factors
my.data <- readr::read_csv(datafile, locale=readr::locale("es", decimal_mark = ","))
vnames <- colnames(my.data)
my.data<-my.data[,-15]
colnames(my.data) <- c("District", "Suburb", paste0("V", 1:12))
Taules
## ----results = 'asis'----------------------------------------------------
my.subset <- subset(my.data, District == "Sant Andreu", select = -V12)
knitr::kable(my.subset, caption = "Table with kable")
Table with kable
Sant Andreu |
La Trinitat Vella |
47.0 |
29.5 |
53.5 |
44.7 |
17.4 |
50.7 |
83.8 |
92.7 |
137.8 |
23.2 |
15.8 |
Sant Andreu |
Baró de Viver |
52.7 |
38.9 |
61.9 |
47.0 |
11.4 |
56.4 |
78.2 |
122.8 |
232.3 |
33.0 |
35.4 |
Sant Andreu |
El Bon Pastor |
50.6 |
26.8 |
71.8 |
39.1 |
12.6 |
46.9 |
82.1 |
115.5 |
114.5 |
16.7 |
16.9 |
Sant Andreu |
Sant Andreu |
48.2 |
31.6 |
79.3 |
26.4 |
11.2 |
35.3 |
83.9 |
97.0 |
86.0 |
18.9 |
5.8 |
Sant Andreu |
La Sagrera |
49.4 |
29.0 |
74.3 |
27.4 |
10.1 |
37.1 |
84.8 |
90.8 |
92.7 |
14.5 |
8.5 |
Sant Andreu |
El Congrés i els Indians |
63.1 |
36.8 |
73.7 |
27.5 |
10.8 |
38.3 |
84.7 |
88.9 |
74.1 |
21.4 |
4.2 |
Sant Andreu |
Navas |
51.8 |
30.9 |
75.4 |
25.8 |
10.4 |
36.3 |
85.4 |
87.7 |
87.7 |
20.2 |
5.8 |
print(xtable::xtable(my.subset, caption = "Table with xtable"),
type = "html", html.table.attributes = "border=1")
Table with xtable
|
District
|
Suburb
|
V1
|
V2
|
V3
|
V4
|
V5
|
V6
|
V7
|
V8
|
V9
|
V10
|
V11
|
1
|
Sant Andreu
|
La Trinitat Vella
|
47.00
|
29.50
|
53.50
|
44.70
|
17.40
|
50.70
|
83.80
|
92.70
|
137.80
|
23.20
|
15.80
|
2
|
Sant Andreu
|
Baró de Viver
|
52.70
|
38.90
|
61.90
|
47.00
|
11.40
|
56.40
|
78.20
|
122.80
|
232.30
|
33.00
|
35.40
|
3
|
Sant Andreu
|
El Bon Pastor
|
50.60
|
26.80
|
71.80
|
39.10
|
12.60
|
46.90
|
82.10
|
115.50
|
114.50
|
16.70
|
16.90
|
4
|
Sant Andreu
|
Sant Andreu
|
48.20
|
31.60
|
79.30
|
26.40
|
11.20
|
35.30
|
83.90
|
97.00
|
86.00
|
18.90
|
5.80
|
5
|
Sant Andreu
|
La Sagrera
|
49.40
|
29.00
|
74.30
|
27.40
|
10.10
|
37.10
|
84.80
|
90.80
|
92.70
|
14.50
|
8.50
|
6
|
Sant Andreu
|
El Congrés i els Indians
|
63.10
|
36.80
|
73.70
|
27.50
|
10.80
|
38.30
|
84.70
|
88.90
|
74.10
|
21.40
|
4.20
|
7
|
Sant Andreu
|
Navas
|
51.80
|
30.90
|
75.40
|
25.80
|
10.40
|
36.30
|
85.40
|
87.70
|
87.70
|
20.20
|
5.80
|
stargazer::stargazer(my.subset, type = "html",
title = "Table with stargazer", summary=FALSE)
Table with stargazer
|
|
District
|
Suburb
|
V1
|
V2
|
V3
|
V4
|
V5
|
V6
|
V7
|
V8
|
V9
|
V10
|
V11
|
|
1
|
Sant Andreu
|
La Trinitat Vella
|
47
|
29.5
|
53.5
|
44.7
|
17.4
|
50.7
|
83.8
|
92.7
|
137.8
|
23.2
|
15.8
|
2
|
Sant Andreu
|
Baró de Viver
|
52.7
|
38.9
|
61.9
|
47
|
11.4
|
56.4
|
78.2
|
122.8
|
232.3
|
33
|
35.4
|
3
|
Sant Andreu
|
El Bon Pastor
|
50.6
|
26.8
|
71.8
|
39.1
|
12.6
|
46.9
|
82.1
|
115.5
|
114.5
|
16.7
|
16.9
|
4
|
Sant Andreu
|
Sant Andreu
|
48.2
|
31.6
|
79.3
|
26.4
|
11.2
|
35.3
|
83.9
|
97
|
86
|
18.9
|
5.8
|
5
|
Sant Andreu
|
La Sagrera
|
49.4
|
29
|
74.3
|
27.4
|
10.1
|
37.1
|
84.8
|
90.8
|
92.7
|
14.5
|
8.5
|
6
|
Sant Andreu
|
El Congrés i els Indians
|
63.1
|
36.8
|
73.7
|
27.5
|
10.8
|
38.3
|
84.7
|
88.9
|
74.1
|
21.4
|
4.2
|
7
|
Sant Andreu
|
Navas
|
51.8
|
30.9
|
75.4
|
25.8
|
10.4
|
36.3
|
85.4
|
87.7
|
87.7
|
20.2
|
5.8
|
|
boxplot(my.subset$V1)

plot(my.subset$V1,my.subset$V2)

barplot(my.subset$V1,legend.text=my.subset$Suburb,col=rainbow(7))

Taules dinàmiques
require('DT')
d = data.frame(
my.subset,
stringsAsFactors = FALSE
)
dt <- datatable(d, filter = 'bottom', options = list(pageLength = 5)) %>%
formatStyle('V1',
color = styleInterval(c(0.5, 56), c('black', 'red', 'blue')),
backgroundColor = styleInterval(56.5, c('snow', 'lightyellow')),
fontWeight = styleInterval(58.0, c('italics', 'bold')))
dt
# As you can see there is an extra column that came with a note, and not real data. Therefore, we can remove it
tablelegend <- cbind(colnames(my.data[1:14]), vnames[1:14])
tablelegend <- rbind(tablelegend, unlist(strsplit(vnames[15], ":")))
colnames(tablelegend) <- c("Variable Code", "Variable Description")
knitr::kable(tablelegend, caption = "Table with kable")
Table with kable
District |
District (Barcelona-Catalonia-Spain) |
Suburb |
Suburb |
V1 |
Rate of over-aging, year 2014 |
V2 |
% of 75 y.o people or more living alone, year 2014 |
V3 |
Available Family income rate, year 2013* |
V4 |
% of 15 y.o people or more with primary studies or less, year 2014 |
V5 |
% of recorded unemployment 16-64 y.o, year 2014 |
V6 |
% of non-voters municipal elections, year 2015 |
V7 |
Life expectancy when born, period 2009-2013 |
V8 |
Comparative mortality rate, period 2009-2013* |
V9 |
Rate of Potential life years lost, period 2009-2013* |
V10 |
Tuberculosis Rate, period 2010-2014 |
V11 |
Teenager fecundity rate, period 2010-2014 |
V12 |
Prevalence of low weight when born, period 2010-2014 |
Note |
* 100 based on the total of Barcelona; dark gray corresponds to 25% with the worst indicator, green 25% better indicator and light gray the remaining 50%. |
# Simple interactive scatter chart
require(knitr)
library(plotly)
# Add some info about variables displayed
cat(paste0("V3: ", tablelegend[5,2]),
paste0("\nV5: ", tablelegend[7,2]),
paste0("\nV6: ", tablelegend[8,2]),
paste0("\nV11: ", tablelegend[13,2]))
## V3: Available Family income rate, year 2013*
## V5: % of recorded unemployment 16-64 y.o, year 2014
## V6: % of non-voters municipal elections, year 2015
## V11: Teenager fecundity rate, period 2010-2014
plot_ly(my.subset,
x = my.subset$V5, y = my.subset$V6, text = paste("Over-aging: ", my.subset$V1,
"Income: ", my.subset$V3,
"Fecundity: ", my.subset$V11,
"Suburb: ", my.subset$Suburb),
mode="marker",
size = my.subset$V3, opacity = my.subset$V3,
group = my.subset$District)