我在使用qplot绘制一条线时遇到问题。我已经回顾过以前与ggplot2相关的帖子(例如ggplot2 each group consists of only one observation),但我的问题有点不同。
我只想要一个简单的线图来绘制温度与时间的关系(ymd_hms)。不是两行。
我的数据如下:
Date.time
2011/06/17 00:00:00
2011/06/17 00:30:00
2011/06/17 01:00:00
2011/06/17 01:30:00
2011/06/17 02:00:00
2011/06/17 02:30:00
2011/06/17 03:00:00
2011/06/17 03:30:00
Temp
71.1
71.1
71.1
71.1
70.8
70.8
70.8
70.5
这是我用过的代码:
as.POSIXct(data$Date.time, format = "%Y/%m/%d %H:%M:%S")
qplot(Date.time, Temp, data=mydata, geom="line")
But I get this error:
"Each group consists of only one observation. Do you need to adjust the group aesthetic?"
我该如何解决这个问题?
谢谢!
答案 0 :(得分:2)
我认为在您的问题中,您应该将data=mydata
更改为data=data
,然后它应该有用。
这是一个解决方案:
data <- structure(list(date.time = structure(c(1308288600, 1308290400,
1308292200, 1308294000, 1308295800, 1308297600, 1308299400), class = c("POSIXct",
"POSIXt"), tzone = ""), temperature = c(71.1, 71.1, 71.1, 70.8,
70.8, 70.8, 70.5)), .Names = c("date.time", "temperature"), row.names = c(NA,
-7L), class = "data.frame")
data$date.time <- as.POSIXct(data$date.time,format="%m/%d/%Y %H:%M")
library(ggplot2)
qplot(data=data,x=date.time,y=temperature,geom="line")
输出如下: