2017-06-16 46 views
-1

我有这样的数据:选择特定的列数据的geom点

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 

我想绘制geom_point仅使用列ID ==“A”。

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时给我

"Warning: Ignoring unknown parameters: thedata"

UPDATE

使用:

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指出,

结果:

Error: ggplot2 doesn't know how to deal with data of class ts

+0

的说法是'data'不是'thed ata'。您需要将data.frame传递给该参数;在你目前的代码中,它看起来像你正在使用矢量。 – aosmith

+0

@aosmith:是的,我错过了'数据'的事情。我更新了,但仍然有错误。 – George

+0

你是否试图使用dplyr中的'filter'或stats中的'filter'?如果前者是'thedata%>%filter(ID ==“A”)' – aosmith

回答

2

我认为这是你的方式ð做到这一点:

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()的问题,注意,如果你没有,你可以用mtcars数据集指定不同的data所有的geom,如本例:

ggplot() + 
    geom_point(data = mtcars, aes(mpg, disp, colour = cyl)) + 
    geom_point(data = filter(mtcars, cyl == 6), aes(qsec, drat)) 
+0

你的最后一个例子不能像写入的那样工作。您需要明确写出'data'参数或传递数据集作为函数中的第二个参数。 'data'参数在geom图层中不是*,而是在'ggplot'中。 – aosmith

+0

好的,谢谢你的帮助。(upv)。我试图将数据加载到ggplot()中,以便让它们已经存在,因为除了geom_point之外,还可能需要添加其他内容。所以,我不想再写数据(newDate,Value)。这就是为什么我想'ggplot(thedata,aes(newDate,Value))+ geom_point(...)',但我不想要再次使用'aes(newDat,Value)'。 – George