# sets the directory of location of this script as the current directory
# setwd(dirname(rstudioapi::getSourceEditorContext()$path))
### load packages
require(pacman)
p_load('jsonlite', 'xlsx', 'ggpubr',
'stargazer', 'DT', 'tools', 'tidyverse')
### load abbrevations dictionary
setwd("data")
<- jsonlite::fromJSON(txt = "abbreviations_dict.txt")
abbreviations_dict
### load frequency table of codes
setwd("../../output/G3")
#> RR
<- xlsx::read.xlsx2(file = "rescue robot frequency table codes.xlsx", sheetIndex = 1)
codes_frequency_RR
$Code <- str_replace_all(
codes_frequency_RRstring = codes_frequency_RR$Code,
pattern = "_",
replacement = " ")
$Code <- tools::toTitleCase(codes_frequency_RR$Code)
codes_frequency_RR
$soft <- as.numeric(codes_frequency_RR$soft)
codes_frequency_RR$rigid <- as.numeric(codes_frequency_RR$rigid)
codes_frequency_RR
$soft <- codes_frequency_RR$soft + codes_frequency_RR$rigid
codes_frequency_RR
#> SAR
<- xlsx::read.xlsx2(file = "socially assistive robot frequency table codes.xlsx", sheetIndex = 1)
codes_frequency_SAR
$Code <- str_replace_all(
codes_frequency_SARstring = codes_frequency_SAR$Code,
pattern = "_",
replacement = " ")
$Code <- tools::toTitleCase(codes_frequency_SAR$Code)
codes_frequency_SAR
$soft <- as.numeric(codes_frequency_SAR$soft)
codes_frequency_SAR$rigid <- as.numeric(codes_frequency_SAR$rigid)
codes_frequency_SAR
$soft <- codes_frequency_SAR$soft + codes_frequency_SAR$rigid
codes_frequency_SAR
### load improved coding guidelines
setwd("json RR - improved")
# List all JSON files in the directory
<- list.files(pattern = "json$", full.names = FALSE)
json_files # Initialize an empty list to store code descriptions
<- list()
list_json_files_RR # Loop through each JSON file
for (i in 1:length(json_files)) {
# Read the JSON file
<- jsonlite::fromJSON(json_files[i])
list_json_files_RR[[i]]
}names(list_json_files_RR) <- json_files
setwd("../json SAR - improved")
# List all JSON files in the directory
<- list.files(pattern = "json$", full.names = FALSE)
json_files # Initialize an empty list to store code descriptions
<- list()
list_json_files_SAR # Loop through each JSON file
for (i in 1:length(json_files)) {
# Read the JSON file
<- jsonlite::fromJSON(json_files[i])
list_json_files_SAR[[i]]
}names(list_json_files_SAR) <- json_files
rm(i); rm(json_files)
Create tables and graphics for G3 (result part II)
Notes
load cleaned data files
Create table for Supplementary Materials
for RR
# Create an empty data frame to store results
<- data.frame(
results_df CategoryAbbrevation = character(),
Category = character(),
CodeName = character(),
Description = character(),
stringsAsFactors = FALSE
)
# Loop through the JSON files
for (i in 1:length(list_json_files_RR)) {
# Improve code names
names(list_json_files_RR[[i]]$code_descriptions) <- str_replace_all(
string = names(list_json_files_RR[[i]]$code_descriptions),
pattern = "_",
replacement = " "
)names(list_json_files_RR[[i]]$code_descriptions) <- tools::toTitleCase(
names(list_json_files_RR[[i]]$code_descriptions)
)
# Get full name of category
<- names(abbreviations_dict$abbreviations_dict[
tmp_categoryAbbrevation names(abbreviations_dict$abbreviations_dict) == list_json_files_RR[[i]]$category])
<- abbreviations_dict$abbreviations_dict[
tmp_category names(abbreviations_dict$abbreviations_dict) == list_json_files_RR[[i]]$category
1]]
][[
# Loop through each code description
for (j in 1:length(list_json_files_RR[[i]]$code_descriptions)) {
# Extract the code name and description
<- names(list_json_files_RR[[i]]$code_descriptions)[j]
code_name <- list_json_files_RR[[i]]$code_descriptions[j][[1]]
description
# Append to the results data frame
<- rbind(
results_df
results_df,data.frame(
CategoryAbbrevation = tmp_categoryAbbrevation,
Category = tmp_category,
CodeName = code_name,
Description = description,
stringsAsFactors = FALSE
)
)
}
}
colnames(results_df) <- c("Abbrevation", "Category", "Code", "Description")
$soft <- NA
results_df$rigid <- NA
results_df
### add frequencies
for(c in unique(results_df$Abbrevation)){
<- codes_frequency_RR[codes_frequency_RR$Category == c, ]
tmp_codes_frequency for(i in 1:nrow(tmp_codes_frequency)){
$soft[results_df$Abbrevation == c & results_df$Code == tmp_codes_frequency[i,"Code"]] <- tmp_codes_frequency[i,"soft"]
results_df
$rigid[results_df$Abbrevation == c & results_df$Code == tmp_codes_frequency[i,"Code"]] <- tmp_codes_frequency[i,"rigid"]
results_df
}
}
### save overall table
::write.xlsx2(x = results_df, file = "outputs/RR_codes_complete.xlsx")
xlsx
### sort according to frequency soft, and save top 20
<- results_df[order(results_df$soft, decreasing = TRUE),]
results_df_sorted ::write.xlsx2(x = results_df_sorted[1:20, ], file = "outputs/RR_codes_first20.xlsx")
xlsx
### backup:
<- results_df
results_df_RR
### show overall table
::datatable(data = results_df_RR) DT
for SAR
# Create an empty data frame to store results
<- data.frame(
results_df CategoryAbbrevation = character(),
Category = character(),
CodeName = character(),
Description = character(),
stringsAsFactors = FALSE
)
# Loop through the JSON files
for (i in 1:length(list_json_files_SAR)) {
# Improve code names
names(list_json_files_SAR[[i]]$code_descriptions) <- str_replace_all(
string = names(list_json_files_SAR[[i]]$code_descriptions),
pattern = "_",
replacement = " "
)names(list_json_files_SAR[[i]]$code_descriptions) <- tools::toTitleCase(
names(list_json_files_SAR[[i]]$code_descriptions)
)
# Get full name of category
<- names(abbreviations_dict$abbreviations_dict[
tmp_categoryAbbrevation names(abbreviations_dict$abbreviations_dict) == list_json_files_SAR[[i]]$category])
<- abbreviations_dict$abbreviations_dict[
tmp_category names(abbreviations_dict$abbreviations_dict) == list_json_files_SAR[[i]]$category
1]]
][[
# Loop through each code description
for (j in 1:length(list_json_files_SAR[[i]]$code_descriptions)) {
# Extract the code name and description
<- names(list_json_files_SAR[[i]]$code_descriptions)[j]
code_name <- list_json_files_SAR[[i]]$code_descriptions[j][[1]]
description
# Append to the results data frame
<- rbind(
results_df
results_df,data.frame(
CategoryAbbrevation = tmp_categoryAbbrevation,
Category = tmp_category,
CodeName = code_name,
Description = description,
stringsAsFactors = FALSE
)
)
}
}
colnames(results_df) <- c("Abbrevation", "Category", "Code", "Description")
$soft <- NA
results_df$rigid <- NA
results_df
### add frequencies
for(c in unique(results_df$Abbrevation)){
<- codes_frequency_SAR[codes_frequency_SAR$Category == c, ]
tmp_codes_frequency for(i in 1:nrow(tmp_codes_frequency)){
$soft[results_df$Abbrevation == c & results_df$Code == tmp_codes_frequency[i,"Code"]] <- tmp_codes_frequency[i,"soft"]
results_df
$rigid[results_df$Abbrevation == c & results_df$Code == tmp_codes_frequency[i,"Code"]] <- tmp_codes_frequency[i,"rigid"]
results_df
}
}
### save overall table
::write.xlsx2(x = results_df, file = "outputs/SAR_codes_complete.xlsx")
xlsx
### sort according to frequency soft, and save top 20
<- results_df[order(results_df$soft, decreasing = TRUE),]
results_df_sorted ::write.xlsx2(x = results_df_sorted[1:20, ], file = "outputs/SAR_codes_first20.xlsx")
xlsx
### backup:
<- results_df
results_df_SAR
### show overall table
::datatable(data = results_df_SAR) DT
Create plots for Main Article
for RR
filter for most frequent soft terms:
for(c in unique(results_df_RR$Abbrevation)){
<- results_df_RR[results_df_RR$Abbrevation == c, ]
tmp_frequency <- tmp_frequency[!is.na(tmp_frequency$soft),]
tmp_frequency <- tmp_frequency[tmp_frequency$soft >= median(tmp_frequency$soft, na.rm = TRUE),]
tmp_frequency
$Abbrevation <- NULL
tmp_frequency$Description <- NULL
tmp_frequency
<- paste0(unique(tmp_frequency$Category), collapse = " ") # " - rescue robots"
tmp_title
cat("Plot for:\n", tmp_title, "\n")
# Insert line breaks into long category names
$Code <- str_wrap(tmp_frequency$Code, width = 20)
tmp_frequency
# Reshape data for ggplot (long format)
<- reshape2::melt(tmp_frequency, id.vars = c("Category", "Code"),
tmp_long variable.name = "Type", value.name = "Count")
# Create the plot
<- ggplot(tmp_long, aes(y = factor(Code, levels = rev(unique(Code))),
plot x = Count, fill = Type)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7)) + # Increase bar spacing
labs(
title = tmp_title,
y = "Category",
x = "Frequency",
fill = "Type"
+
) theme_pubr() + # APA-like theme
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 14),
axis.title.y = element_blank(),
axis.text.y = element_text(size = 12, hjust = 1), # Adjust y-axis text alignment
axis.text.x = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(), # Remove grid lines for clarity
panel.grid.minor = element_blank()
+
) scale_y_discrete(labels = function(labels) str_wrap(labels, width = 20)) # Ensure proper wrapping
# Display the plot
print(plot)
}
Plot for:
perceived negative anthropomorphism
Plot for:
perceived positive anthropomorphism
Plot for:
perceived negative Human-Robot-Interaction
Plot for:
perceived positive Human-Robot-Interaction
Plot for:
perceived risks
Plot for:
perceived safety
Plot for:
perceived technological limitations
Plot for:
perceived technological possibilities
do not filter for most frequent soft terms:
setwd("outputs/G3_RR")
for(c in unique(results_df_RR$Abbrevation)){
<- results_df_RR[results_df_RR$Abbrevation == c, ]
tmp_frequency <- tmp_frequency[!is.na(tmp_frequency$soft),]
tmp_frequency # tmp_frequency <- tmp_frequency[tmp_frequency$soft >= median(tmp_frequency$soft, na.rm = TRUE),]
$Abbrevation <- NULL
tmp_frequency$Description <- NULL
tmp_frequency
<- paste0(unique(tmp_frequency$Category), collapse = " ") # " - rescue robots"
tmp_title
cat("Plot for:\n", tmp_title, "\n")
# Insert line breaks into long category names
$Code <- str_wrap(tmp_frequency$Code, width = 20)
tmp_frequency
# Reshape data for ggplot (long format)
<- reshape2::melt(tmp_frequency, id.vars = c("Category", "Code"),
tmp_long variable.name = "Type", value.name = "Count")
# Create the plot
<- ggplot(tmp_long, aes(y = factor(Code, levels = rev(unique(Code))),
plot x = Count, fill = Type)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7)) + # Increase bar spacing
labs(
title = tmp_title,
y = "Category",
x = "Frequency",
fill = "Type"
+
) theme_pubr() + # APA-like theme
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 14),
axis.title.y = element_blank(),
axis.text.y = element_text(size = 12, hjust = 1), # Adjust y-axis text alignment
axis.text.x = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(), # Remove grid lines for clarity
panel.grid.minor = element_blank()
+
) scale_y_discrete(labels = function(labels) str_wrap(labels, width = 20)) # Ensure proper wrapping
# Display the plot
print(plot)
ggsave(paste0(tmp_title, ".pdf"))
}
Plot for:
perceived negative anthropomorphism
Saving 7 x 5 in image
Plot for:
perceived positive anthropomorphism
Saving 7 x 5 in image
Plot for:
perceived negative Human-Robot-Interaction
Saving 7 x 5 in image
Plot for:
perceived positive Human-Robot-Interaction
Saving 7 x 5 in image
Plot for:
perceived risks
Saving 7 x 5 in image
Plot for:
perceived safety
Saving 7 x 5 in image
Plot for:
perceived technological limitations
Saving 7 x 5 in image
Plot for:
perceived technological possibilities
Saving 7 x 5 in image
for SAR
filter for most frequent soft terms:
for(c in unique(results_df_SAR$Abbrevation)){
<- results_df_SAR[results_df_SAR$Abbrevation == c, ]
tmp_frequency <- tmp_frequency[!is.na(tmp_frequency$soft),]
tmp_frequency <- tmp_frequency[tmp_frequency$soft >= median(tmp_frequency$soft, na.rm = TRUE),]
tmp_frequency
$Abbrevation <- NULL
tmp_frequency$Description <- NULL
tmp_frequency
<- paste0(unique(tmp_frequency$Category), collapse = " ") # " - rescue robots"
tmp_title
cat("Plot for:\n", tmp_title, "\n")
# Insert line breaks into long category names
$Code <- str_wrap(tmp_frequency$Code, width = 20)
tmp_frequency
# Reshape data for ggplot (long format)
<- reshape2::melt(tmp_frequency, id.vars = c("Category", "Code"),
tmp_long variable.name = "Type", value.name = "Count")
# Create the plot
<- ggplot(tmp_long, aes(y = factor(Code, levels = rev(unique(Code))),
plot x = Count, fill = Type)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7)) + # Increase bar spacing
labs(
title = tmp_title,
y = "Category",
x = "Frequency",
fill = "Type"
+
) theme_pubr() + # APA-like theme
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 14),
axis.title.y = element_blank(),
axis.text.y = element_text(size = 12, hjust = 1), # Adjust y-axis text alignment
axis.text.x = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(), # Remove grid lines for clarity
panel.grid.minor = element_blank()
+
) scale_y_discrete(labels = function(labels) str_wrap(labels, width = 20)) # Ensure proper wrapping
# Display the plot
print(plot)
}
Plot for:
perceived negative anthropomorphism
Plot for:
perceived positive anthropomorphism
Plot for:
perceived negative Human-Robot-Interaction
Plot for:
perceived positive Human-Robot-Interaction
Plot for:
perceived risks
Plot for:
perceived safety
Plot for:
perceived technological limitations
Plot for:
perceived technological possibilities
do not filter for most frequent soft terms:
setwd("outputs/G3_SAR")
for(c in unique(results_df_SAR$Abbrevation)){
<- results_df_SAR[results_df_SAR$Abbrevation == c, ]
tmp_frequency <- tmp_frequency[!is.na(tmp_frequency$soft),]
tmp_frequency # tmp_frequency <- tmp_frequency[tmp_frequency$soft >= median(tmp_frequency$soft, na.rm = TRUE),]
$Abbrevation <- NULL
tmp_frequency$Description <- NULL
tmp_frequency
<- paste0(unique(tmp_frequency$Category), collapse = " ") # " - rescue robots"
tmp_title
cat("Plot for:\n", tmp_title, "\n")
# Insert line breaks into long category names
$Code <- str_wrap(tmp_frequency$Code, width = 20)
tmp_frequency
# Reshape data for ggplot (long format)
<- reshape2::melt(tmp_frequency, id.vars = c("Category", "Code"),
tmp_long variable.name = "Type", value.name = "Count")
# Create the plot
<- ggplot(tmp_long, aes(y = factor(Code, levels = rev(unique(Code))),
plot x = Count, fill = Type)) +
geom_bar(stat = "identity", position = position_dodge(width = 0.7)) + # Increase bar spacing
labs(
title = tmp_title,
y = "Category",
x = "Frequency",
fill = "Type"
+
) theme_pubr() + # APA-like theme
theme(
plot.title = element_text(size = 16, face = "bold"),
axis.title = element_text(size = 14),
axis.title.y = element_blank(),
axis.text.y = element_text(size = 12, hjust = 1), # Adjust y-axis text alignment
axis.text.x = element_text(size = 14),
legend.title = element_text(size = 12),
legend.text = element_text(size = 10),
panel.grid.major = element_blank(), # Remove grid lines for clarity
panel.grid.minor = element_blank()
+
) scale_y_discrete(labels = function(labels) str_wrap(labels, width = 20)) # Ensure proper wrapping
# Display the plot
print(plot)
ggsave(paste0(tmp_title, ".pdf"))
}
Plot for:
perceived negative anthropomorphism
Saving 7 x 5 in image
Plot for:
perceived positive anthropomorphism
Saving 7 x 5 in image
Plot for:
perceived negative Human-Robot-Interaction
Saving 7 x 5 in image
Plot for:
perceived positive Human-Robot-Interaction
Saving 7 x 5 in image
Plot for:
perceived risks
Saving 7 x 5 in image
Plot for:
perceived safety
Saving 7 x 5 in image
Plot for:
perceived technological limitations
Saving 7 x 5 in image
Plot for:
perceived technological possibilities
Saving 7 x 5 in image