---
title: "DSTG - Klasteriranje po ishodima i logovima"
output: 
  flexdashboard::flex_dashboard:
    social: menu
    orientation: columns
    vertical_layout: fill
    source_code: embed
---

```{css, echo=FALSE}
.sidebar { overflow: auto; }
.dataTables_scrollBody {
    height:95% !important;
    max-height:95% !important;
}
.chart-stage-flex {
    overflow:auto !important;
}
```

```{r setup, include=FALSE}
library(readxl)
library(tidyverse)
library(tidytext)
library(cluster)
library(knitr)
library(kableExtra)
library(tidyverse)
library(corrplot)
library(dendextend)
library(factoextra)
library(circlize)
library(fmsb)

data1 <- read_excel('DSTG_ishodi.xlsx', sheet=1)
data1 <- na.omit(data1)

data2 <- data1 %>% select(LO_1:LO_6) 
data3 <- data.frame(scale(data2))

dist_student <- dist(data3)
hc_ward <- hclust(dist_student, method = "ward.D2")
ward_dend <- as.dendrogram(hc_ward) #%>% set("labels", 1:99)

ward_dend_color3 <- color_branches(ward_dend, k=3, col=c("#00BA38","#619CFF","#F8766D"))

klasteri3 <- cutree(hc_ward, k=3)
data3_cluster3 <- data3 %>% mutate(Cluster = klasteri3)
#data1_cluster3 <- data1 %>% mutate(Cluster = klasteri3)

AS <- data1 %>% summarise_at(vars(LO_1:LO_6), mean, na.rm = TRUE)

# rez3 <- data1_cluster3 %>% group_by(Cluster) %>% 
#         summarise_at(vars(LO_1:LO_6), mean, na.rm = TRUE) %>% 
#         gather(learning_outcome, Average, LO_1:LO_6)
# rez3 <- rez3 %>% 
#         add_row(Cluster=rep(0,6), learning_outcome=c("LO_1","LO_2","LO_3","LO_4","LO_5","LO_6"), 
#                 Average=as.numeric(AS))
# rez3$Cluster <- factor(rez3$Cluster)
# levels(rez3$Cluster) <- c("All", "1", "2", "3")

bodovi <- c(10,14,26,8,26,16)

boje <- c("#C77CFF","#F8766D","#00BA38","#619CFF")


sil3 <- silhouette(data3_cluster3$Cluster, dist(data3))
rez1 <- data.frame(unclass(sil3))
tablica <- data1 %>% rowwise() %>% mutate(ukupno = sum(c_across(LO_1:LO_6), na.rm = T)) %>% ungroup()
tablica1 <- tablica %>% bind_cols(rez1) %>% group_by(cluster) %>% arrange(sil_width, .by_group = TRUE)
tablica1 <- tablica1 %>% ungroup()

#viz3 <- data3_cluster3 %>% gather(learning_outcome, Scores, LO_1:LO_6)

hl <- data.frame(learning_outcome=c("LO_1","LO_2","LO_3","LO_4","LO_5","LO_6"), 
                 Scores=c(10,14,26,8,26,16),
                 xcor1=rep(2.5,6), xcor2=rep(3,6), ycor=c(12,16,28,10,28,18))

klast <- c()
for (i in 1:length(sil3[,"cluster"])) {
  ifelse(sil3[,"sil_width"][i] < 0 && sil3[,"cluster"][i] == 2, klast[i] <- sil3[,"neighbor"][i], klast[i] <- sil3[,"cluster"][i])
}
sil3_novi <- silhouette(klast, dist(data3))
rez2 <- data.frame(unclass(sil3_novi))

tablica2 <- tablica %>% bind_cols(rez2) %>% group_by(cluster) %>% arrange(sil_width, .by_group = TRUE)
tablica2 <- tablica2 %>% ungroup()

data3_cluster3_novi <- data3 %>% mutate(Cluster = klast)
viz3_novi <- data3_cluster3_novi %>% gather(learning_outcome, Scores, LO_1:LO_6)

data1_cluster3_novi <- data1 %>% mutate(Cluster = klast)
rez3_novi <- data1_cluster3_novi %>% group_by(Cluster) %>% 
        summarise_at(vars(LO_1:LO_6), mean, na.rm = TRUE) %>% 
        gather(learning_outcome, Average, LO_1:LO_6)
rez3_novi <- rez3_novi %>% 
        add_row(Cluster=rep(0,6), learning_outcome=c("LO_1","LO_2","LO_3","LO_4","LO_5","LO_6"), 
                Average=as.numeric(AS))
rez3_novi$Cluster <- factor(rez3_novi$Cluster)
levels(rez3_novi$Cluster) <- c("All", "1", "2", "3")

radar <- data.frame(
  LO_1 = c(10, 0), LO_2 = c(14, 0), LO_3 = c(26, 0),
  LO_4 = c(8, 0), LO_5 = c(26, 0), LO_6 = c(16, 0))
kl1 <- t(rez3_novi %>% filter(Cluster==1) %>% select(Average))
kl2 <- t(rez3_novi %>% filter(Cluster==2) %>% select(Average))
kl3 <- t(rez3_novi %>% filter(Cluster==3) %>% select(Average))
colnames(kl1) <- c("LO_1", "LO_2", "LO_3", "LO_4", "LO_5","LO_6")
colnames(kl2) <- c("LO_1", "LO_2", "LO_3", "LO_4", "LO_5","LO_6")
colnames(kl3) <- c("LO_1", "LO_2", "LO_3", "LO_4", "LO_5","LO_6")
radar <- radar %>% bind_rows(AS, as_tibble(kl1), as_tibble(kl2), as_tibble(kl3))
rownames(radar) <- c("Max", "Min", "All", "Cluster 1", "Cluster 2", "Cluster 3")

logovi <- read_excel('studenti_komponente.xlsx', sheet=1)
logovi <- logovi %>% replace(is.na(.), 0)
logovi <- logovi %>% select(!c(Chat, Journal, Napomene_zadaca, Pregledni_izvjestaj, Vodici_korisnici,
                               Odabir, Postavljanje_datoteke))
logovi_long <- logovi %>% pivot_longer(cols = !Student, names_to="Aktivnost", values_to = "Vrijednost")

logovi2 <- as_tibble(data.frame(scale(logovi %>% select(!Student))))

dist_logdata <- dist(logovi2)
hc_logdata <- hclust(dist_logdata, method = "ward.D2")
dend_logdata <- as.dendrogram(hc_logdata) %>% place_labels(parse_number(logovi$Student))

br_klast <- 3
dend_color_logdata <- color_branches(dend_logdata, k=br_klast, col=c("#00BA38","#619CFF","#F8766D"))
klasteri_logdata <- cutree(hc_logdata, k=br_klast)
logovi2_cluster <- logovi2 %>% mutate(klaster = klasteri_logdata)
sil_logovi <- silhouette(logovi2_cluster$klaster, dist_logdata)

analiza1 <- logovi %>% rowwise() %>% mutate(Ukupno = sum(c_across(!Student), na.rm = T)) %>% ungroup()
tablica_logovi <- analiza1 %>% bind_cols(data.frame(unclass(sil_logovi))) %>% group_by(cluster) %>% arrange(sil_width, .by_group = TRUE) %>% ungroup()

logs0 <- logovi %>% mutate(klaster=0) %>% pivot_longer(cols = !c(Student,klaster), names_to="Aktivnost", values_to = "Vrijednost")
logs1 <- logovi %>% mutate(klaster = klasteri_logdata) %>% filter(klaster == 1) %>% 
  pivot_longer(cols = !c(Student,klaster), names_to="Aktivnost", values_to = "Vrijednost")
logs2 <- logovi %>% mutate(klaster = klasteri_logdata) %>% filter(klaster == 2) %>% 
  pivot_longer(cols = !c(Student,klaster), names_to="Aktivnost", values_to = "Vrijednost")
logs3 <- logovi %>% mutate(klaster = klasteri_logdata) %>% filter(klaster == 3) %>% 
  pivot_longer(cols = !c(Student,klaster), names_to="Aktivnost", values_to = "Vrijednost")
logoviSVI <- logs0 %>% add_row(logs1) %>% add_row(logs2) %>% add_row(logs3)
logoviSVI$klaster <- factor(logoviSVI$klaster)
levels(logoviSVI$klaster) <- c("All", "1", "2", "3")

avg1 <-logovi %>% mutate(klaster = klasteri_logdata) %>% group_by(klaster) %>% summarize_at(vars(BigBlueButton:Zoom_meeting), list(mean))
avg2 <- logovi %>% summarize_at(vars(BigBlueButton:Zoom_meeting), list(mean)) %>% add_column(klaster=0, .before="BigBlueButton")
avg <- avg2 %>% bind_rows(avg1)
avg$klaster <- factor(avg$klaster)
levels(avg$klaster) <- c("All", "1", "2", "3")
avg <- avg %>% pivot_longer(BigBlueButton:Zoom_meeting, names_to="Aktivnost", values_to="Vrijednost")

postotak0 <- logs0 %>% mutate(suma = sum(Vrijednost)) %>% group_by(Aktivnost) %>% 
  summarize(postotak = sum(Vrijednost) / suma, .groups="drop_last") %>% 
  distinct() %>% mutate(klaster=0)
postotak1 <- logs1 %>% mutate(suma = sum(Vrijednost)) %>% group_by(Aktivnost) %>% 
  summarize(postotak = sum(Vrijednost) / suma, .groups="drop_last") %>% 
  distinct() %>% mutate(klaster=1)
postotak2 <- logs2 %>% mutate(suma = sum(Vrijednost)) %>% group_by(Aktivnost) %>% 
  summarize(postotak = sum(Vrijednost) / suma, .groups="drop_last") %>% 
  distinct() %>% mutate(klaster=2)
postotak3 <- logs3 %>% mutate(suma = sum(Vrijednost)) %>% group_by(Aktivnost) %>% 
  summarize(postotak = sum(Vrijednost) / suma, .groups="drop_last") %>% 
  distinct() %>% mutate(klaster=3)
postotak_klasteri <- postotak0 %>% add_row(postotak1) %>% 
  add_row(postotak2) %>% add_row(postotak3)
postotak_klasteri$klaster <- factor(postotak_klasteri$klaster)
levels(postotak_klasteri$klaster) <- c("All", "1", "2", "3")

studenti_ishodi <- tablica %>% bind_cols(rez1) %>% 
  select(Student=osoba, Ukupno=ukupno, cluster, sil_width)
studenti_logovi <- analiza1 %>% bind_cols(data.frame(unclass(sil_logovi))) %>% 
  select(Student, Ukupno, cluster, sil_width)
studenti_SVE <- studenti_ishodi %>% 
  inner_join(studenti_logovi, by="Student", suffix=c(".ishod", ".log")) %>%
  relocate(Ukupno.log, .after=Ukupno.ishod) %>%
  relocate(cluster.log, .after=cluster.ishod)
```


