Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in strsplit(unitspec, " ") in code for Machine Learning for Hackers

Tags:

r

I am new to this book and get an error with the example code for the first chapter. I installed the latest version of R 3.2.3 and RStudio. I installed package_install.R given by the sample code and then installed ggplot2 by myself. When I run the code ufo_sightings.R, I got an error as follow

`stat_bin()` using `bins = 30`. Pick better value with `binwidth`.
Error in strsplit(unitspec, " ") : non-character argument
In addition: Warning message:
Removed 1 rows containing non-finite values (stat_bin). 

I am new to R so I have no idea what is wrong. Can anyone help? Is it because I used the latest version of R?

Edit: I think I found the reason. If I change ggplot to 1.01 version, it worked fine. If I upgrade it to 2.0.0 the error occurs. I guess it is a bug?

like image 924
Ben Avatar asked Dec 22 '15 23:12

Ben


1 Answers

The problem lies in the ggplot function scale_x_date. In the original code this is coded as:

quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
  geom_histogram() + 
  scale_x_date(breaks = "50 years")

The breaks in scale_x_date has been adjusted to date_breaks. If you adjust the code to the following it works.

quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
  geom_histogram() + 
  scale_x_date(date_breaks = "50 years", date_labels = "%Y")

In the rest of the code where you see ggplot and scale_x_date, you will have to adjust the breaks into date_breaks.

like image 199
phiver Avatar answered Oct 30 '22 07:10

phiver