Adrià Vilanova Martínez | c2d9f31 | 2023-04-23 11:59:30 +0200 | [diff] [blame] | 1 | library(ggplot2) |
| 2 | library(svglite) |
| 3 | library(zoo) |
| 4 | library(dplyr) |
| 5 | |
| 6 | inflect <- function(x, threshold = 1){ |
| 7 | up <- sapply(1:threshold, function(n) c(x[-(seq(n))], rep(NA, n))) |
| 8 | down <- sapply(-1:-threshold, function(n) c(rep(NA,abs(n)), x[-seq(length(x), length(x) - abs(n) + 1)])) |
| 9 | a <- cbind(x,up,down) |
| 10 | list(minima = which(apply(a, 1, min) == a[,1]), maxima = which(apply(a, 1, max) == a[,1])) |
| 11 | } |
| 12 | |
| 13 | drawMinimums <- TRUE |
| 14 | drawSmoothedCurve <- FALSE |
| 15 | if (drawMinimums) { |
| 16 | filenames <- c('160K_uh8.5_2', '170K_uh8.5', '180K_uh8.5', '190K_uh8.5', '190K_uh7.5', '200K_uh8.5') |
| 17 | } else { |
| 18 | filenames <- c('160K_uh7.5', '160K_uh8.5_3', '170K_uh7.5', '180K_uh7.5', '190K_uh7.5', '200K_uh8.5', '160K_uh8.5_2', '160K_uh8.5', '170K_uh8.5', '180K_uh8.5', '190K_uh8.5') |
| 19 | } |
| 20 | dependanceOnTFilenames <- c('160K_uh8.5_2', '170K_uh8.5', '180K_uh8.5', |
| 21 | #'190K_uh8.5', |
| 22 | '190K_uh7.5', '200K_uh8.5') |
| 23 | |
| 24 | for (filename in filenames) { |
| 25 | temp <- gsub("(\\d+)K_.*", "\\1", filename) |
| 26 | uh <- gsub(".*_uh([^_]+).*", "\\1", filename) |
| 27 | table <- read.table(paste0('dades/', filename, '.txt'), sep="\t", dec=',', header=TRUE) |
| 28 | table$Smooth_IA <- rollmean(table$IA, k = 60, align = "center", fill = NA) |
| 29 | bottoms <- inflect(table$Smooth_IA, threshold = 2)$minima |
| 30 | #print(bquote("T =" ~ .(temp) ~ "K;" ~ "U"[h]~"=" ~ .(uh) ~ "V")) |
| 31 | #print(table$U1[bottoms]) |
| 32 | pplot <- ggplot(table, aes(x = U1, y = Smooth_IA)) |
| 33 | if (drawMinimums) { |
| 34 | min_vals <- read.table(paste0('dades/', filename, '_minims.txt'), sep="\t", dec=",", header=TRUE) |
| 35 | if (drawSmoothedCurve) { |
| 36 | pplot <- pplot + |
| 37 | geom_line(aes(x = U1, y = IA), colour = "#FCC8C5") + |
| 38 | geom_line(colour = "#F8766D") + |
| 39 | geom_vline(data = min_vals, aes(xintercept = U1), linetype = "dashed", color="#FAA49E") |
| 40 | } else { |
| 41 | pplot <- pplot + |
| 42 | geom_line(aes(x = U1, y = IA), colour = "#F8766D") + |
| 43 | geom_vline(data = min_vals, aes(xintercept = U1), linetype = "dashed", color="#FAA49E") |
| 44 | } |
| 45 | } else { |
| 46 | pplot <- pplot + |
| 47 | geom_line(aes(x = U1, y = IA), colour = "#F8766D") |
| 48 | } |
| 49 | pplot <- pplot + |
| 50 | xlab(bquote(U["1"] ~ "(V)")) + |
| 51 | ylab(bquote(I[A] ~ "(nA)")) + |
| 52 | ggtitle(bquote("T =" ~ .(temp) ~ "ºC;" ~ "U"[h]~"=" ~ .(uh) ~ "V")) + |
| 53 | theme(legend.position = "none", text = element_text(family = "Montserrat")) |
| 54 | outfilenamesuffix <- "" |
| 55 | if (drawMinimums) { |
| 56 | outfilenamesuffix <- "_minims" |
| 57 | } |
| 58 | ggsave(paste0('output/', filename, outfilenamesuffix, '.svg'), pplot, width = 6.6, height = 3.75, bg = "transparent") |
| 59 | } |
| 60 | |
| 61 | abs_data <- NA |
| 62 | for (filename in dependanceOnTFilenames) { |
| 63 | temp <- gsub("(\\d+)K_.*", "\\1", filename) |
| 64 | min_vals <- read.table(paste0('dades/', filename, '_minims.txt'), sep="\t", dec=",", header=TRUE) |
| 65 | ur_diffs <- diff(min_vals$U1) |
| 66 | ns <- min_vals$n[-1] |
| 67 | ur_diffs_data <- data.frame(n = ns, UR_diff = ur_diffs) |
| 68 | ur_diffs_data$T <- temp |
| 69 | if (!is.data.frame(abs_data)) { |
| 70 | abs_data <- ur_diffs_data |
| 71 | } else { |
| 72 | abs_data <- rbind(abs_data, ur_diffs_data) |
| 73 | } |
| 74 | print(paste0('T=', temp)) |
| 75 | print(paste0('Mean: ', mean(ur_diffs))) |
| 76 | print(paste0('Unc. mean: ', sd(ur_diffs)/sqrt(length(ur_diffs)))) |
| 77 | } |
| 78 | |
| 79 | urplot <- ggplot(data = abs_data, aes(x = n, y = UR_diff, color = T)) + |
| 80 | geom_line() + |
| 81 | scale_x_continuous(breaks = function(x) unique(floor(pretty(3:11)))) + |
| 82 | xlab("n") + |
| 83 | ylab(bquote(U[R] ~ "(V)")) + |
| 84 | ylim(4, 5.4) + |
| 85 | labs(color = "T (ºC)") + |
| 86 | theme(text = element_text(family = "Montserrat")) |
| 87 | ggsave('output/ur_for_different_ts.svg', urplot, width = 6.6, height = 3.75, bg = "transparent") |
| 88 | |
| 89 | boxp1plot <- ggplot(data = abs_data, aes(x = T, y = UR_diff, color = T)) + |
| 90 | geom_boxplot() + |
| 91 | xlab("T (ºC)") + |
| 92 | ylab(bquote(U[R] ~ "(V)")) + |
| 93 | #ylim(4, 5.4) + |
| 94 | theme(legend.position = "none", text = element_text(family = "Montserrat")) |
| 95 | ggsave('output/mean_ur_vs_t_boxplot.svg', boxp1plot, width = 6.6, height = 3.75, bg = "transparent") |
| 96 | |
| 97 | abs_data.summary <- abs_data %>% |
| 98 | group_by(T) %>% |
| 99 | summarise( |
| 100 | sd = sd(UR_diff, na.rm = TRUE)/sqrt(n()), |
| 101 | UR_diff = mean(UR_diff) |
| 102 | ) |
| 103 | boxp2plot <- ggplot(data = abs_data, aes(x = T, y = UR_diff, color = T)) + |
| 104 | geom_jitter( |
| 105 | position = position_jitter(0.2), color = "darkgray" |
| 106 | ) + |
| 107 | geom_point(size = 2.5, data = abs_data.summary) + |
| 108 | geom_errorbar( |
| 109 | aes(ymin = UR_diff - sd, ymax = UR_diff + sd), |
| 110 | width = 0.15, |
| 111 | data = abs_data.summary |
| 112 | ) + |
| 113 | xlab("T (ºC)") + |
| 114 | ylab(bquote(U[R] ~ "(V)")) + |
| 115 | #ylim(4, 5.4) + |
| 116 | theme(legend.position = "none", text = element_text(family = "Montserrat")) |
| 117 | ggsave('output/mean_ur_vs_t_boxplot2.svg', boxp2plot, width = 6.6, height = 3.75, bg = "transparent") |