---
title: "Survey engines"
output: html_notebook
---

```{r}
library(dplyr)
library(tidyr)
library(ggplot2)

dataset.survey <- read.csv("../../datasets/survey.csv", sep = ",") %>%
  rename(q1 = X1..Why.did.you.create..collaborate.with..the.engine.project.) %>%
  rename(q2 = X2..Have.you.ever.wrote.code.for.a.traditional.software..not.related.to.games..like.web.app..phone.app..desktop.app..etc.) %>%
  rename(q3 = X3..How.similar.do.you.think.writing.a.Video.Game.Engine.is.compared.to.writing.a.Framework.for.traditional.apps..like.Vue..Laravel..Rails..Django..and.Spring.boot..)
```
# q1

```{r}
q1 <- dataset.survey %>% select(q1) %>% separate(q1, c("a1", "a2", "a3", "a4", "a5", "a6", "other"), sep = ";")

d1 <- q1 %>% select(a = a1) %>% na.omit()
d2 <- q1 %>% select(a = a2) %>% na.omit()
d3 <- q1 %>% select(a = a3) %>% na.omit()
d4 <- q1 %>% select(a = a4) %>% na.omit()
d5 <- q1 %>% select(a = a5) %>% na.omit()
d6 <- q1 %>% select(a = a6) %>% na.omit()
d7 <- q1 %>% select(a = other) %>% na.omit()

q1t <- rbind(d1, d2, d3, d4, d5, d6, d7) %>%
  group_by(a) %>% 
  summarise(freq = n()) %>%
  mutate(countT= sum(freq)) %>%
  mutate(per=paste(round(100*freq/countT,0), "%", sep = "")) %>%
  ungroup() %>%
  arrange(desc(freq))

other_n <- q1t %>% filter(freq == 1) %>% nrow

# q1t <- rbind(q1t, data.frame(a="Other", freq=other_n)) %>% filter(freq > 1)

other <- q1t %>% filter(freq == 1) %>% select(a)
q1t <- q1t %>% filter(freq > 1)

# To help me to create a game
# To learn how to build an engine
# To have the full control of the environment
# Because the existent engines do not provide the features I need
# Because I wanted to work with this especific programming language
# Because the licenses of the existent engines are too expensive

resps <- c("R1: Create game", 
           "R2: Learn", 
           "R3: Full control", 
           "R4: Need new features", 
           "R5: Language", 
           "R6: License")

library(stringr)

plot <- ggplot(q1t, aes(reorder(a, freq),  freq)) +
  geom_bar(stat="identity", fill = "gray98", colour="gray20") +
  geom_text(aes(label=per),  colour="gray20", hjust=1.2, vjust=0.5, size=5) +
  coord_flip() +
  labs(title="", x="", y="Frequency") +
  scale_x_discrete(labels = function(x) str_wrap(str_replace_all(x, "foo" , " "), width = 40))+
  theme_minimal() +
  theme(legend.position="none", axis.text=element_text(size=14))
  
  
# +   scale_x_discrete(labels=resps)

ggsave(filename=paste("../../plots/survey-q1.pdf", sep = ""), plot=plot, dpi=300, units = "cm")

# Others
# write.table(other, col.names = F, row.names = F)
# other
```

# q2

```{r}
d <- dataset.survey %>% select(q2) %>%
  group_by(q2, add=TRUE) %>%
  summarise(freq = n()) %>%
  mutate(countT= sum(freq)) %>%
  mutate(per=round(100*freq/countT,2)) %>%
  mutate(question="Why?")

plot <- ggplot(d, aes(x="", y=freq, fill=q2)) +
  geom_bar(width = 1, stat = "identity") +
  # geom_text(aes(y = freq, label = paste(q2, freq) )) +
  # theme(axis.text=element_text(size=18)) +
  coord_polar(theta = "y", start = 0,  clip = "on") +
  scale_fill_brewer(palette=6)+
  labs(title="", x="", y="", fill="") +
  theme_minimal() + theme(legend.position="bottom")

ggsave(filename=paste("../../plots/survey-q2.pdf", sep = ""), plot=plot, dpi=300, units = "cm")
```

# q3
```{r}
# require(likert)
# require(plyr)
# library(reshape2)

#aggregating for all the responses into a df that contains only the question wise category wise total for all the students
# agg_table <- sqldf::sqldf("select question, category, SUM(responses) as total from survey group by question, category")

d <- dataset.survey %>% select(q3) %>%
  group_by(q3, add=TRUE) %>%
  summarise(freq = n()) %>%
  mutate(countT= sum(freq)) %>%
  mutate(per=paste(round(100*freq/countT,0), "%", sep = "")) %>%
  mutate(question="Why?")

d$q3 <- as.factor(d$q3)

scale_labels <- c("Very different","4","3","2","Very similar")

plot <- ggplot(d, aes(reorder(q3, -freq), freq)) +   
  geom_bar(position = "dodge", stat="identity", fill = "gray98", colour="gray20")+
  geom_text(aes(label=per), colour="gray20", hjust=0.5, vjust=1.5, size=5) +
  # scale_fill_brewer(palette=6)+
  theme_minimal() + theme(legend.position="bottom") +
  labs(title="", x="Difference Levels", y="Frequency") +
  scale_x_discrete(labels=scale_labels) + 
  theme(axis.text=element_text(size=14))
  # theme(axis.text.x = element_text(angle = 0, vjust = 1))

ggsave(filename=paste("../../plots/survey-q3.pdf", sep = ""), plot=plot, dpi=300, units = "cm", height = 8)
```
