【问题标题】:Using Base R to graph subset of data使用 Base R 绘制数据子集
【发布时间】:2021-02-04 11:58:41
【问题描述】:

我的室内训练器(自行车)出现问题,我想尝试探索数据,看看是否可以找出问题所在。我一般对 R 不是很好,但我参加了使用 R 的数据可视化课程,我认为我对一些优秀的图表大约有一半的路程。我希望这个社区的一些提示可以帮助我完成它。我知道很多人都是 ggplot 的传教者,我尊重这一点,但我认为这个阶段我试图在我所知道的范围内进行扩充,而不是试图从头开始学习一套全新的功能。我已将数据包含在底部。

mymindata<-load(file = "Indoor Cycling Data - Old.Rdata")

我似乎无法在绘制图表时对我的数据进行子集化,并且因为我有一个长格式数据集,它给了我一个图表,它将第一次锻炼中的最后一个值与下一次锻炼中的第一个值联系起来。

我想通过在我的lines 函数上使用subset 函数来绘制其中一项锻炼。但是,我对 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"))

【问题讨论】:

    标签: r plot graph


    【解决方案1】:

    子集然后绘图,尝试:

    # 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"))
    

    【讨论】:

      猜你喜欢
      • 2014-11-21
      • 1970-01-01
      • 1970-01-01
      • 2017-01-03
      • 1970-01-01
      • 2013-08-13
      • 1970-01-01
      • 1970-01-01
      • 2015-02-26
      相关资源
      最近更新 更多