Dendrogram {data-navmenu="Klasteriranje po ishodima"}
=======================================================================

Column {data-width=800}
-------------------------------------

### Dendrogram ishodi učenja (tri klastera)
```{r fig.width=18,fig.height=10}
circlize_dendrogram(ward_dend_color3, dend_track_height = 0.8)
```

Column {data-width=100}
-------------------------------------

### Broj studenata u klasterima
```{r}
data3_cluster3 %>% count(Cluster)
```

Silhouette {data-navmenu="Klasteriranje po ishodima"}
=======================================================================

Row {data-width=500}
-------------------------------------

### Silhouette

```{r}
fviz_silhouette(sil3, print.summary=FALSE)
```

Row {data-width=400}
-------------------------------------

### Silhouette info {data-height=200}
```{r}
summary(sil3)
```

### Preporučeni broj klastera {data-height=400}
```{r}
fviz_nbclust(data3, FUN = hcut, method = "silhouette")
```

Tablice studenata {data-navmenu="Klasteriranje po ishodima"}
=======================================================================

Column {data-width=500}
-------------------------------------

### Svi studenti

```{r}
tablica1 %>%
  kbl(caption = "Tablica svih studenata") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Column {data-width=500}
-------------------------------------

### Studenti u krivom klasteru

```{r}
tablica1 %>% filter(sil_width<0) %>%
  kbl(caption = "Tablica studenata u pogrešnom klasteru") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Silhouette (ručni zahvat) {data-navmenu="Klasteriranje po ishodima"}
