0

I recently started to work with R to display price forecast from firms and institutions (World Bank, EIA, Deloitte and Macquarie) for different products (USA Natural gas, Japan LNG and NBP gas). So there is a graph displaying price forcast for the World Bank about USA Natural Gas, another graph for the World Bank about Japan LNG, another one for the World Bank about the NBP gas and so on with the EIA, Deloitte and Macquarie. So it's 12 graphs for now. I had some trouble with paste and sprintf() functions but I managed to solve it.

Now I have a big final problem, beacause when I run the code, I get :

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

And I really don't know how to deal with it. Do you have any idea what is wrong with it? Thanks!

Here is the code :

chemin <- choose.files()
forecast <- data.frame(read.table(chemin, sep=";", dec=",", header=TRUE))
forecast$Publication.date <- as.Date(forecast$Publication.date, format="%d/%m/%Y")
forecast$Price.date <- as.Date(forecast$Price.date, format="%d/%m/%Y")
EIAforecast <- forecast[forecast$Forecaster=="EIA",]
WorldBankforecast <- forecast[forecast$Forecaster=="World Bank",]
Macquarieforecast <- forecast[forecast$Forecaster=="Macquarie",]
Deloitteforecast <- forecast[forecast$Forecaster=="Deloitte",]

library(ggplot2)
library(scales)
n <- 1
c1 <- c("USA", "Japan", "NBP")
c2 <- c("WorldBank", "EIA", "Deloitte", "Macquarie")
for (i in c1)
{
for (j in c2)
{
assign(sprintf("%srealizedprice", i), data.frame(read.table(paste("C:\\Travail\\Maxime\\Memoire\\Forecasts\\csv", paste0(i, "realizedprice.csv"), sep="\\"), sep=";", dec=",", header=TRUE)))
assign(sprintf("%srealizedprice$Date", i), as.Date(sprintf("%srealizedprice$Date", i), format="%d/%m/%Y"))
assign(sprintf("%srealizedpricegraph", i), ggplot(data=paste0(i, "realizedprice"), mapping=aes(x=Date, y=paste(i, "price", sep="."))) + geom_line())
assign(sprintf("%s%sforecast", i, j), paste(paste(paste0(j, "forecast"), paste0(j, "forecast$Price.type == "), sep="["), paste("\"Natural Gas", paste0(i, "\"]"), sep=", "), sep=""))
assign(sprintf("%sgraph", i), ggplot(data=get(paste0(i, j, "forecast")), aes(x=Price.date, y=Price, colour=as.character(paste(Price.type, Publication.date))))+geom_line()+geom_point()+geom_line(data = paste0(i, "realizedprice"), aes(x = Date, y = paste(i, "price", sep=".")), color = "cornflowerblue")+xlab('Date')+ylab('Price')+ggtitle(sprintf("World Bank forecast for Natural Gas %s", i)))
assign(sprintf("%sgraph", i), sprintf("%sgraph", i) + scale_x_date(breaks = date_breaks("year"),labels = date_format("%Y"))+scale_y_continuous(breaks = seq(0, 25, 1)))
mypath <- file.path(sprintf("C:\\Travail\\Maxime\\Memoire\\Forecasts\\Graphs\\Boucle\\%d.png", n))
paste0(i, "graph")
png(file=mypath)
plot(sprintf("%sgraph", i))
dev.off()
n <- n + 1
}
}
user37103
  • 21
  • 8
  • I won't try reading that code, but based on the error message, try converting your character variable to a factor with `factor` or `as.factor` and see if it works. – lmo Jul 07 '16 at 14:04
  • 2
    Basically all of your problems come from trying to solve your problems using `assign` and `get`. If you are just beginning, you need to start over and _literally forget you ever heard of those functions_. All they will do is cause you to code yourself into a corner. Spend some time with _named lists_. Named lists. Named lists. – joran Jul 07 '16 at 14:09
  • What's with all the `assign`s? That doesn't look like very R-like code. Plus in the end use seem to just use `plot()` which isn't a `ggplot` plotting function. You can't swap strings for variables in R. – MrFlick Jul 07 '16 at 14:09
  • 1
    I think the problem here is ggplot(data=get(paste0(i, j, "forecast")), but I started working with R very recently, so by "character varaible" you mean i and j (the loop variables) or the whole paste0(i, j, "forecast") ? Thanks! – user37103 Jul 07 '16 at 14:10
  • 3
    @user37103 Yes, essentially. But you need to back up and scrap the _entire concept_ of building variable names using `paste` and `sprintf` and assigning to them using `assign`. Named lists! – joran Jul 07 '16 at 14:12
  • Well, the assign thing come from some topic on stackexchange, because at first I was using sprintf(smthg) – user37103 Jul 07 '16 at 14:15
  • 1
    I'm telling you to use ` – joran Jul 07 '16 at 14:19
  • 1
    I feel like @joran's comment needs some emphasis... My advice would also be to first learn R's basics, like fundamental data structures but also principles. Things like `sprintf(x) – slamballais Jul 07 '16 at 14:21
  • @joran I am learning by myself so I know I lack basic knowledge, I know it's far from a well composed code but I intend to improve by trying to write some codes and running it. Of course I read R introduction pdfs but I need to get this project done (experienced people see that as pretty easy so they don't give so much time^^) so I can't learn all of R theory from scratch. I know it's not the proper way to learn but do you think this code (as it is now), may work, or there is no way it will work and it needs to be erased and rebuilt from scratch? – user37103 Jul 07 '16 at 14:48
  • @lmo I tried to use ggplot(data=factor(get(paste0(i, j, "forecast"))), as well as ggplot(data=get(factor(paste0(i, j, "forecast"))), and ggplot(data=get(paste0(factor(i), factor(j), "forecast"))), but the ggplot character class remains no matter what...Sorry if I use it in the wrong way, I am not used to as.factor() or factor() functions. But, as I read some documentations about factors, I don't understand why I should convert it into factors here. – user37103 Jul 07 '16 at 15:16
  • I would take joran's advice and build this up from scratch using named lists. It is nearly impossible to tell what variable is causing `ggplot` to complain. One example of creating a named list, try `myList – lmo Jul 07 '16 at 15:31
  • After reading some documentation on named lists, from my point of view, I can't see why rebuilding it with named lists would help solving the errors. I still would have to call i and j with sprintf and paste function (as the name of my lists would be my loop variables), which apparently doesn't work. I think I will just give up on that. I just wanted to create this loop to get quickly all the graphs, but it's not worth it if I have to spend so much time on that by rebuilding it from scratch. Thanks – user37103 Jul 07 '16 at 15:52

1 Answers1

1

I had this problem while trying to plot latitude/longitude on a map. The message was slightly ambiguous for my tastes. What the problem was was that I had extracted the coordinates from another data set using c(lat, lon) thus creating a list when I should have used cbind(lat,lon) to create a matrix which I then coerced into a data frame using as.data.frame.

Clarius
  • 677
  • 6
  • 5