如何处理“未评估的类数据"来自ggplot2的错误? [英] How to deal with "data of class uneval" error from ggplot2?

查看:26
本文介绍了如何处理“未评估的类数据"来自ggplot2的错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试将新行叠加到现有 ggplot 时,出现以下错误:

While trying to overlay a new line to a existing ggplot I am getting the following error:

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

我的代码的第一部分工作正常.下面是来自美国中西部电力市场的最近"每小时风力发电数据的图像.

The first part of my code works fine. Below is a image of "recent" hourly wind generation data from a Midwestern United States electric power market.

现在我想用红色叠加过去两天的观察值.这应该很容易,但我不知道为什么我会收到错误.

Now I want to overlay the last two days worth of observations in Red. It should be easy but I cant figure out why I am getting a error.

如有任何帮助,我们将不胜感激.

Any assistance would be greatly appreciated.

下面是一个可重现的例子:

Below is a reproducable example:

# Read in Wind data
fname <- "https://www.midwestiso.org/Library/Repository/Market%20Reports/20130510_hwd_HIST.csv"
df <- read.csv(fname, header=TRUE, sep="," , skip=7)
df <- df[1:(length(df$MKTHOUR)-5),]

# format variables
df$MWh <- as.numeric(df$MWh)
df$Datetime <- strptime(df$MKTHOUR, "%m/%d/%y %I:%M %p")

# Create some variables
df$Date  <- as.Date(df$Datetime)
df$HrEnd <- df$Datetime$hour+1

# Subset recent and last data
last.obs  <- range(df$Date)[2]
df.recent <- subset(df, Date %in% seq(last.obs-30, last.obs-2, by=1))
df.last   <- subset(df, Date %in% seq(last.obs-2,  last.obs,   by=1))

# plot recent in Grey
p <- ggplot(df.recent, aes(HrEnd, MWh, group=factor(Date))) + 
  geom_line(color="grey") +
  scale_y_continuous(labels = comma) + 
  scale_x_continuous(breaks = seq(1,24,1)) +
  labs(y="MWh") + 
  labs(x="Hour Ending") + 
  labs(title="Hourly Wind Generation")    
p

# plot last two days in Red
p <- p + geom_line(df.last, aes(HrEnd, MWh, group=factor(Date)), color="red")  
p

推荐答案

当您向 geom 添加新数据集时,您需要使用 data= 参数.或者以正确的顺序放置参数mapping=..., data=....查看 ?geom_line 的参数.

when you add a new data set to a geom you need to use the data= argument. Or put the arguments in the proper order mapping=..., data=.... Take a look at the arguments for ?geom_line.

因此:

p + geom_line(data=df.last, aes(HrEnd, MWh, group=factor(Date)), color="red") 

或者:

p + geom_line(aes(HrEnd, MWh, group=factor(Date)), df.last, color="red") 

这篇关于如何处理“未评估的类数据"来自ggplot2的错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