=======================================================================

Column {data-width=600}
-------------------------------------

### Napomena {data-height=100}
Svi studenti iz klastera **2** s negativnom silhouettom su prebačeni u susjedni klaster.

### Silhouette (nakon ručnog zahvata) {data-height=800}
```{r}
fviz_silhouette(sil3_novi, print.summary=FALSE)
```

Column {data-width=300}
-------------------------------------

### info (nakon ručnog zahvata)
```{r}
summary(sil3_novi)
```

Tablice studenata (ručni zahvat) {data-navmenu="Klasteriranje po ishodima"}
=======================================================================

Column {data-width=500}
-------------------------------------

### Svi studenti (nakon ručnog zahvata)

```{r}
tablica2 %>%
  kbl(caption = "Tablica svih studenata") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Column {data-width=500}
-------------------------------------

### Studenti u krivom klasteru (nakon ručnog zahvata)

```{r}
tablica2 %>% filter(sil_width<0) %>%
  kbl(caption = "Tablica studenata u pogrešnom klasteru") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Silhouette svih verzija {data-navmenu="Klasteriranje po ishodima"}
=======================================================================

Column {data-width=300}
-------------------------------------

### Silhouette (dobivena računalom)
```{r}
fviz_silhouette(sil3, print.summary=FALSE)
```

