0

I got this R code to make visualization using ggplot. However, I am getting the problem with syntax. Can anyone see where it went wrong? Thanks!

t <- ggplot_build(p)$data[[3]]
s[i,1] <- as.character(a[i])
s[i,2] <- t[1,2] - t[(length(t[,1])),2]

The error is related to t in this three lines of code.

Spacedman
  • 92,590
  • 12
  • 140
  • 224
Mayur Mane
  • 13
  • 1
  • 1
  • 7
  • To make your question clearer: Please read [How to make a great reproducible example in R?](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) to ask a clear question in r. Also you may want to take the [tour](https://stackoverflow.com/tour) and read [how to ask?](https://stackoverflow.com/help/how-to-ask) – M-- Jul 28 '17 at 14:15
  • Very likely the error is due to the fact that `t` happens to have 0 columns. Try for instance: `t<-iris[integer(0)];print(ncol(t));t[,1]`. If you replace `t[(length(t[,1])),2]` with `t[nrow(t),2]` you probably avoid the error (although you get `NULL` and likely unwanted consequences) and keep the behaviour whenever `t` does have columns. – nicola Jul 28 '17 at 14:18
  • I think this error came from `t[1]` (list index), rather than `t[,1]` (matrix index), according to the number of commas in `\`[.data.frame\`(t, 1)`. If `t[,1]` were used it would be `\`[.data.frame\`(t, , 1)`. So it may be from some other piece of code. Nevertheless, a reproducible example is necessary here. – Rich Scriven Jul 28 '17 at 14:21
  • Thanks @nicola for explaining the code. I had the data frame with zero length and now I have added if loop to avoid if the length is zero. Explaining the reason behind getting error helped me resolving the issue. Cheers! – Mayur Mane Jul 28 '17 at 14:55

0 Answers0