Files
Physics-Lab-Report/plot1/plot.R
2024-03-13 21:59:22 -05:00

119 lines
2.4 KiB
R

library(ggplot2)
library(readr)
library(latex2exp)
data_t1 <- read_csv(
"m1,m2,a_measured
0.055,0.060,0.380
0.060,0.065,0.354
0.065,0.070,0.323
0.070,0.075,0.310
0.075,0.080,0.303
0.080,0.085,0.279
0.065,0.075,0.649
0.080,0.095,0.780"
)
data_t1$cat <- rep("Theoretical", 8)
colnames(data_t1) <- c("m1", "m2", "a_measured", "Theoretical")
a_theoretical <- function(m) {
9.81 * m
}
m_from_masses <- function(m1, m2) {
(m2 - m1) / (m1 + m2)
}
x_lab <- function() {
TeX(r"($M$)")
}
y_lab <- function() {
TeX(r"($a \ \small{m \cdot s^{-2}}$)")
}
lm_eqn <- function(df) {
m <- lm(df)
eq <- substitute(
italic(y) == a + b %.% italic(x),
list(a = format(unname(coef(m)[1]), digits = 3),
b = format(unname(coef(m)[2]), digits = 3))
)
as.character(as.expression(eq))
}
th_eqn <- function() {
eq <- substitute(
italic(y) == b %.% italic(x),
list(b = format(unname(9.81), digits = 3))
)
as.character(as.expression(eq))
}
data_t1["M"] <- m_from_masses(data_t1["m1"], data_t1["m2"])
data_t1["a"] <- data_t1$M * 9.8
plt <- ggplot(
data = data_t1,
mapping = aes(x = M, y = a_measured)
) +
geom_point(
aes(color = "D55E00"),
size = 8,
) +
geom_smooth(
se = FALSE,
method = "lm",
linewidth = 2.6,
col = "#D55E00",
) +
geom_segment(aes(x = min(data_t1$M),
y = a_theoretical(min(data_t1$M)),
xend = max(data_t1$M),
yend = a_theoretical(max(data_t1$M)),
linetype = "Theoretical"),
size = 2.2,
color = "#0072B2"
) +
theme(
axis.text = element_text(size = 32),
axis.title = element_text(size = 50),
panel.grid.minor = element_line(size = 1.5),
panel.grid.major = element_line(size = 2),
legend.title = element_blank(),
legend.text = element_text(size = 36),
legend.key.size = unit(8, "line")
) +
labs(
x = x_lab(),
y = y_lab(),
) +
annotate("text",
label = lm_eqn(data_t1[, c("a_measured", "M")]),
x = 0.076,
y = 0.58,
size = 18,
col = "#D55E00",
parse = TRUE,
) +
annotate("text",
label = th_eqn(),
x = 0.068,
y = 0.74,
col = "#0072B2",
size = 18,
parse = TRUE,
) +
scale_color_manual(name = "", values = c("#D55E00"), labels = "Measured") +
scale_linewidth_manual("Theoretical", values = c("Theoretical" = 2))
plt
ggsave("plot.svg",
plot = plt,
width = 30,
height = 25,
)