### info
```{r}
summary(sil3)
```

Column {data-width=300}
-------------------------------------

### Silhouette (nakon ručnog zahvata)
```{r}
fviz_silhouette(sil3_novi, print.summary=FALSE)
```

### info (nakon ručnog zahvata)
```{r}
summary(sil3_novi)
```

Funkcije gustoća {data-navmenu="Vizualizacije klastera po ishodima"}
=======================================================================

Column {data-width=250}
-------------------------------------

### Funkcije gustoća za pojedini ishod

```{r}
ggplot(viz3_novi, aes(x=Scores, fill=learning_outcome, color=learning_outcome)) + 
  geom_density(alpha=.2) +
  facet_wrap(~learning_outcome) + theme(legend.position="none") + ylim(0,1.5)
```

Column {data-width=500}
-------------------------------------

### Funkcije gustoća za pojedini ishod unutar klastera

```{r}
ggplot(viz3_novi, aes(x=Scores, fill=learning_outcome, color=learning_outcome)) + 
  geom_density(alpha=.2) + facet_grid(Cluster ~ learning_outcome) + ylim(0,1.5) +
  theme(axis.text.x = element_text(angle = 90, vjust = 0.5, hjust=1),legend.position="none")
```

Planirani i ostvareni bodovi (slika1) {data-navmenu="Vizualizacije klastera po ishodima"}
=======================================================================

### Tri klastera

