forked from Chuan-Peng-Lab/CPP_Multiverse_RR
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path6_Figure4a&s1.Rmd
More file actions
304 lines (285 loc) · 18.9 KB
/
6_Figure4a&s1.Rmd
File metadata and controls
304 lines (285 loc) · 18.9 KB
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
---
title: "CPP_multiverse_RR"
author: yikang Liu
output: html_notebook
---
In our manuscript of RR, there are five figures.
Figure 1,2,3 are completed by PPT.
Figure 4a and figure s1 are completed by this script.
```{r}
library(pacman)
p_load('tidyverse','patchwork','ggridges','viridis','hrbrthemes')
```
```{r figure 4a}
# Load the raw data from a CSV file, and select columns matching "v_" while excluding columns that match "std" or "sub"
df_task1_params <- read.csv('../Data/model_trace/t_slps_traces.csv')
df_task1_vcoef <- select(df_task1_params,matches("v_"),-matches("std"),-matches("sub"))
# Calculate the 2.5th percentile, 97.5th percentile, and mean for the 3rd column
percentile_2.5 <- quantile(df_task1_vcoef[,3], 0.025)
percentile_97.5 <- quantile(df_task1_vcoef[,3], 0.975)
mean_value <- mean(df_task1_vcoef[,3])
# Create Figure 4a, showing the posterior probability density distribution of the coefficient
figure_4a <- ggplot(df_task1_vcoef, aes(x = df_task1_vcoef[, 3])) +
geom_density() +
geom_segment(aes(x = percentile_2.5, y = 0, xend = percentile_97.5, yend = 0),linewidth = 2,color = "lightblue") +
geom_point(aes(x = mean_value, y = 0),size = 1,color = "black") +geom_vline(xintercept = 0, color = "gray")+
labs(y = 'Posterior probability', x = 'Coefficient of CPP effect on drift rate') +
theme_bw() +
coord_cartesian(xlim = c(-1, 1)) +
theme(axis.text = element_text(size = 6),panel.grid = element_blank(),text = element_text(family = 'serif'),panel.spacing = unit(0.1, "cm"))
# Save Figure 4a as a PDF and png file with dimensions 10x10 cm
# ggsave("../Figure/figure4.a.pdf", width = 10, height = 10, units = "cm")
# ggsave("../Figure/figure4.a.png", width = 10, height = 10, units = "cm")
```
```{r figure s1}
# Load multiple datasets from CSV files, each representing different types of traces
df_task1_params_slps <- read.csv('../Data/model_trace/t_slps_traces.csv')
df_task1_params_ams <- read.csv('../Data/model_trace/t_ams_traces.csv')
df_task1_params_pams <- read.csv('../Data/model_trace/t_pams_traces.csv')
df_task1_params_slp_bin <- read.csv('../Data/model_trace/t_slp_bin_traces.csv')
df_task1_params_am_bin <- read.csv('../Data/model_trace/t_am_bin_traces.csv')
df_task1_params_pam_bin <- read.csv('../Data/model_trace/t_pam_bin_traces.csv')
#df_task1_params_slp_cond <- read.csv('../Data/model_trace/t_slp_cond_traces.csv')
#df_task1_params_am_cond <- read.csv('../Data/model_trace/t_am_cond_traces.csv')
#df_task1_params_pam_cond <- read.csv('../Data/model_trace/t_pam_cond_traces.csv')
```
```{r}
# Select columns containing "v_" while excluding those containing "std" or "sub"
df_task1_vcoef_slps <- select(df_task1_params_slps,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_ams <- select(df_task1_params_ams,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_pams <- select(df_task1_params_pams,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_slp_bin <- select(df_task1_params_slp_bin,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_am_bin <- select(df_task1_params_am_bin,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_pam_bin <- select(df_task1_params_pam_bin,matches("v_"),-matches("std"),-matches("sub"))
#df_task1_vcoef_slp_cond <- select(df_task1_params_slp_cond,matches("v_"),-matches("std"),-matches("sub"))
#df_task1_vcoef_am_cond <- select(df_task1_params_am_cond,matches("v_"),-matches("std"),-matches("sub"))
#df_task1_vcoef_pam_cond <- select(df_task1_params_pam_cond,matches("v_"),-matches("std"),-matches("sub"))
# Create a data frame for density plots with labels for feature type and analysis type
df_task1_vcoef_density <- data.frame(feature = factor(c(rep("slp", length(df_task1_vcoef_slps[, 3])),
rep("slp", length(df_task1_vcoef_slp_bin[, 3])),
#rep("slp", length(df_task1_vcoef_slp_cond[, 3])),
rep("am", length(df_task1_vcoef_ams[, 3])),
rep("am", length(df_task1_vcoef_am_bin[, 3])),
#rep("am", length(df_task1_vcoef_am_cond[, 3])),
rep("pam", length(df_task1_vcoef_pams[, 3])),
rep("pam", length(df_task1_vcoef_pam_bin[, 3]))
#rep("pam", length(df_task1_vcoef_pam_cond[, 3]))
)),
type = factor(c(rep("trial", length(df_task1_vcoef_slps[, 3])),
rep("bin", length(df_task1_vcoef_slp_bin[, 3])),
#rep("cond", length(df_task1_vcoef_slp_cond[, 3])),
rep("trial", length(df_task1_vcoef_ams[, 3])),
rep("bin", length(df_task1_vcoef_am_bin[, 3])),
#rep("cond", length(df_task1_vcoef_am_cond[, 3])),
rep("trial", length(df_task1_vcoef_pams[, 3])),
rep("bin", length(df_task1_vcoef_pam_bin[, 3]))
#rep("cond", length(df_task1_vcoef_pam_cond[, 3]))
)),
vcoef = c(rep(NA, 6*length(df_task1_vcoef_slps[, 3]))))
# Assign values to the 'v' column based on feature type and analysis type
df_task1_vcoef_density[df_task1_vcoef_density$feature=='slp'&df_task1_vcoef_density$type=='trial','v']=df_task1_vcoef_slps[, 3]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='am'&df_task1_vcoef_density$type=='trial','v']=df_task1_vcoef_ams[, 3]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='pam'&df_task1_vcoef_density$type=='trial','v']=df_task1_vcoef_pams[, 3]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='slp'&df_task1_vcoef_density$type=='bin','v']=df_task1_vcoef_slp_bin[, 3]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='am'&df_task1_vcoef_density$type=='bin','v']=df_task1_vcoef_am_bin[, 3]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='pam'&df_task1_vcoef_density$type=='bin','v']=df_task1_vcoef_pam_bin[, 3]
#df_task1_vcoef_density[df_task1_vcoef_density$feature=='slp'&df_task1_vcoef_density$type=='cond','v']=df_task1_vcoef_slp_cond[, 3]
#df_task1_vcoef_density[df_task1_vcoef_density$feature=='am'&df_task1_vcoef_density$type=='cond','v']=df_task1_vcoef_am_cond[, 3]
#df_task1_vcoef_density[df_task1_vcoef_density$feature=='pam'&df_task1_vcoef_density$type=='cond','v']=df_task1_vcoef_pam_cond[, 3]
# Re-label the 'feature' and 'type' columns for better readability in plots
df_task1_vcoef_density$feature <- factor(df_task1_vcoef_density$feature, levels = c("slp", "am", "pam"),labels = c("Build-up rate", "Amplitude", "Peak amplitude"))
df_task1_vcoef_density$type <- factor(df_task1_vcoef_density$type, levels = c("trial", "bin"),labels = c("Trial", "bin"))
# Aggregate data by type and feature, calculate averages, and prepare for plotting
df_task1_vcoef_density_agg <- df_task1_vcoef_density %>% group_by(type,feature) %>% summarise(avg_v = mean(v)) %>% left_join(df_task1_vcoef_density, by = c("type","feature")) %>% mutate(pipeline = factor(paste(type, feature, sep = "-")))
df_task1_vcoef_density_agg$pipeline <- factor(df_task1_vcoef_density_agg$pipeline,levels = unique(df_task1_vcoef_density_agg$pipeline))
# Calculate the 2.5th and 97.5th percentiles, as well as the mean for each pipeline
df_task1_vcoef_density_stats <- df_task1_vcoef_density_agg %>% group_by(pipeline) %>% summarise(percentile_2.5 = quantile(v, 0.025), percentile_97.5 = quantile(v, 0.975), mean = mean(v)) %>% arrange(mean) %>% ungroup() %>% mutate(pipeline = factor(pipeline))
# Create label data
# This code creates a data frame named 'pipeline_labels' that contains unique pipeline names from another data frame
# and corresponding custom labels. These labels will be used to annotate the plot later.
pipeline_labels <- data.frame(
pipeline = unique(df_task1_vcoef_density_agg$pipeline),
label = c('Trialwise Build-up rate',
'Trialwise Amplitude',
'Trialwise Peak amplitude',
'Binwise Build-up rate',
'Binwise Amplitude',
'Binwise Peak amplitude')
)
# Create a ggplot object named 'figure_s1'
# This plot visualizes the density of the variable 'v' from the data frame 'df_task1_vcoef_density_agg'.
# It also adds some statistical elements and customizes the appearance of the plot.
figure_s1 <- ggplot(df_task1_vcoef_density_agg, aes(x = v)) +
# Add a density plot layer
geom_density() +
# Add a horizontal segment representing the 2.5th to 97.5th percentiles
geom_segment(
data = df_task1_vcoef_density_stats,
aes(x = percentile_2.5, y = 0, xend = percentile_97.5, yend = 0),
linewidth = 2, color = "lightblue"
) +
# Add a point representing the mean value
geom_point(
data = df_task1_vcoef_density_stats,
aes(x = mean, y = 0), size = 1, color = "black"
) +
# Create a grid of sub - plots based on the 'pipeline' variable
# Each sub - plot has its own scale and the y - axis label is switched to the left
facet_grid(rows = vars(pipeline), scales = "free", switch = "y") +
# Add a vertical line at x = 0
geom_vline(xintercept = 0, color = "gray") +
# Set the labels for the x and y axes
labs(y = 'Posterior probability', x = 'Coefficient of CPP effect on drift rate') +
# Apply a black - and - white theme to the plot
theme_bw() +
# Set the x - axis limits and turn off clipping
coord_cartesian(xlim = c(-1, 1), clip = "off") +
# Customize the theme elements
theme(
axis.text = element_text(size = 8),
panel.grid = element_blank(),
text = element_text(family = 'serif'),
panel.spacing = unit(0.2, "cm"), # Increase the spacing between facets
strip.text.y = element_blank(),
strip.placement = "outside",
strip.background = element_blank(),
plot.margin = margin(r = 200) # Leave blank space on the right side of the plot
) +
# Add text annotations using the 'pipeline_labels' data frame
geom_text(
data = pipeline_labels,
aes(x = Inf, y = 0, label = label),
hjust = -0.1, # Move the text to the right
vjust = -2, # Fine - tune the text position downward
size = 4,
color = "black",
family = 'serif'
)
# Print the plot
print(figure_s1)
print(df_task1_vcoef_density_stats)
# Save the plot as PDF and PNG with specified dimensions
# These lines are currently commented out. Uncomment them to save the plot in the specified formats and dimensions.
# ggsave("./Figure/figure_s1.pdf", width = 10, height = 10, units = "cm")
# ggsave("./Figure/figure_s1.png", width = 10, height = 10, units = "cm")
```
```{r}
# Select columns containing "v_" while excluding those containing "std" or "sub"
df_task1_vcoef_slps <- select(df_task1_params_slps,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_ams <- select(df_task1_params_ams,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_pams <- select(df_task1_params_pams,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_slp_bin <- select(df_task1_params_slp_bin,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_am_bin <- select(df_task1_params_am_bin,matches("v_"),-matches("std"),-matches("sub"))
df_task1_vcoef_pam_bin <- select(df_task1_params_pam_bin,matches("v_"),-matches("std"),-matches("sub"))
#df_task1_vcoef_slp_cond <- select(df_task1_params_slp_cond,matches("v_"),-matches("std"),-matches("sub"))
#df_task1_vcoef_am_cond <- select(df_task1_params_am_cond,matches("v_"),-matches("std"),-matches("sub"))
#df_task1_vcoef_pam_cond <- select(df_task1_params_pam_cond,matches("v_"),-matches("std"),-matches("sub"))
# Create a data frame for density plots with labels for feature type and analysis type
df_task1_vcoef_density <- data.frame(feature = factor(c(rep("slp", length(df_task1_vcoef_slps[, 4])),
rep("slp", length(df_task1_vcoef_slp_bin[, 4])),
#rep("slp", length(df_task1_vcoef_slp_cond[, 4])),
rep("am", length(df_task1_vcoef_ams[, 4])),
rep("am", length(df_task1_vcoef_am_bin[, 4])),
#rep("am", length(df_task1_vcoef_am_cond[, 3])),
rep("pam", length(df_task1_vcoef_pams[, 4])),
rep("pam", length(df_task1_vcoef_pam_bin[, 4]))
#rep("pam", length(df_task1_vcoef_pam_cond[, 3]))
)),
type = factor(c(rep("trial", length(df_task1_vcoef_slps[, 4])),
rep("bin", length(df_task1_vcoef_slp_bin[, 4])),
#rep("cond", length(df_task1_vcoef_slp_cond[, 3])),
rep("trial", length(df_task1_vcoef_ams[, 4])),
rep("bin", length(df_task1_vcoef_am_bin[, 4])),
#rep("cond", length(df_task1_vcoef_am_cond[, 3])),
rep("trial", length(df_task1_vcoef_pams[, 4])),
rep("bin", length(df_task1_vcoef_pam_bin[, 4]))
#rep("cond", length(df_task1_vcoef_pam_cond[, 3]))
)),
vcoef = c(rep(NA, 6*length(df_task1_vcoef_slps[, 4]))))
# Assign values to the 'v' column based on feature type and analysis type
df_task1_vcoef_density[df_task1_vcoef_density$feature=='slp'&df_task1_vcoef_density$type=='trial','v']=df_task1_vcoef_slps[, 4]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='am'&df_task1_vcoef_density$type=='trial','v']=df_task1_vcoef_ams[, 4]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='pam'&df_task1_vcoef_density$type=='trial','v']=df_task1_vcoef_pams[, 4]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='slp'&df_task1_vcoef_density$type=='bin','v']=df_task1_vcoef_slp_bin[, 4]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='am'&df_task1_vcoef_density$type=='bin','v']=df_task1_vcoef_am_bin[, 4]
df_task1_vcoef_density[df_task1_vcoef_density$feature=='pam'&df_task1_vcoef_density$type=='bin','v']=df_task1_vcoef_pam_bin[, 4]
#df_task1_vcoef_density[df_task1_vcoef_density$feature=='slp'&df_task1_vcoef_density$type=='cond','v']=df_task1_vcoef_slp_cond[, 3]
#df_task1_vcoef_density[df_task1_vcoef_density$feature=='am'&df_task1_vcoef_density$type=='cond','v']=df_task1_vcoef_am_cond[, 3]
#df_task1_vcoef_density[df_task1_vcoef_density$feature=='pam'&df_task1_vcoef_density$type=='cond','v']=df_task1_vcoef_pam_cond[, 3]
# Re-label the 'feature' and 'type' columns for better readability in plots
df_task1_vcoef_density$feature <- factor(df_task1_vcoef_density$feature, levels = c("slp", "am", "pam"),labels = c("Build-up rate", "Amplitude", "Peak amplitude"))
df_task1_vcoef_density$type <- factor(df_task1_vcoef_density$type, levels = c("trial", "bin"),labels = c("Trial", "bin"))
# Aggregate data by type and feature, calculate averages, and prepare for plotting
df_task1_vcoef_density_agg <- df_task1_vcoef_density %>% group_by(type,feature) %>% summarise(avg_v = mean(v)) %>% left_join(df_task1_vcoef_density, by = c("type","feature")) %>% mutate(pipeline = factor(paste(type, feature, sep = "-")))
df_task1_vcoef_density_agg$pipeline <- factor(df_task1_vcoef_density_agg$pipeline,levels = unique(df_task1_vcoef_density_agg$pipeline))
# Calculate the 2.5th and 97.5th percentiles, as well as the mean for each pipeline
df_task1_vcoef_density_stats <- df_task1_vcoef_density_agg %>% group_by(pipeline) %>% summarise(percentile_2.5 = quantile(v, 0.025), percentile_97.5 = quantile(v, 0.975), mean = mean(v)) %>% arrange(mean) %>% ungroup() %>% mutate(pipeline = factor(pipeline))
# Create label data
# This code creates a data frame named 'pipeline_labels' that contains unique pipeline names from another data frame
# and corresponding custom labels. These labels will be used to annotate the plot later.
pipeline_labels <- data.frame(
pipeline = unique(df_task1_vcoef_density_agg$pipeline),
label = c('Trialwise Build-up rate',
'Trialwise Amplitude',
'Trialwise Peak amplitude',
'Binwise Build-up rate',
'Binwise Amplitude',
'Binwise Peak amplitude')
)
# Create a ggplot object named 'figure_s1'
# This plot visualizes the density of the variable 'v' from the data frame 'df_task1_vcoef_density_agg'.
# It also adds some statistical elements and customizes the appearance of the plot.
figure_s1 <- ggplot(df_task1_vcoef_density_agg, aes(x = v)) +
# Add a density plot layer
geom_density() +
# Add a horizontal segment representing the 2.5th to 97.5th percentiles
geom_segment(
data = df_task1_vcoef_density_stats,
aes(x = percentile_2.5, y = 0, xend = percentile_97.5, yend = 0),
linewidth = 2, color = "pink"
) +
# Add a point representing the mean value
geom_point(
data = df_task1_vcoef_density_stats,
aes(x = mean, y = 0), size = 1, color = "black"
) +
# Create a grid of sub-plots based on the 'pipeline' variable
facet_grid(rows = vars(pipeline), scales = "free", switch = "y") +
# Add a vertical line at x = 0
geom_vline(xintercept = 0, color = "gray") +
# Set the labels for the x and y axes
labs(y = 'Posterior probability', x = 'Coefficient of interaction effect on drift rate') +
# Apply a black-and-white theme to the plot
theme_bw() +
# Set the x-axis limits and turn off clipping
coord_cartesian(xlim = c(-1, 1), clip = "off") +
# Add text annotations using the 'pipeline_labels' data frame
geom_text(
data = pipeline_labels,
aes(x = Inf, y = 0, label = label), # 定位到右侧边缘
hjust = -0.05, # 向左微调(负值向右,正值向左)
vjust = -2, # 垂直居中调整
size = 4,
color = "black",
family = 'serif',
check_overlap = TRUE # 避免标签重叠
) +
# Customize the theme elements
theme(
plot.margin = margin(t = 20, r = 200, b = 20, l = 20), # 右侧留足空间
axis.text = element_text(size = 8),
panel.grid = element_blank(),
text = element_text(family = 'serif'),
panel.spacing = unit(0.2, "cm"),
strip.text.y = element_blank(),
strip.placement = "outside",
strip.background = element_blank()
)
# Print the plot
print(figure_s1)
print(df_task1_vcoef_density_stats)
# Save the plot as PDF and PNG with specified dimensions
# Uncomment these lines to save the plot in the specified formats and dimensions.
# ggsave("./Figure/figure_s1.pdf", width = 10, height = 10, units = "cm")
# ggsave("./Figure/figure_s1.png", width = 10, height = 10, units = "cm")
```