politics = read.csv("politics.csv")
library(dplyr)
## 
## Attaching package: 'dplyr'
## The following objects are masked from 'package:stats':
## 
##     filter, lag
## The following objects are masked from 'package:base':
## 
##     intersect, setdiff, setequal, union
library(ggplot2)
library(lubridate)
## 
## Attaching package: 'lubridate'
## The following object is masked from 'package:base':
## 
##     date
library(stringr)
getwd()
## [1] "/students/apbakhareva_1/11.09"
politics1 = politics %>%
  select(polintr, psppsgva, actrolga, psppipla, cptppola, trstprl, trstlgl, trstplc, trstplt, trstprt, vote, contplt, wrkprty, sgnptit, pbldmn, bctprd, prtdgcl,lrscale, stflife, stfeco, stfgov, stfedu, stfhlth,freehms, prtvtbie) 
#polintr: How interested in politics: ORDINAL +

# psppsgva: Political system allows people to have a say in what government does: ORDINAL +

# actrolga: Able to take active role in political group: ORDINAl +

#psppipla: Political system allows people to have influence on politics: ORDINAL +

#cptppola: Confident in own ability to participate in politics: ORDINAL +

#trstprl: Trust in country's parliament: INTERVAL+

#trstlgl: Trust in the legal system: INTERVAL+

#trstplc: Trust in the police: INTERVAL+

#trstplt: Trust in politicians: INTERVAL+

#trstprt: Trust in political parties: INTERVAL+

#vote: Voted last national election: ORDINAL+

#contplt: Contacted politician or government official last 12 months: NOMINAL +

#wrkprty: Worked in political party or action group last 12 months: NOMINAL+

#sgnptit: Signed petition last 12 months: NOMINAL+

#pbldmn: Taken part in lawful public demonstration last 12 months: NOMINAL+

#bctprd: Boycotted certain products last 12 months: NOMINAL +

#prtdgcl: How close to party: ORDINAL+

#lrscale: Placement on left right scale: INTERVAL+

#stflife: How satisfied with life as a whole: INTERVAL+

#stfeco: How satisfied with present state of economy in country: INTERVAL+

#stfgov: How satisfied with the national government: INTERVAL+

#stfedu: State of education in country nowadays: INTERVAL

#stfhlth: State of health services in country nowadays: INTERVAL

#freehms: Gays and lesbians free to live life as they wish: ORDINAL

#prtvtbie: Party voted for in last national election, Ireland: NOMINAL

Describing variables

