New Features in ggplot2 version 0.8.5

January 6, 2010

(This article was first published on Learning R, and kindly contributed to R-bloggers)

Just before Christmas ggplot2 version 0.8.5 was released, closely following the release of version 0.8.4 a week or so earlier. Whilst both versions included included numerous bugfixes (25 in 0.8.4 and 17 in 0.8.5), the latest version also incorporated some new features.

As ggplot2 is all about graphical display, so I went through the list of new features and below is a visual example of each new feature, plotted most often utilising the code examples included in the respective bugtracker issues.

1)geom_text gains parse argument which makes it possible to display expressions

> library(ggplot2)
> set.seed(1)
> mydata <- data.frame(x = sample(10), y = sample(10))
> ggplot(mydata, aes(x, y)) + geom_point() + annotate("text",
+     x = mydata[6, 1], y = mydata[6, 2], label = "beta[1] == 1",
+     parse = T, vjust = 0, hjust = -0.1)

2) all scales now have legend parameter, which defaults to TRUE. Setting to false will prevent that scale from contributing to the legend.

> ggplot(mydata, aes(x, y, colour = x)) + geom_point(legend = FALSE) +
+     geom_line()

In previous version the legend of the above plot looked like this and there was now way to change this.

> ggplot(mydata, aes(x, y, colour = x)) + geom_point() +
+     geom_line()

3) default axis labels and legend titles are now stored in the options, instead of in each scale.

This allows to specify them using the opts call.

> p <- ggplot(mtcars, aes(cyl, mpg, colour = cyl)) +
+     geom_point()
> p + opts(labels = c(x = "Cylinders", y = "Miles per gallon",
+     colour = "Colour"))

The other options to set labels exist as previously:

> p + labs(x = "Cylinders")
> p + xlab("Cylinders")
> p + scale_colour_gradient(name = "Colour")

4) coord_equal: when ratio = NULL (the default), it will adjust the aspect ratio of the plot, rather than trying to extend the shortest axis.

> qplot(mpg, wt, data = mtcars) + coord_equal()

This is what the default looked like in the previous version, and it was impossible to generate the plot above.

> qplot(mpg, wt, data = mtcars) + coord_equal(ratio = 1)

5) x and y positions can be set to Inf or -Inf to refer to the top/right and bottom/left extents of the panel.

This is useful for annotations, and for geom_rect, geom_vline and geom_hline.

> ggplot(data.frame(x = 0:1, y = 0:1)) + geom_rect(aes(x = x,
+     y = y), xmin = 0.1, xmax = 0.2, ymin = -Inf,
+     ymax = Inf)

6)expand_limits(): a new function to make it easy to force the inclusion of any set of values in the limits of any aesthetic.

> p <- qplot(mpg, wt, data = mtcars)
> p + expand_limits(x = 0)
> p + expand_limits(y = c(1, 9))

is the equivalent of

> p + ylim(c(1, 9))
> p + expand_limits(x = 0, y = 0)

It would be good if similar functionality could be extended to xlim, so that the following would work. This way the setting of limits would be encapsulated in one function (xlim/ylim).

> p + xlim(0, Inf)

To leave a comment for the author, please follow the link and comment on their blog: Learning R. offers daily e-mail updates about R news and tutorials on topics such as: Data science, Big Data, R jobs, visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...

If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Comments are closed.

Search R-bloggers


Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)