```{r fig.width=15}
ggplot(rez3_novi, aes(x=learning_outcome, y=Average, fill=Cluster, color=Cluster)) + 
  geom_bar(stat="identity", width=0.4, position = position_dodge(width=0.9)) +
  geom_segment(aes(x=0.6,xend=1.4,y=bodovi[1],yend=bodovi[1]),color="black") +
  geom_segment(aes(x=1.6,xend=2.4,y=bodovi[2],yend=bodovi[2]),color="black") +
  geom_segment(aes(x=2.6,xend=3.4,y=bodovi[3],yend=bodovi[3]),color="black") +
  geom_segment(aes(x=3.6,xend=4.4,y=bodovi[4],yend=bodovi[4]),color="black") +
  geom_segment(aes(x=4.6,xend=5.4,y=bodovi[5],yend=bodovi[5]),color="black") +
  geom_segment(aes(x=5.6,xend=6.4,y=bodovi[6],yend=bodovi[6]),color="black") +
  geom_text(x=1, y=10.7, label=bodovi[1], color="black") +
  geom_text(x=2, y=14.7, label=bodovi[2], color="black") +
  geom_text(x=3, y=26.7, label=bodovi[3], color="black") +
  geom_text(x=4, y=8.7, label=bodovi[4], color="black") +
  geom_text(x=5, y=26.7, label=bodovi[5], color="black") +
  geom_text(x=6, y=16.7, label=bodovi[6], color="black") +
  geom_text(aes(label=round(Average,2)), position=position_dodge(width=0.9), vjust=-0.5) +
  scale_fill_manual(values = c("#C77CFF","#F8766D","#00BA38","#619CFF")) +
  scale_color_manual(values = c("#C77CFF","#F8766D","#00BA38","#619CFF"))
```

Planirani i ostvareni bodovi (slika2) {data-navmenu="Vizualizacije klastera po ishodima"}
=======================================================================

### Tri klastera

```{r fig.width=10}
ggplot() + 
  geom_bar(data=rez3_novi, aes(x=Cluster, y=Average, fill=Cluster, color=Cluster), stat="identity", width=0.65, position = position_dodge(width=0.9)) +
  facet_wrap(~learning_outcome) + 
  geom_hline(data= hl, aes(yintercept=Scores), linetype="dotted") +
  geom_text(data = hl, aes(x=xcor1, y=ycor, label=Scores)) +
  geom_text(data=rez3_novi, aes(x=Cluster, y=Average, label=round(Average,2)), vjust=1.5) +
  scale_fill_manual(values = c("#C77CFF","#F8766D","#00BA38","#619CFF")) +
  scale_color_manual(values = c("#C77CFF","#F8766D","#00BA38","#619CFF"))
```


Planirani i ostvareni bodovi (slika3) {data-navmenu="Vizualizacije klastera po ishodima"}
=======================================================================

### Tri klastera

```{r fig.width=9}
ggplot(data=rez3_novi, aes(x=Cluster, y=Average, fill=Cluster, color=Cluster)) + 
  geom_bar(stat="identity", width=0.65, position = position_dodge(width=0.9)) +
  geom_text(aes(label=round(Average,2)), vjust=-0.3) + expand_limits(y = c(0, 27)) +
  facet_wrap(~learning_outcome, scales="free",
             labeller=as_labeller(c(LO_1="LO_1 (max: 10)", LO_2="LO_2 (max: 14)", LO_3="LO_3 (max: 26)",
                                  LO_4="LO_4 (max: 8)", LO_5="LO_5 (max: 26)", LO_6="LO_6 (max: 16)"))) +
  scale_fill_manual(values = c("#C77CFF","#F8766D","#00BA38","#619CFF")) +
  scale_color_manual(values = c("#C77CFF","#F8766D","#00BA38","#619CFF"))
```

Radar charts {data-navmenu="Vizualizacije klastera po ishodima"}
=====================================

Row {data-width=200}
-----------------------------------------------------------------------

### Uspjeh svih studenata

```{r, echo = F}
#knitr::include_graphics("radar_svi.png")
par(mar=c(1,1,1,1))
radarchart(radar[c(1:3),], axistype=2, pcol = c(boje[1]),
           pfcol = scales::alpha(c(boje[1]), 0.5), 
           plty = 1, plwd = 1.5, vlcex = 0.9, palcex = 0.8, pty=20, cglcol = "grey", cglty = 1)
legend(
  x = "bottomright", legend = rownames(radar[3,]), horiz = FALSE,
  pch = 20 , col = boje[1], cex=0.8,
  text.col = "black", xpd=TRUE
)
```

