Hi I'm trying to use ggplot2 to chart a dataset that I've drawn in from excel.? When I run the qplot command, I get the following error message: Error: ggplot2 doesn't know how to deal with data of class character It feels like I need to coerce one of the fields in my data frame. Here's the code, which is short: #draw data in from network location setwd("S:\\790\\Actuarial\\KFC\\609 Kernel\\Sensitivity Tests\\Stressed ESG inputs") input<-read.csv("yieldcurve plus 1pct abs.csv",header=FALSE) #label columns colnames(input)<-c("scenID","yrs_ahead",0.5,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0) #create a data frame that i hope! will work with ggplot.. flat_data<-data.frame(cbind(input[1:2],stack(input[3:15]))) colnames(flat_data)<-c("scenID","yrs_ahead","yield","time") #plot library(ggplot2) qplot(time,yield,data="flat_data",facets = yrs_ahead~.) now the error. In terms of the data format, head(flat data) returns the following:> head(flat_data)scenID yrs_ahead yield time 1 1 0 0.01637218 0.5 2 1 1 0.02446376 0.5 3 1 2 0.04501267 0.5 4 1 3 0.06947724 0.5 5 1 4 0.08262982 0.5 6 1 5 0.09761927 0.5 1) Am I right that it's about coercion of variable type? 2) Any ideas how to proceed? Many thanks, Dave
I suspect it is to do with your method of creating the dataframe, I would check to see if the columns in the df are numeric, which you can do by: is.numeric(flat_data$time) for each variable, if it is not numeric (and at least one must be a character, given the error message) then redefine as a numeric flat_data$time<-as.numeric(flat_data$time) I reckon people better versed in R will have a more efficient solution, but that should work...... Ross -- View this message in context: http://r.789695.n4.nabble.com/ggplot-class-character-problem-tp3809657p3809786.html Sent from the R help mailing list archive at Nabble.com.
Hi David, On Tue, Sep 13, 2011 at 6:00 AM, David Menezes <david.n.menezes at gmail.com> wrote:> Hi > > I'm trying to use ggplot2 to chart a dataset that I've drawn in from > excel.? When I run the qplot command, I get the following error > message: > > Error: ggplot2 doesn't know how to deal with data of class character > > > It feels like I need to coerce one of the fields in my data frame. > Here's the code, which is short: > > #draw data in from network location > setwd("S:\\790\\Actuarial\\KFC\\609 Kernel\\Sensitivity > Tests\\Stressed ESG inputs") > input<-read.csv("yieldcurve plus 1pct abs.csv",header=FALSE) > > #label columns > colnames(input)<-c("scenID","yrs_ahead",0.5,1.0,2.0,3.0,4.0,5.0,6.0,7.0,8.0,9.0,10.0,11.0,12.0) > > #create a data frame that i hope! will work with ggplot.. > flat_data<-data.frame(cbind(input[1:2],stack(input[3:15]))) > colnames(flat_data)<-c("scenID","yrs_ahead","yield","time") > > #plot > library(ggplot2) > qplot(time,yield,data="flat_data",facets = yrs_ahead~.)Try dropping the quotes from "flat_data", i.e., qplot(time, yield, data=flat_data, facets = yrs_ahead~.) Best, Ista> > now the error. > > In terms of the data format, head(flat data) returns the following: > >> head(flat_data) > ?scenID yrs_ahead ? ? ?yield time > 1 ? ? ?1 ? ? ? ? 0 0.01637218 ?0.5 > 2 ? ? ?1 ? ? ? ? 1 0.02446376 ?0.5 > 3 ? ? ?1 ? ? ? ? 2 0.04501267 ?0.5 > 4 ? ? ?1 ? ? ? ? 3 0.06947724 ?0.5 > 5 ? ? ?1 ? ? ? ? 4 0.08262982 ?0.5 > 6 ? ? ?1 ? ? ? ? 5 0.09761927 ?0.5 > > 1) Am I right that it's about coercion of variable type? > 2) Any ideas how to proceed? > > Many thanks, > Dave > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org
On Tue, Sep 13, 2011 at 7:32 AM, RCulloch <ross.culloch at dur.ac.uk> wrote:> I suspect it is to do with your method of creating the dataframe,I think the error is actually triggered by quoting the name of the data.frame in the qplot call. I would> check to see if the columns in the df are numeric, which you can do by: > > is.numeric(flat_data$time)Or just use str(flat_data) Best, Ista> > for each variable, if it is not numeric (and at least one must be a > character, given the error message) then redefine as a numeric > > flat_data$time<-as.numeric(flat_data$time) > > I reckon people better versed in R will have a more efficient solution, but > that should work...... > > Ross > > -- > View this message in context: http://r.789695.n4.nabble.com/ggplot-class-character-problem-tp3809657p3809786.html > Sent from the R help mailing list archive at Nabble.com. > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code. >-- Ista Zahn Graduate student University of Rochester Department of Clinical and Social Psychology http://yourpsyche.org