我有这个数据:
Date ID Value
10-Apr-17 12:02:30 A 4.107919756
10-Apr-17 12:02:31 A 4.107539119
10-Apr-17 12:02:32 A 5.503949115
10-Apr-17 12:02:33 B 5.842728032
10-Apr-17 12:02:34 B 8.516053634
10-Apr-17 12:02:35 B 1.515112486
10-Apr-17 12:02:36 B 5.224667007
我想仅使用列 ID == 'A' 来绘制 geom_point。
library(ggplot2)
library(lubridate)
library(magrittr)
thedata <- read.csv("~/Downloads/Vel.csv", header = TRUE)
thedata$newDate <- dmy_hms(thedata$Date)
ggplot(thedata, aes(newDate, Value)) +
geom_point(thedata=thedata$ID %>% filter(thedata$ID == "A"))
但它绘制了所有点(A 和 B ID)。
它给了我
“警告:忽略未知参数:数据”
使用 ggplot 时。
更新
使用 :
thedata <- read.csv("~/Downloads/Vel.csv", header = TRUE)
thedata <- as.data.frame(thedata)
thedata$newDate <- dmy_hms(thedata$Date)
ggplot(thedata, aes(newDate, Value)) +
geom_point(data=thedata$ID %>% filter(thedata$ID == "A"))
因此,使用数据作为数据框,而geom_point(data=thedata$ID %>%
不是geom_point(thedata=thedata$ID %>%
像@aosmith 指出的那样使用,
结果是 :
错误:ggplot2 不知道如何处理 ts 类的数据
我认为你应该这样做:
ggplot(thedata %>% dplyr::filter(ID == "A"), aes(newDate, Value)) +
geom_point()
关键是你不能geom
在 ggplot() 中指定一个新的数据框时指定一个新的数据框。我想你也可以做这样的事情:
ggplot() +
geom_point(data = thedata %>% dplyr::filter(ID == "A"), aes(newDate, Value))
编辑 :
我更新了第二个代码块,所以它现在应该可以工作了。
关于 filter() 函数,您不需要thedata
在您的情况下使用管道。这项工作也一样,更容易阅读:geom_point(data = filter(thedata, ID == "A"), aes(newDate, Value))
此外,这只是我的意见,但我想您可以通过 ID 绘制整个数据和颜色会更有趣,如下所示:
ggplot() +
geom_point(data = thedata, aes(newDate, Value, colour = ID))
要完成ggplot()
使用数据框进行馈送的问题,请注意您可以data
为所有几何体指定不同,如本例中的mtcars
数据集:
ggplot() +
geom_point(data = mtcars, aes(mpg, disp, colour = cyl)) +
geom_point(data = filter(mtcars, cyl == 6), aes(qsec, drat))
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句