### Uspjeh klastera 1

```{r, echo = F}
#knitr::include_graphics("radar_svi_klaster1.png")
par(mar=c(1,1,1,1))
radarchart(radar[c(1:3,4),], axistype=2, pcol = c(boje[1],boje[2]),
           pfcol = scales::alpha(c(boje[1],boje[2]), 0.3), 
           plty = 1, plwd = 1.5, vlcex = 0.9, palcex = 0.8, pty=20, cglcol = "grey", cglty = 1)
legend(
  x = "bottomright", legend = rownames(radar[c(3,4),]), horiz = FALSE,
  pch = 20 , col = boje[1:2], cex=0.8,
  text.col = "black", xpd=TRUE
)
```

Row {data-width=200}
-----------------------------------------------------------------------

### Uspjeh klastera 2

```{r, echo = F}
#knitr::include_graphics("radar_svi_klaster2.png")
par(mar=c(1,1,1,1))
radarchart(radar[c(1:3,5),], axistype=2, pcol = c(boje[1],boje[3]),
           pfcol = scales::alpha(c(boje[1],boje[3]), 0.3), 
           plty = 1, plwd = 1.5, vlcex = 0.9, palcex = 0.8, pty=20, cglcol = "grey", cglty = 1)
legend(
  x = "bottomright", legend = rownames(radar[c(3,5),]), horiz = FALSE,
  pch = 20 , col = boje[c(1,3)], cex=0.8,
  text.col = "black", xpd=TRUE
)
```

### Uspjeh klastera 3

```{r, echo = F}
#knitr::include_graphics("radar_svi_klaster3.png")
par(mar=c(1,1,1,1))
radarchart(radar[c(1:3,6),], axistype=2, pcol = c(boje[1],boje[4]),
           pfcol = scales::alpha(c(boje[1],boje[4]), 0.3), 
           plty = 1, plwd = 1.5, vlcex = 0.9, palcex = 0.8, pty=20, cglcol = "grey", cglty = 1)
legend(
  x = "bottomright", legend = rownames(radar[c(3,6),]), horiz = FALSE,
  pch = 20 , col = boje[c(1,4)], cex=0.8,
  text.col = "black", xpd=TRUE
)
```

Dendrogram {data-navmenu="Klasteriranje po logovima"}
=======================================================================

Column {data-width=800}
-------------------------------------

### Dendrogram logovi (3 klastera)
```{r fig.width=18,fig.height=10}
circlize_dendrogram(dend_color_logdata, dend_track_height = 0.8)
```

Column {data-width=100}
-------------------------------------

### Broj studenata u klasterima
```{r}
logovi2_cluster %>% count(klaster)
```

Silhouette {data-navmenu="Klasteriranje po logovima"}
=======================================================================

Row {data-width=500}
-------------------------------------

### Silhouette
```{r}
fviz_silhouette(sil_logovi, print.summary=FALSE)
```

Row {data-width=400}
-------------------------------------

### Silhouette info {data-height=200}
```{r}
summary(sil_logovi)
```

### Preporučeni broj klastera {data-height=400}
```{r}
fviz_nbclust(logovi2, FUN = hcut, method = "silhouette")
```

Tablice studenata {data-navmenu="Klasteriranje po logovima"}
=======================================================================

Column {.tabset}
-------------------------------------

### Svi studenti

```{r}
tablica_logovi %>%
  kbl(caption = "Tablica svih studenata") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

### Studenti u krivom klasteru

```{r}
tablica_logovi %>% filter(sil_width<0) %>%
  kbl(caption = "Tablica studenata u pogrešnom klasteru") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Vizualizacija klastera (BoxPlot) {data-navmenu="Vizualizacija klastera po logovima"}
=======================================================================

