-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplot_scopus_comparison.R
87 lines (72 loc) · 3.99 KB
/
plot_scopus_comparison.R
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
# Plot output from scopus_comparison()
plot_scopus_comparison =
function( input, # results from `scopus_comparison()`
pub_count_in_legend = TRUE, # include publication counts in the legend
pub_count_in_lines = FALSE # include publication counts in the lines
) {
require(stringr)
require(ggplot2)
require(geomtextpath)
require(ggtext)
# Colour reference query in dark blue
input = input %>% mutate(
abridged_query_total_publications =
case_when(query_type == 'reference' ~
abridged_query_total_publications %>%
str_replace("^'", "'<span style = 'color: darkblue;'>") %>%
str_replace(fixed("' ["), "</span>' ["),
.default = abridged_query_total_publications)
)
# Sort queries by their average percentage rank throughout search_period
query_order =
input %>% arrange(-average_comparison_percentage) %>%
pull(abridged_query_total_publications) %>% unique()
input$abridged_query_total_publications =
factor(input$abridged_query_total_publications, levels = query_order)
# Based on inputs, tailor appearance of publication counts
colour_variable = ifelse(pub_count_in_legend,
'abridged_query_total_publications', # if TRUE
'abridged_query') # if FALSE
label_variable = ifelse(pub_count_in_lines,
'abridged_query_total_publications', # if TRUE
'abridged_query') # if FALSE
# Plot
input %>%
# Select comparison queries only
filter(query_type == 'comparison') %>%
ggplot(aes(year, comparison_percentage, colour = get(colour_variable))) +
scale_x_continuous(breaks = scales::breaks_pretty(10), expand = expansion(0.01)) +
scale_y_continuous(expand = expansion(0.02), n.breaks = 8,
labels = scales::label_percent(accuracy = 1, scale = 1)) +
geom_line(linewidth = 2, alpha = 0.12,
# https://github.com/tidyverse/ggplot2/issues/5728#issuecomment-1970036164
show.legend = c(colour = TRUE)) +
guides(colour = guide_legend(override.aes = list(alpha = 1))) +
geom_textpath(aes(label = get(label_variable),
color = get(colour_variable)),
linetype = 0, text_smoothing = 40, spacing = 80,
show.legend = FALSE) +
ggtitle( paste0( 'Comparisons to reference query ',
input %>%
filter(query_type == 'reference') %>%
pull(abridged_query_total_publications) %>%
unique() ) ) +
ylab(paste0('% relative to ', input[1, 'abridged_query_total_publications'])) +
xlab('Year') + ylab('Percentage of publications relative to reference query') +
theme_minimal() +
theme(plot.title.position = 'plot', plot.title = element_markdown(hjust = 0.5),
plot.subtitle = element_text(colour = 'grey55', hjust = .5,
margin = margin(3, 0, 2, 0)),
axis.title.x = element_text(margin = margin(5, 0, 0, 0, 'pt')),
axis.title.y = element_text(margin = margin(0, 6, 0, 0, 'pt')),
axis.text.x = element_text(margin = margin(0, 0, 2, 0)),
axis.text.y = element_text(margin = margin(0, 0, 0, 2)),
legend.text = element_text(margin = margin(4, 0, 4, 0)),
legend.margin = margin(-2, 19, 6, 3),
legend.box.margin = margin(5, 1, 0, 1),
legend.title = element_blank(), legend.direction = 'vertical',
legend.key.width = unit(25, 'pt'), legend.position = 'bottom',
legend.background = element_rect(color = 'grey90', fill = 'grey98'),
panel.grid.minor = element_blank(),
plot.margin = unit(c(8, 0, 8, 8), 'pt'))
}