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
- old syntax on scale_x_date (thanks
Jonas Heidelberg) - the need to explicitly reference the
scaleslibrary (thanksB0WSER) - deprecated syntax for
legend=(replaced byguide=) - deprecated syntax for
opts()(replaced bylabs()and others)
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)