r - 使用 Base R 绘制数据子集
问题描述
我的室内训练器(自行车)有问题,我想尝试探索数据以查看是否可以隔离问题。我一般对 R 不是很好,但我参加了使用 R 的数据可视化课程,我认为我对一些优秀的图表大约一半。我希望这个社区的一些提示可以帮助我完成它。我知道很多人都是 ggplot 的传教者,我尊重这一点,但我认为这个阶段我试图在我所知道的范围内进行扩充,而不是试图从头开始学习一套全新的功能。我已经在底部包含了数据。
mymindata<-load(file = "Indoor Cycling Data - Old.Rdata")
在绘图时,我似乎无法对我的数据进行子集化,并且因为我有一个长格式的数据集,它给了我一个图表,它将第一次锻炼中的最后一个值与下一次锻炼中的第一个值联系起来。
我想通过subset
在我的函数上使用一个函数来绘制这些锻炼中的一个lines
。但是,我对 lines 函数中的子集函数的尝试失败了。我不想为每次锻炼设置不同的数据框。有没有办法在每个lines
函数中做这个子集?我失败的尝试在下面的行中。
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)
我的数据使用 dput
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"))
解决方案
子集然后绘图,尝试:
# 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"))
推荐阅读
- visual-studio - 在本地托管自定义 Visual Studio 模板
- tensorflow - 张量流中的图优化阶段
- linux - 在循环linux bash之外获取变量
- java - Jhipster:如何在开始时使用 Liquibase 填充数据库
- reactjs - 在 React 中的 try 和 catch 块中呈现相同的组件,而不会失去 ReactJS 中的字段焦点
- process - 如何从运行 OOM 的 JVM 子进程中恢复?
- android - 检索和存储图像
- javascript - 未处理的“错误”事件
- highcharts - HightChart 标志不起作用,错误:Highcharts 错误 #17:www.highcharts.com/errors/17
- php - Typo3 8.7.16:在页面 TSConfig 上设置 options.defaultUploadFolder 不起作用