Label = c("polintr", "psppsgva", "actrolga", "psppipla", "cptppola", "trstprl", "trstlgl", "trstplc", "trstplt", "trstprt", "vote", "contplt", "wrkprty", "sgnptit", "pbldmn", "bctprd", "prtdgcl", "lrscale", "stflife", "stfeco", "stfgov", "stfedu", "stfhlth", "freehms", "prtvtbie") 
Meaning = c("How interested in politics", "Political system allows people to have a say in what government does", "Able to take active role in political group", "Political system allows people to have influence on politics", "Confident in own ability to participate in politics", "Trust in country's parliament", "Trust in the legal system", "Trust in the police", "Trust in politicians", "Trust in political parties", "Voted last national election", "Contacted politician or government official last 12 months", "Worked in political party or action group last 12 months", "Signed petition last 12 months", "Taken part in lawful public demonstration last 12 months","Boycotted certain products last 12 months", "How close to party","Placement on left right scale", "How satisfied with life as a whole", "How satisfied with present state of economy in country", "How satisfied with the national government", "State of education in country nowadays", "State of health services in country nowadays", "Gays and lesbians free to live life as they wish", "Party voted for in last national election")
Level_Of_Measurement <- c("Ordinal", "Ordinal", "Ordinal", "Ordinal", "Ordinal", "Interval", "Interval", "Interval", "Interval", "Interval", "Ordinal", "Nominal", "Nominal", "Nominal", "Nominal", "Nominal", "Ordinal", "Interval", "Interval", "Interval", "Interval", "Interval", "Interval", "Ordinal", "Nominal")
Categorical_Or_Continuous = c("Categorical", "Categorical", "Categorical", "Categorical", "Categorical", "Continuous", "Continuous", "Continuous", "Continuous", "Continuous", "Categorical", "Categorical", "Categorical", "Categorical", "Categorical", "Categorical", "Categorical", "Continuous", "Continuous", "Continuous", "Continuous", "Continuous", "Continuous", "Categorical", "Categorical")
df <- data.frame(Label, Meaning, Level_Of_Measurement, Categorical_Or_Continuous, stringsAsFactors = FALSE)
library(knitr) 
kable(df)
Label Meaning Level_Of_Measurement Categorical_Or_Continuous
polintr How interested in politics Ordinal Categorical
psppsgva Political system allows people to have a say in what government does Ordinal Categorical
actrolga Able to take active role in political group Ordinal Categorical
psppipla Political system allows people to have influence on politics Ordinal Categorical
cptppola Confident in own ability to participate in politics Ordinal Categorical
trstprl Trust in country’s parliament Interval Continuous
trstlgl Trust in the legal system Interval Continuous
trstplc Trust in the police Interval Continuous
trstplt Trust in politicians Interval Continuous
trstprt Trust in political parties Interval Continuous
vote Voted last national election Ordinal Categorical
contplt Contacted politician or government official last 12 months Nominal Categorical
wrkprty Worked in political party or action group last 12 months Nominal Categorical
sgnptit Signed petition last 12 months Nominal Categorical
pbldmn Taken part in lawful public demonstration last 12 months Nominal Categorical
bctprd Boycotted certain products last 12 months Nominal Categorical
prtdgcl How close to party Ordinal Categorical
lrscale Placement on left right scale Interval Continuous
stflife How satisfied with life as a whole Interval Continuous
stfeco How satisfied with present state of economy in country Interval Continuous
stfgov How satisfied with the national government Interval Continuous
stfedu State of education in country nowadays Interval Continuous
stfhlth State of health services in country nowadays Interval Continuous
freehms Gays and lesbians free to live life as they wish Ordinal Categorical
prtvtbie Party voted for in last national election Nominal Categorical
Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}  
###Calculating central tendency measures: interval variables are taken for analysis
#1
politics1$trstplt =  as.numeric(as.character(politics1$trstplt))
values.trstplt <- c(mean(politics1$trstplt), Mode(politics1$trstplt), median(politics1$trstplt))
names(values.trstplt) <- c("mean", "mode", "median")


#2
politics1$trstprl =  as.numeric(as.character(politics1$trstprl))
values.trstprl <- c(mean(politics1$trstprl), Mode(politics1$trstprl), median(politics1$trstprl))
names(values.trstprl) <- c("mean", "mode", "median")


#3
politics1$trstlgl =  as.numeric(as.character(politics1$trstlgl))
values.trstlgl <- c(mean(politics1$trstlgl), Mode(politics1$trstlgl), median(politics1$trstlgl))
names(values.trstlgl) <- c("mean", "mode", "median")


#4
politics1$trstplc =  as.numeric(as.character(politics1$trstplc))
values.trstplc <- c(mean(politics1$trstplc), Mode(politics1$trstplc), median(politics1$trstplc))
names(values.trstplc) <- c("mean", "mode", "median")


#5
politics1$trstprt =  as.numeric(as.character(politics1$trstprt))
values.trstprt <- c(mean(politics1$trstprt), Mode(politics1$trstprt), median(politics1$trstprt))
names(values.trstprt) <- c("mean", "mode", "median")


#6
politics1$lrscale = as.numeric(as.character(politics1$lrscale))
values.lrscale <- c(mean(politics1$lrscale), Mode(politics1$lrscale), median(politics1$lrscale))
names(values.lrscale) <- c("mean", "mode", "median")


#7
politics1$stflife = as.numeric(as.character(politics1$stflife))
values.stflife <- c(mean(politics1$stflife), Mode(politics1$stflife), median(politics1$stflife))
names(values.stflife) <- c("mean", "mode", "median")


#8
politics1$stfeco = as.numeric(as.character(politics1$stfeco))
values.stfeco <- c(mean(politics1$stfeco), Mode(politics1$stfeco), median(politics1$stfeco))
names(values.stfeco) <- c("mean", "mode", "median")


#9
politics1$stfgov = as.numeric(as.character(politics1$stfgov))
values.stfgov <- c(mean(politics1$stfgov), Mode(politics1$stfgov), median(politics1$stfgov))
names(values.stfgov) <- c("mean", "mode", "median")


