-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.R
More file actions
374 lines (355 loc) · 16.8 KB
/
Copy pathapp.R
File metadata and controls
374 lines (355 loc) · 16.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
library(ggplot2)
library(shiny)
library(shinyjs)
library(shinydashboard)
library(ggplot2)
library(reshape2)
library(gridExtra)
library(dplyr)
library(tidyr)
library(DT)
library(data.table)
library(corrplot)
library(sf)
library(ggplot2)
library("rnaturalearth")
library("rnaturalearthdata")
df2015 = read.csv("2015.csv")
df2016 = read.csv("2016.csv")
df2017 = read.csv("2017.csv")
df2018 = read.csv("2018.csv")
df2019 = read.csv("2019.csv")
names(df2018) = c('Happiness.Rank','Country','Happiness.Score',
'Economy..GDP.per.Capita.', 'Family',
'Health..Life.Expectancy.', 'Freedom', 'Generosity',
'Trust..Government.Corruption.')
names(df2019) = c('Happiness.Rank','Country','Happiness.Score',
'Economy..GDP.per.Capita.', 'Family',
'Health..Life.Expectancy.', 'Freedom', 'Generosity',
'Trust..Government.Corruption.')
world <- ne_countries(scale = "medium", returnclass = "sf")
countries = sort( unique(c(df2015$Country,df2016$Country,df2017$Country,df2018$Country,df2019$Country)))
setdiff(countries,df2015$Country)
for (country in countries){
if (!(country %in% df2015$Country)){
df2015[nrow(df2015) + 1,] = c(country,rep(NA, ncol(df2015)-1))
}
}
for (country in countries){
if (!(country %in% df2016$Country)){
df2016[nrow(df2016) + 1,] = c(country,rep(NA, ncol(df2016)-1))
}
}
for (country in countries){
if (!(country %in% df2017$Country)){
df2017[nrow(df2017) + 1,] = c(country,rep(NA, ncol(df2017)-1))
}
}
for (country in countries){
if (!(country %in% df2018$Country)){
df2018[nrow(df2018) + 1,] = c(NA,country,rep(NA, ncol(df2018)-2))
}
}
for (country in countries){
if (!(country %in% df2019$Country)){
df2019[nrow(df2019) + 1,] = c(NA,country,rep(NA, ncol(df2019)-2))
}
}
cdf2015 = df2015[order(df2015$Country),]
cdf2016 = df2016[order(df2016$Country),]
cdf2017 = df2017[order(df2017$Country),]
cdf2018 = df2018[order(df2018$Country),]
cdf2019 = df2019[order(df2019$Country),]
rdf = copy(df2016)
rdf[] = lapply(rdf, function(x) {
x1 = type.convert(as.character(x), as.is=TRUE)
ifelse(grepl("^[0-9.]+$", x1), round(as.numeric(x1), 3), x1)})
rdf$Country = NULL
rdf = rdf %>% group_by(Region) %>%
summarise(Mean_Happiness = mean(Happiness.Score, na.rm=T),
Mean_gdp = mean(Economy..GDP.per.Capita.,na.rm=T),
Mean_family=mean(Family,na.rm=T),
Mean_freedom=mean(Freedom,na.rm=T),
Mean_LE = mean(Health..Life.Expectancy., na.rm=T))
rdf = rdf[1:10,]
names(rdf) = c("Region","Happiness", "GDP_per_capita", "Family", "Freedom", "Life_Expectancy")
rdf = melt(rdf, id='Region')
rdf[] = lapply(rdf, function(x) {
x1 = type.convert(as.character(x), as.is=TRUE)
ifelse(grepl("^[0-9.]+$", x1), round(as.numeric(x1), 3), x1)})
corr = df2019[1:156,3:(ncol(df2019)-2)]
names(corr) = c('Happiness','GDP_per_capita','Family','Life_Expectancy','Freedom')
corr[] = lapply(corr, function(x) {
x = type.convert(as.character(x), as.is=TRUE)})
scatter = df2019[1:156,3:ncol(df2019)]
names(scatter) = c('Happiness','GDP_per_capita','Family','Life_Expectancy','Freedom',
'Generosity','Trust in Government')
scatter[] = lapply(scatter, function(x) {
x = type.convert(as.character(x), as.is=TRUE)})
ui <- dashboardPage(
dashboardHeader(title="World Happiness Data"),
dashboardSidebar(
sidebarMenu(
menuItem("About", tabName = "about"),
menuItem('Map Visualization', tabName='map'),
menuItem("Regional Analysis", tabName = "region"),
menuItem("Cross Factor Analysis", tabName = "scatter"),
menuItem('Cross Country Analysis', tabName='country')
)
),
dashboardBody(
tags$style(HTML(".sidebar-menu li a { font-size: 16px; }")),
tabItems(
tabItem('about',
tags$h2('Overview', align='center'),
tags$br(),
tags$h4('The dashboard is a visual approach to analyze happiness across the world.
Through the use of multiple interactive plots, users can see the variance in
happiness on a regional and country level, and also study the various factors
that affect it. The key variables that we will consider are:'),
tags$h4(
tags$ol(
tags$li('Happiness.Score - Measure of happiness that is calculated using the below factors'),
tags$li('Economy..GDP.per.Capita - Per capita GDP of the country'),
tags$li('Family - Measure of satisfaction provided by family'),
tags$li('Health..Life.Expectancy. - Life expectancy in the country'),
tags$li('Freedom - Measure of freedom provided to the individual by the government, society, etc.'),
)
),
tags$br(),
HTML("<h4>The data can be found <a href='https://www.kaggle.com/datasets/unsdsn/world-happiness'> here</a>.
We have 5 years worth data from 2015-2019, thereby making a time series analysis possible,
each of which has the countrywise data for that year
and every country is also divided into what region of the world it belongs to, so we can perform
both country and region wise analysis.</h4>"),
tags$br(),
tags$h4('The plots might take some time to render, apologies for that.')),
tabItem("region",
tabsetPanel(
tabPanel("Region vs Region",
selectInput("regvar1", "Select Region 1", unique(df2016[,2])[1:10], selected='Australia and New Zealand'),
selectInput("regvar2", "Select Region 2", unique(df2016[,2])[1:10], selected='Eastern Asia'),
fluidRow(plotOutput("rvrplot"))
),
tabPanel("Factor vs Factor",
selectInput("facvar1", "Select Factor 1", c("Happiness", "GDP_per_capita", "Family", "Freedom", "Life_Expectancy"),selected='Happiness'),
selectInput("facvar2", "Seelct Factor 2", c("Happiness", "GDP_per_capita", "Family", "Freedom", "Life_Expectancy"),selected='GDP_per_capita'),
fluidRow(plotOutput("fvfplot"))
)
)),
tabItem("scatter",
tabsetPanel(
tabPanel("Factor vs Factor",
h3("Analyse 2 Factors against each other", align='center'),
fluidRow(plotOutput('corrplot'), align='center'),
selectInput("scatvar1", "Select Factor 1", c("Happiness", "GDP_per_capita", "Family", "Freedom", "Life_Expectancy"), selected='Happiness'),
selectInput("scatvar2", "Select Factor 2", c("Happiness", "GDP_per_capita", "Family", "Freedom", "Life_Expectancy"), selected='Family'),
fluidRow(plotOutput("scatplot")),
actionButton("fitline", "Fit Regression Line"),
actionButton("removefitline", "Remove Regression Line")
)
)),
tabItem('country',
tabsetPanel(
tabPanel("Country",
h3("Trend in country's happiness across 5 years", align='center'),
selectInput('cvar','Select Country',countries, selected='Finland'),
selectInput('fvar1', 'Select Factor', names(cdf2019)[3:7],selected='Happiness.Score'),
fluidRow(plotOutput('lineplot'), align='center')
),
tabPanel("Country vs Country",
h3("Compare the trends of two countries", align='center'),
selectInput('cvar1','Select Country 1',countries, selected='United States'),
selectInput('cvar2','Select Country 2',countries, selected='India'),
selectInput('fvar2', 'Select Factor', names(cdf2019)[3:7],selected='Happiness.Score'),
fluidRow(plotOutput('lvlplot'))
),
tabPanel("Top and Bottom",
h3("Compare the trends of countries at the (current) top/bottom"),
radioButtons('tb','Select Top or Bottom',c('Top','Bottom'), selected='Top'),
numericInput('pvar','Select number of countries',5, min=1, max=100),
selectInput('fvar3', 'Select Factor', names(cdf2019)[3:7],selected='Happiness.Score'),
fluidRow(plotOutput('plvlplot'))
)
)),
tabItem('map',
h3("Visualize the trends globally", align='center'),
selectInput('var', 'Select Factor', names(cdf2019)[3:7],selected='Happiness.Score'),
fluidRow(plotOutput('mapplot'))
)
)
)
)
server <- function(input, output) {
rv = reactiveValues(ols=0)
output$rvrplot = renderPlot({
rvar1 = input$regvar1
rvar2 = input$regvar2
ggplot(rdf[rdf$Region %in% c(rvar1,rvar2),], aes(x=variable,y=value, fill=variable)) +
geom_bar(stat='identity', col='black', alpha=0.75) + facet_grid(Region~.)+
geom_text( aes(label=value), vjust=1.25) + theme_classic() +
xlab('Factors')+ labs(fill='Factors')+
ggtitle(paste0('Comparison plot of different factors between ', rvar1, ' and ', rvar2))+
theme(axis.text.x = element_blank(),
text = element_text(size=11),
legend.text = element_text(size=11),
legend.title = element_text(size=13, hjust = 0.5),
strip.text.y = element_text(size=10),
axis.text.y = element_text(size=15),
legend.box.background=element_rect(),
plot.title = element_text(hjust=0.5))
})
output$fvfplot = renderPlot({
fvar1 = input$facvar1
fvar2 = input$facvar2
ggplot(rdf[rdf$variable %in% c(fvar1,fvar2),], aes(x=Region,y=value, fill=Region)) +
geom_bar(stat='identity', col='black', alpha=0.75) + facet_grid(variable~.)+
geom_text( aes(label=value), vjust=1.25) + theme_classic() +
xlab('Regions')+labs(fill='Regions')+
ggtitle(paste0('Comparison plot between ', fvar1, ' and ', fvar2, ' across regions'))+
theme(axis.text.x = element_blank(),
legend.text = element_text(size=11),
legend.title = element_text(size=13, hjust=0.5),
strip.text.y = element_text(size=10),
axis.text.y = element_text(size=15),
legend.box.background=element_rect(),
plot.title = element_text(hjust=0.5))
})
output$corrplot = renderPlot({
cormat = round(cor(corr),2)
corrplot(cormat, method='square',type='lower', diag=FALSE
,addCoef.col = "black")
}, width=500)
output$scatplot = renderPlot({
svar1 = input$scatvar1
svar2 = input$scatvar2
if(rv$ols) {
g = ggplot(data=scatter, mapping=aes_string(x=svar1,y=svar2)) +
geom_point(color="black", alpha=0.5) + geom_smooth(method='lm', col='red')+
theme_classic() +
ggtitle(paste0("Scatter plot between ", svar1, " and ", svar2))+
theme(plot.title = element_text(hjust=0.5))
}
else{
g = ggplot(data=scatter, mapping=aes_string(x=svar1,y=svar2)) +
geom_point(color="black", alpha=0.5) + theme_classic() +
ggtitle(paste0("Scatter plot between ", svar1, " and ", svar2)) +
theme(plot.title = element_text(hjust=0.5))
}
g
})
observeEvent(input$fitline, {
rv$ols = 1
})
observeEvent(input$removefitline, {
rv$ols = 0
})
output$lineplot = renderPlot({
fac1 = input$fvar1
cvar = input$cvar
df = as.data.frame(cbind(countries,cdf2015[fac1],cdf2016[fac1],
cdf2017[fac1],cdf2018[fac1],
cdf2019[fac1]))
names(df) = c('Country','2015', '2016', '2017', '2018', '2019')
for (country in countries){
df[which(df$Country==country),][which(is.na(df[which(df$Country==country),]))]=median(as.numeric(df[which(df$Country==country),2:ncol(df)]), na.rm=T)
}
df = df[order(df$`2019`, decreasing =TRUE),]
df[] = lapply(df, function(x) {
x1 = type.convert(as.character(x), as.is=TRUE)
ifelse(grepl("^[0-9.]+$", x1), round(as.numeric(x1), 3), x1)})
ggplot(melt(df[df$Country==cvar,])) + geom_line(aes(x=variable, y=value,group=1)) +
geom_point(aes(x=variable, y=value, group=1),color='red') +
geom_text(aes(x=variable, y=value,label=value),vjust=2,hjust=1)+
labs(x='Year',y=paste0(fac1), title=paste0('Trend in ',cvar, ' : ', fac1)) +
theme_classic()+
theme(plot.title = element_text(hjust = 0.5),
legend.box.background=element_rect())
})
output$lvlplot = renderPlot({
fac2 = input$fvar2
cvar1 = input$cvar1
cvar2 = input$cvar2
df = as.data.frame(cbind(countries,cdf2015[fac2],cdf2016[fac2],
cdf2017[fac2],cdf2018[fac2],
cdf2019[fac2]))
names(df) = c('Country','2015', '2016', '2017', '2018', '2019')
for (country in countries){
df[which(df$Country==country),][which(is.na(df[which(df$Country==country),]))]=median(as.numeric(df[which(df$Country==country),2:ncol(df)]), na.rm=T)
}
df = df[order(df$`2019`, decreasing =TRUE),]
df[] = lapply(df, function(x) {
x1 = type.convert(as.character(x), as.is=TRUE)
ifelse(grepl("^[0-9.]+$", x1), round(as.numeric(x1), 3), x1)})
ggplot(melt(df[df$Country %in% c(cvar1,cvar2),])) +
geom_line(aes(x=variable, y=value,group=Country, color=Country)) +
geom_point(aes(x=variable, y=value, group=Country,color=Country)) +
geom_text(aes(x=variable, y=value,label=value),vjust=2,hjust=1)+
labs(x='Year',y=paste0(fac2), title=paste0(cvar1 ,' vs ',cvar2, " : ", fac2, ' trend')) +
theme_classic()+
theme(plot.title = element_text(hjust = 0.5),
legend.box.background=element_rect())
})
output$plvlplot = renderPlot({
n = as.integer(input$pvar)
fac3 = input$fvar3
df = as.data.frame(cbind(countries,cdf2015[fac3],cdf2016[fac3],
cdf2017[fac3],cdf2018[fac3],
cdf2019[fac3]))
names(df) = c('Country','2015', '2016', '2017', '2018', '2019')
for (country in countries){
df[which(df$Country==country),][which(is.na(df[which(df$Country==country),]))]=median(as.numeric(df[which(df$Country==country),2:ncol(df)]), na.rm=T)
}
df = df[order(df$`2019`, decreasing =TRUE),]
df[] = lapply(df, function(x) {
x1 = type.convert(as.character(x), as.is=TRUE)
ifelse(grepl("^[0-9.]+$", x1), round(as.numeric(x1), 3), x1)})
if (input$tb == "Top"){
ggplot(melt(df[1:n,])) +
geom_line(aes(x=variable, y=value,group=Country, color=Country)) +
geom_point(aes(x=variable, y=value, group=Country,color=Country)) +
geom_text(aes(x=variable, y=value,label=value),vjust=2,hjust=1)+
labs(x='Year',y=paste0(fac3),
title=paste0(fac3,' : Trend in the top ', n ,' countries')) +
theme_classic()+
theme(text = element_text(size=10)) +
theme(plot.title = element_text(hjust = 0.5),
legend.box.background=element_rect())
}
else{
ggplot(melt(df[(length(countries)+1-n):length(countries),])) +
geom_line(aes(x=variable, y=value,group=Country, color=Country)) +
geom_point(aes(x=variable, y=value, group=Country,color=Country)) +
geom_text(aes(x=variable, y=value,label=value),vjust=2,hjust=1)+
labs(x='Year',y=paste0(fac3),
title=paste0(fac3,' : Trend in the bottom ', n ,' countries')) +
theme_classic() +
theme(plot.title = element_text(hjust = 0.5),
legend.box.background=element_rect())
}
})
output$mapplot = renderPlot({
var = input$var
fill = c()
for (country in world$name){
if (country %in% df2019$Country){
fill = c(fill, df2019[df2019$Country==country,var])
}
else{
fill = c(fill,NA)
}
}
fill = lapply(fill, function(x){
return (as.double(x))
})
fill=as.numeric(fill)
ggplot(data = world) +
geom_sf(aes(fill=fill)) +
ggtitle(paste0("World map showing distribution of ", var, " in 2019"),
subtitle = paste0("(Grey countries are due a lack of data)")) +
scale_fill_viridis_c(option='A')+
theme(plot.title = element_text(hjust=0.5),
plot.subtitle = element_text(hjust=0.5))
}, height=800)
}
shinyApp(ui = ui, server = server)