### Vizualizacija aktivnosti po klasterima
```{r fig.width=12, fig.cap='Broj logova po pojedinoj aktivnosti unutar pojedinih klastera'}
ggplot(logoviSVI, aes(x=klaster, y=Vrijednost, group=klaster)) + 
  geom_boxplot() + facet_wrap(vars(Aktivnost), scales="free", ncol=5) + 
  xlab('') + ylab('')
```

Postotak po aktivnosti {data-navmenu="Vizualizacija klastera po logovima"}
=======================================================================

Column {.tabset}
-------------------------------------

### Postotak aktivnosti po klasterima
```{r fig.width=12, fig.cap='Postotak logova po pojedinoj aktivnosti u odnosu na ukupni broj logova unutar samog klastera'}
ggplot(postotak_klasteri, aes(x=Aktivnost, y=postotak, group=Aktivnost)) + 
  geom_bar(stat="identity",fill="#ff2560") + 
  facet_wrap(vars(klaster), ncol=1) +
  geom_text(data=postotak_klasteri, aes(x=Aktivnost, y=postotak, label=sprintf("%0.2f%%",round(postotak*100,2))), vjust=-0.5) +
  scale_y_continuous(labels = scales::percent_format(), limits=c(0,0.6)) + xlab('') + ylab('') + 
  theme(axis.text.x=element_text(size=rel(0.8)))
```

### Postotak aktivnosti po klasterima (sortirano)
```{r fig.width=12, fig.cap='Postotak logova po pojedinoj aktivnosti u odnosu na ukupni broj logova unutar samog klastera'}
ggplot(postotak_klasteri, aes(x=reorder_within(Aktivnost,postotak,klaster), y=postotak, group=Aktivnost)) + 
  geom_bar(stat="identity",fill="#ff2560") + 
  facet_wrap(vars(klaster), ncol=1, scales = "free_x") +
  geom_text(data=postotak_klasteri, aes(x=reorder_within(Aktivnost,postotak,klaster), y=postotak, label=sprintf("%0.2f%%",round(postotak*100,2))), vjust=-0.5) +
  scale_y_continuous(labels = scales::percent_format(), limits=c(0,0.6)) + xlab('') + ylab('') +
  scale_x_reordered() +
  theme(axis.text.x=element_text(size=rel(0.8)))
```

Prosjek logova {data-navmenu="Vizualizacija klastera po logovima"}
=======================================================================

### Prosjek logova po klasterima
```{r fig.width=12, fig.cap='Prosječni broj logova po pojedinoj aktivnosti unutar pojedinog klastera'}
ggplot(logoviSVI, aes(x=klaster, y=Vrijednost, group=klaster, fill=factor(klaster))) + 
  geom_bar(stat="summary", fun=mean) +
  facet_wrap(vars(Aktivnost), scales="free", ncol=5) + 
  scale_fill_manual(values=boje) +
  geom_text(data = avg, aes(x=klaster, y=Vrijednost, label=round(Vrijednost,2)), vjust=-0.25, size=3) + 
  xlab('') + ylab('') + theme(legend.position = "none") +
  scale_y_continuous(limits = c(0, NA), expand = expansion(mult = c(0, 0.2)))
```


Tablica ishoda i logova
=======================================================================

Column {data-width=500}
-------------------------------------

### Sortirano po *ishod* klasterima i ostvarenim bodovima

```{r}
studenti_SVE %>% group_by(cluster.ishod) %>% 
  arrange(desc(Ukupno.ishod), .by_group = TRUE) %>%
  kbl(caption = "Tablica svih studenata s klasteriranjem po ishodima i logovima, sortirano po klasterima po ishodu i ostvarenim bodovima") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```

Column {data-width=500}
-------------------------------------

### Sortirano po *log* klasterima i ukupnim logovima

```{r}
studenti_SVE %>% group_by(cluster.log) %>% 
  arrange(desc(Ukupno.log), .by_group = TRUE) %>%
  kbl(caption = "Tablica svih studenata s klasteriranjem po ishodima i logovima, sortirano po klasterima po logovima i ukupnim logovima") %>%
  kable_classic("hover",full_width = F, html_font = "Cambria")
```