#10
politics1$stfedu = as.numeric(as.character(politics1$stfedu))
values.stfedu <- c(mean(politics1$stfedu), Mode(politics1$stfedu), median(politics1$stfedu))
names(values.stfedu) <- c("mean", "mode", "median")


#11
politics1$stfhlth = as.numeric(as.character(politics1$stfhlth))
values.stfhlth <- c(mean(politics1$stfhlth), Mode(politics1$stfhlth), median(politics1$stfhlth))
names(values.stfhlth) <- c("mean", "mode", "median")


tendencymeasures_overview =  data.frame(values.trstplt, values.trstprl, values.trstlgl, values.trstplc, values.trstprt, values.lrscale, values.stflife, values.stfeco,values.stfgov, values.stfedu,values.stfhlth )

tendencymeasures_overview
##        values.trstplt values.trstprl values.trstlgl values.trstplc
## mean         4.850925       6.785274       7.678999       6.406239
## mode         5.000000       5.000000       7.000000       7.000000
## median       4.000000       5.000000       6.000000       7.000000
##        values.trstprt values.lrscale values.stflife values.stfeco
## mean         5.207835       19.53827       7.393544      5.991658
## mode         5.000000        5.00000       8.000000      6.000000
## median       4.000000        5.00000       7.000000      5.000000
##        values.stfgov values.stfedu values.stfhlth
## mean        5.536815      9.137468       4.581429
## mode        5.000000      7.000000       4.000000
## median      5.000000      7.000000       4.000000
Mode <- function(x) {
  ux <- unique(x)
  ux[which.max(tabulate(match(x, ux)))]
}  
#Hist 1
ggplot()+
  geom_histogram(data = politics1, aes(x = trstplt), binwidth = 1, fill="#7ee0ff", col="#3a0d0d", alpha = 0.5) +
  xlim(c(0, 10)) +
  xlab("Trust in politicians") + 
  ylab("Number of people") +
  geom_vline(aes(xintercept = mean(politics1$trstplt), color = 'mean'), linetype="dashed", size=1) +
  geom_vline(aes(xintercept = median(politics1$trstplt), color = 'median'), linetype="longdash", size=1)+
  geom_vline(aes(xintercept = Mode(politics1$trstplt), color = 'mode'), linetype="dotted",size=1) +
  scale_color_manual(name = "Measurement", values = c(median = "#cb3f68", mean = "#824acd", mode = "#339666"))+
  ggtitle("The level of trust towards politicians")
## Warning: Removed 38 rows containing non-finite values (stat_bin).

#Hist 2
ggplot()+
  geom_histogram(data = politics1, aes(x = stfeco), binwidth = 1, fill="#FFB273", col="#FF9640", alpha = 0.5) +
  xlim(c(0, 10)) +
  xlab("Satisfaction with the economy`s state") + 
  ylab("Number of people") +
  geom_vline(aes(xintercept = mean(politics1$stfeco), color = 'mean'), linetype="dotdash", size=1) +
  geom_vline(aes(xintercept = median(politics1$stfeco), color = 'median'), linetype="solid", size=1)+
  geom_vline(aes(xintercept = Mode(politics1$stfeco), color = 'mode'), linetype="dotted",size=1) +
  scale_color_manual(name = "Measurement", values = c(median = "#008500", mean = "#A60000", mode = "#006363"))+
  ggtitle("The level of satisfaction with present state of economy ")
## Warning: Removed 30 rows containing non-finite values (stat_bin).

#bar 1
politics3 = politics1 %>%
  filter(cptppola != 8 )%>%
  filter(cptppola != 9 )%>% 
  filter(cptppola != 7 )

politics3$cptppola <- factor(politics3$cptppola, labels = c("Not at all confident", "A little confident", "Quite confident", "Very confident", "Completely confident"), ordered= F)
ggplot() +
  geom_bar(data = politics3, aes(x = cptppola), fill="#AD66D5", col="#5F2580", alpha = 0.5) +
  xlab("Confident in own ability to participate in politics") + 
  ylab("Number of people") +
  ggtitle("The level of people`s confidence in ability to participate in politics")

politics4 = politics1 %>%
  filter(freehms != 8 )%>%
  filter(freehms != 9 )%>% 
  filter(freehms != 7 )
