I am interested in creating a scatter plot with the following data using R:
Minutes | Group 1 | Group 2 |
---|---|---|
1 | 199 | 176 |
2 | 188 | 157 |
3 | 169 | 144 |
See screenshot for more info:
https://joshuawkelly.notion.site/Question-about-r-777cd223ed1441afb1f23dece3b9bc9c?pvs=4
I began to write the code for the graph, but then I ran into issues. Namely: (1) how to put multiple sets of data on a single graph, and (2) how to adjust the graph to be within intended ranges and scales.
install.packages("tidyverse")
library(tidyverse)
fnames = c(1, 2, 3)
g1 = c(119, 118, 157)
g2 = c(176, 157, 144)
hr_data_class = tibble(Minutes = fnames,Group_1 = g1,Group_2 = g2)
print(hr_data_class)
ggplot(hr_data_class, aes(x=fnames,, y=g1 + g2, color=cyl, shape=cyl)) + geom_point(shape=6, color= "#154734") + labs(x="Time (Minutes)",y="Heart Rate (BPM)")
ggplot(hr_data_class, aes(x=g1,, y=g2, color=cyl, shape=cyl)) + geom_point(shape=6, color= "#154734") + labs(x="Time (Minutes)",y="Heart Rate (BPM)")
plot(fnames,g1,col='red',pch=19,cex=3,xlab='X1',ylab='Y1',main='hello world')
plot(fnames,g2,col='red',pch=19,cex=3,xlab='Time (Minutes)',ylab='Heart Rate (BPM)',main='Class Heart Rate Data')
As the values for the groups are spread across columns you could use a geom_point
for each column but the tidyverse
way would be to reshape your data to long or tidy format using e.g. tidyr::pivot_longer
which allows to create your plot using one geom_point
. The colors can then be set via scale_color_manual
.
library(ggplot2)
library(tidyr)
hr_data_class_long <- hr_data_class |>
pivot_longer(-Minutes, names_to = "group", values_to = "value")
ggplot(hr_data_class_long, aes(Minutes, value, color = group)) +
geom_point() +
scale_color_manual(values = c("red", "blue")) +
labs(x = "Time (Minutes)", y = "Heart Rate (BPM)", color = NULL) +
theme(legend.position = "bottom")