Home > Back-end >  Error in plot creation when attempting to chart commodity prices
Error in plot creation when attempting to chart commodity prices

Time:05-30

I'm new to r and am trying to do some cross correlation analysis on stocks and commodities. I can't seem to get any plots or correlation functions to work. I have a CSV with a single column of prices for iron but can't plot it. I think the problem is related to it not being integer values in the data frame but I can't seem to change that either. The photo attached is what the iron plot looks like, I want it to look like a normal price chart with price as y axis and x as time.

#Get commodity data from irondata.csv#
iron <- read.csv(file = 'irondata.csv')
print(head(iron))
plot(iron)
hist(iron)

Error:

Error in hist.default(iron) : 'x' must be numeric

incorrect plot image iron data csv screenshot

CodePudding user response:

The error says 'x' must be numeric, so you have to convert your X150 column to numeric. You can use for that as.numeric. Here is an example of using a small piece of your data to plot a hist:

df <- data.frame(X150 = c(150, 150, 154, 154, 164, 170, 170, 170))

hist(as.numeric(df$X150), xlab = "X150", main = "Histogram")

Output:

enter image description here

For plotting a time series, you need to have a sequence for your column like this:

df$time <- sample(1:nrow(df))
plot(df$time, df$X150, xlab = "time", ylab = "X150")

Output:

enter image description here

  • Related