Reputation: 146
I'm having trouble with my indoor trainer (bicycle) and I want to try to explore the data to see if I can isolate the problem. I'm not great with R generally, but I took a course in Data Visualization using R and I think I'm about half way to some excellent graphs. I'm hoping some tips from this community can help me finish it off. I know many people are ggplot proselytizers, and I respect that, but I think that this stage I'm trying to augment within what I know rather than trying to learn a whole new set of functions from scratch. I've included the data at the bottom.
mymindata<-load(file = "Indoor Cycling Data - Old.Rdata")
I don't seem to be able to subset my data when graphing, and because I have a long-form dataset it's giving me a graph which connects the last value in the first workout to the first value in the next workout.
I would like to graph just one of these workouts by using a subset
function on my lines
function. However, my attempts at a subset function within the lines function are failing. I'd prefer not to set up different dataframes for each workout. Is there a way to do this subsetting within each lines
function? My failed attempt is in the line below.
plot(NULL, type = "l",
axes=F,
xlim=c(0,100),
ylim=c(0, 60),
ylab="Speek (kph)",
xlab="Workout Time (as %)",
main = "Cycling Speed over Time", font.main = 2
)
axis(2, col="black", tck = 0.01, cex.axis = .8, las = 1)
axis(1, col="black", tck = 0.01, cex.axis = .8)
lines(mymindata$time_pcnt, mymindata$speed, col="blue", lwd=1)
lines(mymindata$time_pcnt, mymindata$speed, mydata[which(date=="2021-01-21")], col="black", lwd=2)
mymindata <- structure(list(date = c("2021-01-21", "2021-01-21", "2021-01-21",
"2021-01-21", "2021-01-21", "2021-01-21", "2021-01-21", "2021-01-21",
"2021-01-21", "2021-01-21", "2021-01-21", "2021-01-24", "2021-01-24",
"2021-01-24", "2021-01-24", "2021-01-24", "2021-01-24", "2021-01-24",
"2021-01-24", "2021-01-24", "2021-01-24", "2021-01-24"), speed = c(22,
26.7, 25.33, 27.85, 34.33, 40.31, 41.52, 46.66, 54.78, 58.75,
47, 19, 27.91, 28.08, 28.22, 26.5, 28.28, 28.52, 30, 28.99, 29.89,
20.73), time_pcnt = c(0, 10, 20, 30, 40, 50, 60, 70, 80, 90,
100, 0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100), type = structure(c(2L,
2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L), .Label = c("Unformatted Workout", "Preset Workout"
), class = "factor")), class = "data.frame", row.names = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22"))
Upvotes: 2
Views: 50
Reputation: 56119
Subset then plot, try:
# set up plot
plot(NULL, type = "l",
axes=F,
xlim=c(0,100),
ylim=c(0, 60),
ylab="Speek (kph)",
xlab="Workout Time (as %)",
main = "Cycling Speed over Time", font.main = 2)
axis(2, col="black", tck = 0.01, cex.axis = .8, las = 1)
axis(1, col="black", tck = 0.01, cex.axis = .8)
# all data, do not plot
# lines(mymindata$time_pcnt, mymindata$speed, col="blue", lwd=1)
# 21 Jan
with(subset(mymindata[mymindata$date == "2021-01-21", ]),
lines(time_pcnt, speed, col = "red"))
# 24 Jan
with(subset(mymindata[mymindata$date == "2021-01-24", ]),
lines(time_pcnt, speed, col = "green"))
Upvotes: 2