#bar2
politics4$freehms <- factor(politics4$freehms, labels = c("Agree strongly", "Agree", "Neither agree nor disagree", "Disagree", "Disagree strongly"), ordered= F)
ggplot() +
  geom_bar(data = politics4, aes(x = freehms), fill="#FFE773", col="#A68900", alpha = 0.5) +
  xlab("Homosexual peopleare free to live their lives as they wish ") + 
  ylab("Number of people") +
  ggtitle("People`s attitude towards homosexual relationships")

#boxplot 1
politics8 = politics1 %>%
  filter(bctprd != 8 )%>%
  filter(bctprd != 9 )%>% 
  filter(bctprd != 7 )


politics8$bctprd <- factor(politics8$bctprd, labels = c("Yes", "No"), ordered= F,exclude = NA)
ggplot() +
  geom_boxplot(data = politics8, aes(x = bctprd, y = stflife), fill="#C9F76F", col="#679B00", alpha = 0.5) +
  ylim(c(1,10)) +
  xlab("Boycotted certain products last 12 months") + 
  ylab("Satisfied in the life") +
  ggtitle("Life satisfaction due to boycotting certain products")
## Warning: Removed 28 rows containing non-finite values (stat_boxplot).

#??? boxplot2
politics7 = politics1 %>%
  filter(actrolga != 8 )%>%
  filter(actrolga != 9 )%>% 
  filter(actrolga != 7 )

politics7$actrolga <- factor(politics7$actrolga, labels = c("Not at all able", "A little able", "Quite able", "Very able", "Completely able"), ordered= F,exclude = NA)
ggplot() +
  geom_boxplot(data = politics7, aes(x = actrolga, y = trstlgl), fill="#E667AF", col="#85004B", alpha = 0.5) +
  ylim(c(1,10)) +
  xlab("Considering ability to be politically active") + 
  ylab("Trust in the legal system") +
  ggtitle("Considering ability to be politically active due to the trust in the legal system")
## Warning: Removed 208 rows containing non-finite values (stat_boxplot).

# BAD stacked bar 1
#politics10 = politics1 %>%
 # filter(prtvtbie != 77) %>%
  #filter(prtvtbie != 88) %>%
  #filter(prtvtbie != 99)
#politics10$prtvtbie <- factor(politics10$prtvtbie, labels = c("Anti-Austerity Alliance", "Fianna Fáil", #"Fine Gael", "Green Party", "Independents", "Labour", "Sinn Féin", "Social Democrats", "Socialist Party", #"Other", "Not applicable"), ordered= F)

#politics11 = politics10 %>%
 # filter(prtdgcl != 7) %>%
#  filter(prtdgcl != 8) %>%
 # filter(prtdgcl != 9)
#politics11$closeness_to_party <- factor(politics11$prtdgcl, labels = c("Very close", "Quite close", "Not close", "Not at all close"))
#ggplot() +
 # geom_bar(data = politics11, aes(x = prtvtbie, fill = closeness_to_party))+
  #coord_flip()+
  #xlab("Party voted on last elections") + 
#  ylab("Number of people") + ggtitle("The closeness to party due to party affiliation")
#stacked plot2
politics12 = politics1 %>%
  filter(polintr != 7) %>%
  filter(polintr != 8) %>%
  filter(polintr != 9)
politics12$polintr <- factor(politics12$polintr, labels = c("Very interested", "Quite interested", "Hardly interested", "Not at all interested"), ordered= F)

politics13 = politics12 %>%
  filter(sgnptit != 7) %>%
  filter(sgnptit != 8) %>%
  filter(sgnptit != 9)
politics13$signed_petitions <- factor(politics13$sgnptit, labels = c("Yes", "No"), ordered = F)
ggplot() +
  geom_bar(data = politics13, aes(x = polintr, fill = signed_petitions))+
  coord_flip()+
  xlab("How interested in politics") + 
  ylab("Number of people") +
  ggtitle("Participation in signing petitions due to the interest in politics")

#Scatter plot
politics14 = politics1 %>%
  filter(lrscale != 77 | lrscale != 88 | lrscale != 99) %>%
  filter(stfgov != 77 | stfgov != 88 | stfgov != 99)

politics1$lrscale = as.numeric(as.character(politics1$lrscale))
politics1$stfgov = as.numeric(as.character(politics1$stfgov))

plot(politics1$lrscale, politics1$stfgov, pch = 20)

ggplot(data = politics1) +
  geom_point( aes(x = lrscale, y = stfgov))+
 # xlim(c(0,10)) + 
  scale_color_gradient(low = "white", high = "black") +
  theme_bw()