如何处理“不同等级的数据”来自ggplot2的错误? [英] How to deal with "data of class uneval" error from ggplot2?

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

问题描述

 错误:ggplot2 doesn'正在尝试将新行覆盖到现有的ggplot上时出现以下错误:我知道如何处理不同类的数据

我的代码的第一部分工作正常。以下是来自美国中西部电力市场的近期每小时风力发电数据。



现在我想在Red中叠加最近两天的观察值。它应该很容易,但我无法弄清楚为什么我得到一个错误。



任何援助将不胜感激。



以下是一个可重复使用的例子:

 #阅读Wind数据
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:(长度(df $ MKTHOUR)-5),]

#格式变量
df $ MWh < - as.numeric(df $ MWh)
df $ Datetime< - strptime(df $ MKTHOUR,%m /%d /%y%I:%M%p)

#创建一些变量
df $ Date< - as.Date(df $ Datetime)
df $ HrEnd< - df $ Datetime $ hour + 1

#子集最近和最后数据
最后.obs < - 范围(df $ Date)[2]
df.recent < - 子集(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))

# b $ bp < - ggplot(df.recent,aes(HrEnd,MWh,g roup = factor(Date)))+
geom_line(color =gray)+
scale_y_continuous(labels = comma)+
scale_x_continuous(breaks = seq(1,24,1)) +
实验室(y =MWh)+
实验室(x =小时终点)+
实验室(title =每小时风力发电)
p

#最后两天在红色
p < - p + geom_line(df.last,aes(HrEnd,MWh,group = factor(Date)),color =red)
p


解决方案

当您向geom添加新数据集时需要使用 data = 参数。或者按照正确的顺序 mapping = ...,data = ... 来输入参数。看看?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)


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

解决方案

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.

Thus:

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

Or:

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

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

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