-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpoLCA_plot
82 lines (71 loc) · 3.05 KB
/
poLCA_plot
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
poLCA_stacked <- function(model_name, category_labels) {
# Prepare the plot data
pp_plots <- data.frame(model_name$parameters$probability.scale) %>%
mutate(LatentClass = sub("^","Class ", LatentClass)) %>%
dplyr::select(est, LatentClass, param, category) %>%
pivot_wider(names_from = LatentClass, values_from = est) %>%
relocate(category, .after = last_col()) %>%
relocate(param, .after = last_col())
c_size <- as.data.frame(model_name$class_counts$modelEstimated$proportion) %>%
rename("cs" = 1) %>%
mutate(cs = round(cs * 100, 2))
colnames(pp_plots)[1:nrow(c_size)] <- paste0(
colnames(pp_plots)[1:nrow(c_size)],
glue(" ({c_size[1:nrow(c_size),]}%)")
)
# Prepare the plot data for ggplot
plot_data <- pp_plots %>%
pivot_longer(cols = starts_with("class"),
names_to = "Class",
values_to = "Value") %>%
mutate(category = recode(category, !!!category_labels))
# Get the model title
name <- model_name$input$title
# Create and return the plot
ggplot(plot_data, aes(x = param, y = Value, fill = category)) +
geom_bar(stat = "identity", position = "stack") +
facet_wrap(~ Class) + # Facet by Class
labs(title = paste("Class Probabilities by Item and Category"),
x = "Item",
y = "Probability") +
scale_fill_brewer(palette = "Set1", name = "Category") + # Color palette for categories
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme_cowplot()
}
poLCA_grouped <- function(model_name, category_labels) {
# Prepare the plot data
pp_plots <- data.frame(model_name$parameters$probability.scale) %>%
mutate(LatentClass = sub("^","Class ", LatentClass)) %>%
dplyr::select(est, LatentClass, param, category) %>%
pivot_wider(names_from = LatentClass, values_from = est) %>%
relocate(category, .after = last_col()) %>%
relocate(param, .after = last_col())
c_size <- as.data.frame(model_name$class_counts$modelEstimated$proportion) %>%
rename("cs" = 1) %>%
mutate(cs = round(cs * 100, 2))
colnames(pp_plots)[1:nrow(c_size)] <- paste0(
colnames(pp_plots)[1:nrow(c_size)],
glue(" ({c_size[1:nrow(c_size),]}%)")
)
# Prepare the plot data for ggplot
plot_data <- pp_plots %>%
pivot_longer(cols = starts_with("class"),
names_to = "Class",
values_to = "Value") %>%
mutate(category = recode(category, !!!category_labels))
# Get the model title
name <- model_name$input$title
# Create and return the plot
plot_data %>%
ggplot(aes(x = param, y = Value, fill = category, group = category)) +
geom_bar(stat = "identity", position = "dodge") + # Change position to dodge for grouped bars
facet_wrap(~ Class) + # Facet by Class
labs(title = "Class Probabilities by Item and Category",
x = "Item",
y = "Probability") +
scale_fill_brewer(palette = "Set1", name = "Category") + # Color palette for categories
theme_minimal() +
theme(axis.text.x = element_text(angle = 45, hjust = 1)) +
theme_cowplot()
}