Luis Miguel
Luis Miguel

Reputation: 5137

"Error in continuous_scale" and "error in inherits" ggplot2 & R 2.14.2

I am following a tutorial in the book Machine Learning for Hackers by Drew Conway and John White, and I am stuck with a problem plotting a histogram. The example code runs up the plotting section here:

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

produces

Error in continuous_scale(aesthetics, "date", identity, breaks = breaks,  : unused argument(s) (major = "50 years")

and

> ggsave(plot = quick.hist,
+        filename = "C:\test.png",
+        height = 6,
+        width = 8)

produces

Error in inherits(plot, "ggplot") : object 'quick.hist' not found

I am using R version 2.14.2. and the ggplot2 library. Thanks in advance for any help.


Solved

A quick solution that worked for me was to eliminate the '+ scale_x_date(major = "50 years")' part of every line that referenced a label. The final code that produced the histogram was like this:

> quick.hist <- ggplot(ufo.us, aes(x = DateOccurred)) +
+   geom_histogram()

I would like to add labels to the charts at some point, but for now, this solution works with the new version of ggplot2.


Better resolution yet:
I encountered similar problems while running through the book's hands-on example. I'm posting here the complete snippet for the production of the final plot on in the book (this is not the same plot referenced originally in this question, but it too exposed the same problems).
This fix addresses the issues of

Changes to the book's snippet are shown in bold below:

library(ggplot2)
  library(scales)
state.plot <-
    ggplot(all.sightings, aes(x=YearMonth, y=Sightings)) +
    geom_line(aes(color="darkblue")) +
    facet_wrap(~State, nrow=10, ncol=5) +
    theme_bw() +
    scale_color_manual(values=c("darkblue"="darkblue"), guide="none") +
    scale_x_date(breaks= date_breaks(width = "5 years"),
            labels = date_format("%Y")) +
    xlab("Time") + ylab("Nb of Sightings") +
    labs(title="Nb of UFO sightings by Month-Year and US State (1990-2000)")

print(state.plot)

Upvotes: 14

Views: 4864

Answers (5)

griffon vulture
griffon vulture

Reputation: 6774

Simply two steps:

  1. Add library:

    library(scales)

  2. Change old parameter name:

    breaks="50 years" instead of major="50 years"

Thats all.

Upvotes: 4

Olofsson Zee
Olofsson Zee

Reputation: 41

Simply use the code from Author's Github Repositories' code and it works with no extra efforts. Just for your reference, I copy and paste author's code here.

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

ggsave(plot = quick.hist,
       filename = file.path("images", "quick_hist.png"),
       height = 6,
       width = 8)

Upvotes: 4

moon star
moon star

Reputation: 521

After trying other suggestions on this thread, I found it better to just delete the scale_x_date line. The default settings look better even after you make adjustments for the R version.

Also, as you work through the example, you'll realize that it's not worth spending more than 5-10 minutes on this issue. (Unfortunately, I learned this the hard way.)

Upvotes: 1

Jonas Heidelberg
Jonas Heidelberg

Reputation: 5024

The second problem is caused by the first. This forum discussion suggests you are seeing a version incompatibility with your first problem. The PDF linked there talks on page 31/32 about your problem; it seems

 scale_x_date(breaks = date_breaks(width = "50 years"), labels = date_format("%Y"))

is the new syntax you should use instead of scale_x_date(major = "50 years"). Read the PDF for more detail ... and good luck with the tutorial! If you continue with it, you might want to install the exact software version the tutorial was written for...

Per the R help, when entering date_breaks, you must specify a width, then your timeframe and specification ("sec", "min", "hour", "day", "week", "month", "year") to make it function properly. I've added the necessary syntax to the previous code snippet. This was verified and tested while working through the Machine Learning for Hackers tutorial on 14-Oct-2012.

Upvotes: 15

B0WSER
B0WSER

Reputation: 104

Have the same problem.

Adding

library(scales)

solved this problem.

Upvotes: 6

Related Questions