Skip to content

Commit 4cdf718

Browse files
committed
Added force and power plots.
1 parent faa4652 commit 4cdf718

File tree

1 file changed

+108
-8
lines changed

1 file changed

+108
-8
lines changed

app.R

Lines changed: 108 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ library(plotly)
66
ui <- fluidPage(
77
# Application title
88
titlePanel("Load-velocity Model"),
9-
9+
1010
# Sidebar with input fields for speed and charge
1111
sidebarLayout(
1212
sidebarPanel(
@@ -15,10 +15,13 @@ ui <- fluidPage(
1515
actionButton("add", "Add to Plot"),
1616
actionButton("clear", "Clear Plot")
1717
),
18-
18+
1919
# Show a plot of the generated model
2020
mainPanel(
21-
plotlyOutput("lvPlot")
21+
plotlyOutput("lvPlot"),
22+
plotlyOutput("fsPlot"),
23+
plotlyOutput("powerPlot"),
24+
plotlyOutput("combinedPlot")
2225
)
2326
)
2427
)
@@ -40,23 +43,120 @@ server <- function(input, output) {
4043

4144
output$lvPlot <- renderPlotly({
4245
plot_data <- data()
43-
p <- ggplot(plot_data, aes(x = charge, y = speed)) +
46+
p <- ggplot(plot_data, aes(x = speed, y = charge)) +
4447
geom_point() +
45-
labs(x = "Charge (kg)", y = "Speed (m/s)", title = "Load-velocity Model") +
48+
labs(x = "Speed (m/s)", y = "Charge (kg)", title = "Load-velocity Model") +
4649
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
4750

4851
if(nrow(plot_data) > 1) {
49-
fit <- lm(speed ~ charge, data = plot_data)
52+
fit <- lm(charge ~ speed, data = plot_data)
5053
p <- p + geom_smooth(method = "lm", col = "blue", se = FALSE, formula = 'y ~ x')
5154

5255
# Add the regression equation to the plot
5356
eq <- paste0("y = ", round(coef(fit)[1], 4), " + ", round(coef(fit)[2], 4), "x")
54-
p <- p + annotate("text", x = mean(plot_data$charge), y = max(plot_data$speed), label = eq, size = 4, color = "blue")
57+
p <- p + annotate("text", x = max(plot_data$speed), y = max(plot_data$charge), label = eq, size = 4, color = "blue", hjust = 1, vjust = 1)
5558
}
5659

5760
ggplotly(p)
5861
})
62+
63+
output$fsPlot <- renderPlotly({
64+
plot_data <- data()
65+
if (nrow(plot_data) > 0) {
66+
plot_data$force <- plot_data$charge * 9.81 # Assuming charge is mass in kg and force is in Newtons
67+
p <- ggplot(plot_data, aes(x = speed, y = force)) +
68+
geom_point() +
69+
labs(x = "Speed (m/s)", y = "Force (N)", title = "Force-Speed Plot") +
70+
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
71+
72+
if(nrow(plot_data) > 1) {
73+
fit <- lm(force ~ speed, data = plot_data)
74+
p <- p + geom_smooth(method = "lm", col = "blue", se = FALSE, formula = 'y ~ x')
75+
76+
# Add the regression equation to the plot
77+
eq <- paste0("y = ", round(coef(fit)[1], 4), " + ", round(coef(fit)[2], 4), "x")
78+
p <- p + annotate("text", x = max(plot_data$speed), y = max(plot_data$force), label = eq, size = 4, color = "blue", hjust = 1, vjust = 1)
79+
}
80+
81+
ggplotly(p)
82+
} else {
83+
NULL
84+
}
85+
})
86+
87+
output$powerPlot <- renderPlotly({
88+
plot_data <- data()
89+
if (nrow(plot_data) > 0) {
90+
plot_data$power <- plot_data$speed * plot_data$charge
91+
p <- ggplot(plot_data, aes(x = speed, y = power)) +
92+
geom_point() +
93+
labs(x = "Speed (m/s)", y = "Power (W)", title = "Power Evolution by Speed") +
94+
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
95+
96+
if(nrow(plot_data) > 2) {
97+
fit <- lm(power ~ poly(speed, 2), data = plot_data)
98+
p <- p + geom_smooth(method = "lm", formula = y ~ poly(x, 2), col = "red", se = FALSE)
99+
100+
# Add the regression equation to the plot
101+
eq <- paste0("y = ", round(coef(fit)[1], 4), " + ", round(coef(fit)[2], 4), "x + ", round(coef(fit)[3], 4), "x^2")
102+
p <- p + annotate("text", x = max(plot_data$speed), y = max(plot_data$power), label = eq, size = 4, color = "red", hjust = 1, vjust = 1)
103+
}
104+
105+
ggplotly(p)
106+
} else {
107+
NULL
108+
}
109+
})
110+
111+
output$combinedPlot <- renderPlotly({
112+
plot_data <- data()
113+
if (nrow(plot_data) > 0) {
114+
plot_data$force <- plot_data$charge * 9.81 # Assuming charge is mass in kg and force is in Newtons
115+
plot_data$power <- plot_data$speed * plot_data$charge
116+
117+
p1 <- ggplot(plot_data, aes(x = speed, y = charge)) +
118+
geom_point() +
119+
labs(x = "Speed (m/s)", y = "Charge (kg)", title = "Load-velocity Model") +
120+
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
121+
122+
if(nrow(plot_data) > 1) {
123+
fit1 <- lm(charge ~ speed, data = plot_data)
124+
p1 <- p1 + geom_smooth(method = "lm", col = "blue", se = FALSE, formula = 'y ~ x')
125+
eq1 <- paste0("y = ", round(coef(fit1)[1], 4), " + ", round(coef(fit1)[2], 4), "x")
126+
p1 <- p1 + annotate("text", x = max(plot_data$speed), y = max(plot_data$charge), label = eq1, size = 4, color = "blue", hjust = 1, vjust = 1)
127+
}
128+
129+
p2 <- ggplot(plot_data, aes(x = speed, y = force)) +
130+
geom_point() +
131+
labs(x = "Speed (m/s)", y = "Force (N)", title = "Force-Speed Plot") +
132+
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
133+
134+
if(nrow(plot_data) > 1) {
135+
fit2 <- lm(force ~ speed, data = plot_data)
136+
p2 <- p2 + geom_smooth(method = "lm", col = "blue", se = FALSE, formula = 'y ~ x')
137+
eq2 <- paste0("y = ", round(coef(fit2)[1], 4), " + ", round(coef(fit2)[2], 4), "x")
138+
p2 <- p2 + annotate("text", x = max(plot_data$speed), y = max(plot_data$force), label = eq2, size = 4, color = "blue", hjust = 1, vjust = 1)
139+
}
140+
141+
p3 <- ggplot(plot_data, aes(x = speed, y = power)) +
142+
geom_point() +
143+
labs(x = "Speed (m/s)", y = "Power (W)", title = "Power Evolution by Speed") +
144+
theme(plot.title = element_text(hjust = 0.5)) # Center the plot title
145+
146+
if(nrow(plot_data) > 2) {
147+
fit3 <- lm(power ~ poly(speed, 2), data = plot_data)
148+
p3 <- p3 + geom_smooth(method = "lm", formula = y ~ poly(x, 2), col = "red", se = FALSE)
149+
eq3 <- paste0("y = ", round(coef(fit3)[1], 4), " + ", round(coef(fit3)[2], 4), "x + ", round(coef(fit3)[3], 4), "x^2")
150+
p3 <- p3 + annotate("text", x = max(plot_data$speed), y = max(plot_data$power), label = eq3, size = 4, color = "red", hjust = 1, vjust = 1)
151+
}
152+
153+
combined_plot <- subplot(ggplotly(p1), ggplotly(p2), ggplotly(p3), nrows = 3, shareX = TRUE)
154+
combined_plot
155+
} else {
156+
NULL
157+
}
158+
})
59159
}
60160

61161
# Run the application
62-
shinyApp(ui = ui, server = server)
162+
shinyApp(ui = ui, server = server)

0 commit comments

Comments
 (0)