-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathosaVizFinal.qmd
883 lines (769 loc) · 26 KB
/
osaVizFinal.qmd
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
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
---
title: "Prevents föredragna visualiseringar"
title-block-banner: "#009ca6"
title-block-banner-color: "#FFFFFF"
format:
html:
fig-width: 9
fig-height: 6
fig-dpi: 150
execute:
echo: true
warning: false
message: false
cache: false
editor_options:
chunk_output_type: console
---
## Setting up
First let's load the necessary libraries. Some additional libraries will be loaded as we go along.
```{r}
library(tidyverse)
library(ggdist)
library(ggpp)
library(foreign)
library(readxl)
library(showtext)
library(stringr)
library(patchwork)
library(glue)
library(ggridges)
library(scales)
### some commands exist in multiple packages, here we define preferred ones that are frequently used
select <- dplyr::select
count <- dplyr::count
recode <- car::recode
rename <- dplyr::rename
```
### Theming
Then our fonts, colors, and the ggplot theme.
```{r}
## Loading Google fonts (https://fonts.google.com/)
font_add_google("Noto Sans", "noto")
## Flama font with regular and italic font faces
font_add(family = "flama",
regular = "fonts/Flama-Font/Flama Regular.otf",
italic = "fonts/Flama-Font/Flama Italic.otf",
bold = "fonts/Flama-Font/FlamaBlack Regular.otf")
## Automatically use showtext to render text
showtext_auto()
prevent_green <- "#008332"
prevent_light_green <- "#76A100"
prevent_dark_blue <- "#003E6E"
prevent_blue <- "#005F89"
prevent_light_blue <- "#4398BA"
prevent_yellow <- "#FBB900"
prevent_red <- "#BE5014"
prevent_gray_red <- "#6C5861"
prevent_light_gray <- "#F0F0F0"
prevent_gray <- "#d3d3d3"
prevent_dark_gray <- "#3B3B3B"
prevent_turquoise <- "#009a9d"
prevent_green_comp <- "#D9ECE0"
prevent_light_green_comp <- "#DCE7BF"
prevent_dark_blue_comp <- "#BFCEDA"
prevent_blue_comp <- "#BFD7E1"
prevent_light_blue_comp <- "#D0E5EE"
prevent_yellow_comp <- "#FEEDBF"
prevent_red_comp <- "#EFD3C4"
prevent_green_contrast <- "#006632"
prevent_blue_contrast <- "#003E6E"
prevent_yellow_contrast <- "#FBD128"
prevent_red_contrast <- "#B01200"
prevent_gray_red_contrast <- "#68534E"
# manual palette creation, 7 colors
PREVENTpalette1 <- c("#6C5861", "#005F89", "#4398BA", "#76A100", "#008332", "#FBB900", "#FBD128")
# create palette with 12 colors based on Prevent colors above
PREVENTpalette2 <- colorRampPalette(colors = c("#6C5861", "#005F89", "#4398BA", "#76A100", "#008332", "#FBB900", "#FBD128"))(12)
theme_prevent <- function(fontfamily = "flama", axisTitleSize = 13, titlesize = 15,
margins = 12, axisface = "plain", stripsize = 12,
panelDist = 0.6, legendSize = 9, legendTsize = 10,
axisTextSize = 10, ...) {
theme(
text = element_text(family = fontfamily),
axis.title.x = element_text(
margin = margin(t = margins),
size = axisTitleSize
),
axis.title.y = element_text(
margin = margin(r = margins),
size = axisTitleSize
),
plot.title = element_text(
#family = "flama",
face = "bold",
size = titlesize
),
axis.title = element_text(
face = axisface
),
axis.text = element_text(size = axisTextSize),
plot.caption = element_text(
face = "italic"
),
legend.text = element_text(family = fontfamily, size = legendSize),
legend.title = element_text(family = fontfamily, size = legendTsize),
strip.text = element_text(size = stripsize),
panel.spacing = unit(panelDist, "cm", data = NULL),
legend.background = element_rect(color = "lightgrey"),
...
)
}
# these rows are for specific geoms, such as geom_text() and geom_text_repel(), to match font family. Add as needed
# update_geom_defaults("text", list(family = fontfamily)) +
# update_geom_defaults("text_repel", list(family = fontfamily)) +
# update_geom_defaults("textpath", list(family = fontfamily)) +
# update_geom_defaults("texthline", list(family = fontfamily))
```
### Importing data
Moving on to item labels.
```{r}
# get itemlabels for our sample domain (arbetsbelastning och krav)
itemlabels <- read.csv("06_ldrskp/finalItems.csv")
itemlabels
```
And loading data. We will use the items from the "Leadership" domain/subscale for the exampel visualizations.
```{r}
spssDatafile <- "data/2023-04-26 Prevent OSA-enkat.sav"
# read unedited complete SurveyMonkey data file, downloaded in SPSS format
df <- read.spss(spssDatafile, to.data.frame = TRUE) %>%
select(starts_with("q0010")) %>% # include only items from abk and å
select(!ends_with("04")) %>% # remove item that did not work adequately in the psychometric analysis
na.omit() # remove participants with missing data (to simplify)
names(df) <- itemlabels$itemnr # set matching variable names - df and itemlabels need to have common "itemnr" labels
# show first 5 rows of df
df %>%
head(5)
```
## Person scores for each domain
We also need person scores based on their item responses in the domain. There are two ways to get this. The most correct way is to estimate these using a function from the `catR` package, `thetaEst`. Another option is to use a transformation table, where raw responses are simply summarized for each participant, and the sum score is looked up in the table.
We'll first do the estimation, then the transformation table.
### Direct estimation of person scores
For this, we first need to recode the responses into integers, where the lowest response ("Aldrig") is coded as 0, and so on, until "Alltid" = 5.
```{r}
# vector of response categories
svarskategorier <- c("Aldrig","Sällan","Ibland","Ganska ofta","Mycket ofta","Alltid")
# recode responses to numbers and save the output in a separate dataframe
df.scored <- df %>%
mutate(across(everything(), ~ car::recode(.x,"'Aldrig'=0;
'Sällan' =1;
'Ibland'=2;
'Ganska ofta'=3;
'Mycket ofta'=4;
'Alltid'=5",
as.factor = FALSE)))
```
Scored data means each participant has had their overall score estimated based on their responses on a subscale. The score is estimated based on the psychometric/Rasch analysis made separately for each scale.
We'll borrow a simplified function from the RISEkbmRasch package, without actually loading the package.
```{r}
#library(RISEkbmRasch) # devtools::install_github("pgmj/RISEkbmRasch")
library(catR)
estimateScores <- function(dfin, itemParams, model = "PCM", method = "WL") {
estTheta <- function(
personResponse, itemParameters = itemParams,
rmod = model, est = method) {
thetaEst(itemParameters, as.numeric(as.vector(personResponse)),
model = rmod, method = est
)
}
dfin %>%
t() %>%
as_tibble() %>%
map_dbl(., estTheta)
}
# we need to use the item parameters from the Rasch analysis previously made from the whole sample. There is one CSV-file per domain/subscale. The object containing item parameters needs to be a matrix.
itemParamsLeadership <- read.csv("06_ldrskp/itemParameters.csv") %>%
as.matrix()
# then estimate peron scores for this subscale/domain
df$score <- estimateScores(dfin = df.scored,
itemParams = itemParamsLeadership)
df$score %>%
head(5)
```
We've stored the estimated person scores as variable `df$score`.
### Transformation table
This can be used as a simple lookup & replace table, where raw response data is replaced with integers (starting at 0, as shown earlier), and then summed within the items in the domain/subscale. This is the "ordinal sum score" in the table below, which should be replaced with the "Logit score", which is on an interval scale.
```{r}
library(eRm)
scoringTable <- function(dfin) {
sink(nullfile())
ppar <- dfin %>%
PCM() %>%
person.parameter() %>%
print() %>%
as.data.frame()
sink()
scoreTable <- ppar %>%
dplyr::rename(
`Logit score` = "X1.Estimate", `Logit std.error` = "X1.Std.Error",
`Ordinal sum score` = "X1.Raw.Score"
) %>%
remove_rownames() %>%
mutate(across(where(is.numeric), ~ round(.x, 2)))
ordinal_to_interval_table <<- scoreTable
}
scoringTable(df.scored)
ordinal_to_interval_table
```
A limitation of this table is that it will only list the values estimated in the sample used. Since our data is skewed it is safer to directly estimate the scores, as shown previously, to avoid issues with missing values in the lookup table. The table above (for the leadership domain) does contain values from ordinal sum score 0 up til the maximum ordinal sum score.
```{r}
df.scored$score <- df$score
```
## Preparing visualizations
We'll subset a sample of 17 random respondents to use for the visualizations.
```{r}
set.seed(1523)
sampleMed <- 17
# pick random sample to use for visualization example
df.test20 <- df.scored %>%
slice_sample(n = sampleMed) %>%
add_column(group = "Mättillfälle 1")
# get another sample for examples comparing two measurements
df.test20b <- df.scored %>%
slice_sample(n = sampleMed) %>%
add_column(group = "Mättillfälle 2")
# combine data
df.compare20 <- rbind(df.test20, df.test20b)
df.compare20 %>%
head(5)
```
## Visualizing person scores for domains
First we need to get scores for all domains that can be scored. These are estimated as described above, but will now be loaded from pre-estimated CSV-files.
```{r}
#| code-fold: true
df.scores <- read.csv("02_arbkrv/scored.csv") %>%
select(score) %>%
rename(`Arbetsbelastning och krav` = score) %>%
add_column(id = seq_along(1:nrow(.)))
df.scores <- read.csv("03_mpvrk/scored.csv") %>%
select(score) %>%
rename(`Möjlighet att påverka` = score) %>%
add_column(id = seq_along(1:nrow(.))) %>%
full_join(df.scores, by = "id")
df.scores <- read.csv("04_std/scored.csv") %>%
select(score) %>%
rename(Stöd = score) %>%
add_column(id = seq_along(1:nrow(.))) %>%
full_join(df.scores, by = "id")
df.scores <- read.csv("05_rec/scored.csv") %>%
select(score) %>%
rename(Återhämtning = score) %>%
add_column(id = seq_along(1:nrow(.))) %>%
full_join(df.scores, by = "id")
df.scores <- read.csv("06_ldrskp/scored.csv") %>%
select(score) %>%
rename(Ledarskap = score) %>%
add_column(id = seq_along(1:nrow(.))) %>%
full_join(df.scores, by = "id")
df.scores <- read.csv("09_psyktry/scored.csv") %>%
select(score) %>%
rename(`Psykologisk trygghet` = score) %>%
add_column(id = seq_along(1:nrow(.))) %>%
full_join(df.scores, by = "id")
df.scores$id <- NULL
df.scores %>%
head(5)
```
The dataframe needs to be in long format for creating the figure/plot, and we'll choose a random set of 17 participants for this too.
```{r}
#| code-fold: true
df.plot <- df.scores %>%
slice_sample(n = sampleMed) %>%
pivot_longer(everything(),
names_to = "Område",
values_to = "Indexvärde") %>%
group_by(Område) %>%
summarise(Medelvärde = mean(Indexvärde, na.rm = T))
df.plot
```
### Multiple domains
```{r}
#| code-fold: true
#| fig-height: 6
ggplot(df.plot) +
# plot mean values for each domain
geom_point(
aes(
x = Medelvärde,
y = Område
),
color = prevent_green,
size = 10,
shape = 16,
alpha = 0.9
) +
coord_cartesian(
xlim = c(-3, 4), # set x axis limits
clip = "off"
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent(axisTextSize = 11) +
labs(
title = "Översikt områden",
subtitle = "Värden längre till höger är bättre",
caption = "Gröna cirklar indikerar medelvärden. Skalan sträcker sig från lägsta till högsta möjliga värde.",
y = "",
x = ""
) +
theme(
axis.text.x = element_blank(), # remove text from x axis
axis.title = element_blank()
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 12)) + # wrap y label text
annotate("text",
x = 4, y = 0.2,
label = "Högsta\nmöjliga\nvärde",
color = "darkgrey",
size = 3
) +
annotate("text",
x = -3, y = 0.2,
label = "Lägsta\nmöjliga\nvärde",
color = "darkgrey",
size = 3
) +
update_geom_defaults("text", list(family = "flama")) # sets default font for annotate for the rest of the session
```
### Multiple domains comparison
We need a comparison group.
```{r}
#| code-fold: true
set.seed(1452)
df.plot2 <- df.scores %>%
slice_sample(n = sampleMed) %>%
pivot_longer(everything(),
names_to = "Område",
values_to = "Indexvärde") %>%
group_by(Område) %>%
summarise(Medelvärde = mean(Indexvärde, na.rm = T))
```
```{r}
#| fig-height: 6
#| code-fold: true
ggplot() +
# plot "previous measurement"
geom_point(
data = df.plot2,
aes(
x = Medelvärde,
y = Område
),
color = prevent_green,
size = 8,
shape = 16,
alpha = 0.4
) +
# plot mean values for each domain for the "new measurement
geom_point(
data = df.plot,
aes(
x = Medelvärde,
y = Område
),
color = prevent_green,
size = 10,
shape = 16,
alpha = 0.85 # slight transparency in case circles overlap
) +
coord_cartesian(
xlim = c(-3, 4), # set x axis limits
clip = "off" # don't clip the annotate text set at the end of the code chunk
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent() +
labs(
title = "Översikt områden",
subtitle = "Värden längre till höger är bättre",
caption = "Gröna cirklar indikerar medelvärden.\nMörka cirklar = senaste mätningen.\nLjusare/mindre cirklar = föregående mätning.",
y = "",
x = ""
) +
theme(
axis.text.x = element_blank(), # remove text from x axis
axis.title = element_blank()
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 12)) + # wrap y label text
annotate("text",
x = 4, y = 0.2,
label = "Högsta\nmöjliga\nvärde",
color = "darkgrey",
size = 3
) +
annotate("text",
x = -3, y = 0.2,
label = "Lägsta\nmöjliga\nvärde",
color = "darkgrey",
size = 3
)
```
### Single domain item responses
Create dataframe with 17 random participants.
```{r}
#| code-fold: true
# create random sample dataset
df.plot20 <- df %>%
slice_sample(n = sampleMed) %>%
select(all_of(itemlabels$itemnr), score) %>%
pivot_longer(!score) %>% # we need long format for ggplot
rename(
itemnr = name,
svarskategori = value
) %>%
left_join(itemlabels, by = "itemnr") %>% # get item descriptions as a variable in the df
add_column(group = "Mättillfälle 1")
# enable comparisons by adding another random group
df.plot20b <- df %>%
slice_sample(n = sampleMed) %>%
select(all_of(itemlabels$itemnr), score) %>%
pivot_longer(!score) %>% # we need long format for ggplot
rename(
itemnr = name,
svarskategori = value
) %>%
left_join(itemlabels, by = "itemnr") %>% # get item descriptions as a variable in the df
add_column(group = "Mättillfälle 2")
df.plotComp20 <- rbind(df.plot20,df.plot20b)
df.plotComp20 %>%
head(10)
```
Calculate median responses
```{r}
#| code-fold: true
df.medians <- df.plot20 %>%
# create numeric responses where 1 = "Aldrig, and 6 = "Alltid"
mutate(svarNum = as.integer(fct_rev(svarskategori))) %>%
add_column(id = rep(1:17, each = 5)) %>% # sample size = 17, and 5 questions in domain
select(itemnr,svarNum,id) %>%
pivot_wider(names_from = "itemnr",
values_from = "svarNum",
id_cols = "id")
df.medians %>%
head(10)
```
```{r}
#| code-fold: true
# prepare dataframe to store values
medianResponses <- itemlabels
# get median values (can be .5 when we have an even N)
medians <- c()
for (i in medianResponses$itemnr) {
med1 <- median(df.medians[[i]])
medians <- c(medians,med1)
}
medianResponses$medians <- medians
medianResponses
```
### Median responses one domain
```{r}
#| code-fold: true
#| fig-height: 7
#| fig-width: 9
ggplot(medianResponses) +
geom_point(aes(x = medians,
y = item),
color = prevent_green,
size = 12) +
theme_minimal() +
theme_prevent() +
labs(
title = "Indexfrågor",
subtitle = "Medianvärde per fråga",
y = "",
x = ""
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(limits = svarskategorier,
labels = ~ stringr::str_wrap(.x, width = 8))
```
### All responses one domain
```{r}
#| code-fold: true
#| fig-height: 7
#| fig-width: 9
df.plot20 %>%
dplyr::count(item, svarskategori) %>%
mutate(nFactor = factor(n)) %>%
mutate(svarskategori = fct_rev(svarskategori)) %>%
ggplot() +
geom_point(aes(x = svarskategori, y = item, size = n * 1.5, color = svarskategori),
# size = 3,
shape = 16
) +
geom_text(aes(x = svarskategori, y = item, label = n),
color = "white") +
scale_size_continuous(
range = c(7, 16), # set minimum and maximum point size
guide = "none" # remove legend for size aesthetic
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent(legend.position = "none") +
scale_color_viridis_d("",
begin = 0.2,
end = 0.8,
guide = "none" # remove legend for color aesthetic
) +
# scale_color_manual(values = PREVENTpalette1) +
labs(
title = "Indexfrågor",
subtitle = "Fördelning av svar",
y = "",
x = ""
) +
#guides(color = guide_legend(override.aes = list(size = 7))) + # make points in legend bigger
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(labels = ~ stringr::str_wrap(.x, width = 8))
```
### Mixed median and single item full response
```{r}
#| code-fold: true
#| fig-height: 7
#| fig-width: 9
mixPlot <- df.plot20 %>%
dplyr::count(item, svarskategori) %>%
mutate(nFactor = factor(n)) %>%
mutate(svarskategori = fct_rev(svarskategori))
ggplot(mixPlot %>% filter(item == "Min chef ger mig återkoppling på hur jag utför arbetet.")) +
geom_point(data = medianResponses,
aes(x = medians,
y = item),
color = prevent_green,
size = 12,
alpha = 0.4) +
geom_point(aes(x = svarskategori, y = item, size = n * 1.5, color = svarskategori),
# size = 3,
shape = 16
) +
geom_text(aes(x = svarskategori, y = item, label = n),
color = "white") +
scale_size_continuous(
range = c(7, 16), # set minimum and maximum point size
guide = "none"
) +
scale_color_viridis_d("",
begin = 0.2,
end = 0.8,
guide = "none" # remove legend for color aesthetic
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent(legend.position = "none") +
scale_color_viridis_d("",
begin = 0.2,
end = 0.8,
guide = "none" # remove legend for color aesthetic
) +
# scale_color_manual(values = PREVENTpalette1) +
labs(
title = "Indexfrågor",
subtitle = "Fördelning av svar",
y = "",
x = ""
) +
#guides(color = guide_legend(override.aes = list(size = 7))) + # make points in legend bigger
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(labels = ~ stringr::str_wrap(.x, width = 8))
```
### Median comparison
```{r}
#| code-fold: true
df.medians <- df.plot20b %>%
# create numeric responses where 1 = "Aldrig, and 6 = "Alltid"
mutate(svarNum = as.integer(fct_rev(svarskategori))) %>%
add_column(id = rep(1:17, each = 5)) %>% # sample size = 17, and 5 questions in domain
select(itemnr,svarNum,id) %>%
pivot_wider(names_from = "itemnr",
values_from = "svarNum",
id_cols = "id")
```
```{r}
#| code-fold: true
# prepare dataframe to store values
medianResponses2 <- itemlabels
# get median values (can be .5 when we have an even N)
medians <- c()
for (i in medianResponses2$itemnr) {
med1 <- median(df.medians[[i]])
medians <- c(medians,med1)
}
medianResponses2$medians <- medians
# medianComp <- rbind(medianResponses,medianResponses2) %>%
# add_column(Group = rep(1:2, each = 5))
# medianComp
```
```{r}
#| fig-height: 7
#| fig-width: 9
#| code-fold: true
ggplot() +
geom_point(data = medianResponses,
aes(x = medians,
y = item),
color = prevent_green,
size = 12,
alpha = 0.3,
position = position_nudge(x = 0.06, y = 0)
) +
geom_point(data = medianResponses2, # add new responses
aes(x = medians,
y = item),
color = prevent_green,
size = 12) +
theme_minimal() +
theme_prevent() +
labs(
title = "Indexfrågor",
subtitle = "Medianvärde per fråga",
caption = "Ljusgröna cirklar indikerar föregående mätning.\nÖverlappande cirklar indikerar samma medianvärde",
y = "",
x = ""
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(limits = svarskategorier,
labels = ~ stringr::str_wrap(.x, width = 8))
```
### Negative acts
```{r}
#| code-fold: true
# read data again for negative acts questions, "krbet"
df.krbet <- read.spss(spssDatafile, to.data.frame = TRUE) %>%
select(starts_with("q0012")) %>%
mutate(across(starts_with("q0012"), ~ car::recode(.x,"'Dagligen'='Varje vecka'"))) %>% # merge categories
na.omit()
# krbet itemlabels
krbet.itemlabels <- read_excel("data/Itemlabels.xlsx") %>%
filter(str_detect(itemnr, pattern = "kb")) %>%
select(!Dimension)
names(df.krbet) <- krbet.itemlabels$itemnr
df.plot.krbet20 <- df.krbet %>%
slice_sample(n = 20) %>%
pivot_longer(everything()) %>% # we need long format for ggplot
rename(
itemnr = name,
svarskategori = value
) %>%
left_join(krbet.itemlabels, by = "itemnr") %>% # get item descriptions as a variable in the df
add_column(group = "Mättillfälle 1")
df.plot.krbet20b <- df.krbet %>%
slice_sample(n = 20) %>%
pivot_longer(everything()) %>% # we need long format for ggplot
rename(
itemnr = name,
svarskategori = value
) %>%
left_join(krbet.itemlabels, by = "itemnr") %>% # get item descriptions as a variable in the df
add_column(group = "Mättillfälle 2")
df.krbetComp <- rbind(df.plot.krbet20,df.plot.krbet20b)
krbet.svarskategorier <- c("Aldrig","Det har hänt","Varje månad","Varje vecka")
```
```{r}
#| fig-height: 7
#| fig-width: 9
#| code-fold: true
df.plot.krbet20 %>%
dplyr::count(item, svarskategori) %>%
mutate(nFactor = factor(n)) %>%
ggplot() +
geom_point(aes(x = svarskategori, y = item, size = n * 1.5, color = svarskategori),
# size = 3,
shape = 16
) +
scale_size_continuous(
range = c(7, 18), # set minimum and maximum point size
guide = "none" # remove legend for size aesthetic
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent() +
scale_color_manual(values = c("#008332","#FBB900","#BE5014","#B01200","#B01200"),
guide = "none") +
labs(
title = "Kränkande beteenden",
subtitle = "Fördelning av svar",
y = "",
x = ""
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(breaks = krbet.svarskategorier,
limits = krbet.svarskategorier)
```
### Negativs acts comparison
```{r}
#| fig-height: 7
#| fig-width: 9
#| code-fold: true
plot.krbetComp <- df.krbetComp %>%
dplyr::count(group, item, svarskategori) %>%
mutate(nFactor = factor(n))
ggplot() +
geom_point(
data = plot.krbetComp %>% filter(group == "Mättillfälle 2"),
aes(x = svarskategori, y = item, size = n * 1.5, color = svarskategori),
shape = 16
) +
geom_point(
data = plot.krbetComp %>% filter(group == "Mättillfälle 1"),
aes(x = svarskategori, y = item, size = n * 1.5, color = svarskategori),
alpha = 0.3,
shape = 16,
position = position_nudge(x = 0.15, y = 0)
) +
scale_size_continuous(
range = c(7, 18), # set minimum and maximum point size
guide = "none" # remove legend for size aesthetic
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent() +
scale_color_manual(
values = c("#008332", "#FBB900", "#BE5014", "#B01200", "#B01200"),
guide = "none"
) +
labs(
title = "Kränkande beteenden",
subtitle = "Fördelning av svar",
y = "",
x = ""
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(
breaks = krbet.svarskategorier,
limits = krbet.svarskategorier
)
```
### Negative acts with numbers
```{r}
#| fig-height: 7
#| fig-width: 9
#| code-fold: true
df.plot.krbet20 %>%
dplyr::count(item, svarskategori) %>%
mutate(nFactor = factor(n)) %>%
ggplot() +
geom_point(aes(x = svarskategori, y = item, size = n * 1.5, color = svarskategori),
# size = 3,
shape = 16
) +
geom_text(aes(x = svarskategori, y = item, label = n),
color = "white") +
scale_size_continuous(
range = c(7, 18), # set minimum and maximum point size
guide = "none" # remove legend for size aesthetic
) +
### theming, colors, fonts, etc below
theme_minimal() +
theme_prevent() +
scale_color_manual(values = c("#008332","#FBB900","#BE5014","#B01200","#B01200"),
guide = "none") +
labs(
title = "Kränkande beteenden",
subtitle = "Fördelning av svar",
y = "",
x = ""
) +
scale_y_discrete(labels = ~ stringr::str_wrap(.x, width = 30)) +
scale_x_discrete(breaks = krbet.svarskategorier,
limits = krbet.svarskategorier)
```