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
Jonas Heidelberg
)scales
library (thanks B0WSER
)legend=
(replaced by guide=
)opts()
(replaced by labs()
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)
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.
Have the same problem.
Adding
library(scales)
solved this problem.
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With