Showing posts with label R. Show all posts
Showing posts with label R. Show all posts

Friday, February 13, 2015

Here's one more option for generating plots in R: plotrix.
It can create some novel things like nested bar graphs and can search for empty space in plots.
There are a number of examples here. The 3D plots are probably best avoided but many of these examples include features not present in ggplot2.

Monday, February 09, 2015

A multitude of R plot examples

You don't have to do it all by hand anymore. Note: I don't work with this kind of data.

Here's what happens more often than it should when I sit down to make some figures:

  1. I start with R and remember seeing an example similar to what I'm trying to make
  2. Searching for the example leads me back to the helpful but limited ggplot2 docs or a Stackoverflow question
  3. I piece together what I need from what I've found, knowing all the while that a better example was out there somewhere

This kind of thing drives me crazy, so here's a short list of places where decent R graph examples can be found. Hopefully these sites help others find what they need. One caveat: just because R can spit out a particular figure doesn't mean that figure is appropriate or represents the data well. Your mileage may vary, etc.


  • R Graph Catalog. Intended to complement a guidebook, this set of examples covers a wide variety of presentations and audiences. It filters graph types based on whether they're recommended or not (but hey, you can still use the examples). All code is included right on the site and on Github.
  • Quick-R Graphs. This site covers the basics and includes some useful figures like a plotting symbol chart.  
  • Cookbook for R Graphs. Another book accompaniment.
  • Plotly R Library. Here's where things start to get exotic. Plotly isn't R specific but supposedly plays nice with ggplot2. It could be worth using for interactive charts.
  • Gvis cookbook. A ggplot2 alternative. It also allows for interactive graphs.
  • Wikibooks R Programming Graphics. A few more examples, including some in 3D (those should probably be avoided, honestly).
  • R-bloggers. Not a list of examples as much as a source for examples, especially those of the bleeding-edge kind.
If you're getting tired of finding example graphs, there's also GrapheR, a GUI for producing R graphics.

Edit: wanted to add a postscript about some hacky ways to make ggplot2 display patterns.

Edit 2: I found one more resource in the ZevRoss ggplot2 cheatsheet. It's primary a guide to themes in plots, one of the more fiddly aspects of plotting in R.


Friday, December 05, 2014

Another bit of R

R makes it easy to chop up and reassemble data frames, whether it's with the subset() function or with dplyr's filter() function. It isn't always obvious how to do it for a whole data frame. That's where apply() is useful.
This will filter a data frame such that all columns have at least one value less than zero in any row, for example:
newdata.df <- data.df[ ,apply(data.df, MARGIN = 2, function(x) any(x < 1))]

any() can be exhanged for all() to restrict the selection to columns in which all values are less than one.
The same code works for rows: just move the apply(...) in front of the comma, then change MARGIN to 1 instead of 2.

Thursday, November 13, 2014

I just found this software called Beaker today - it's essentially a way to mash together several different data analysis and presentation languages, allowing output from one to seamlessly become input for the next. To be fair, this isn't too difficult to do manually as long as the data sets are properly organized, but it's frequently a pain to convert something like R output to a presentable format without a few extra steps. Beaker appears to handle that. It'll even export to LaTeX as far as I can tell, so I can put off learning that for another year or so!

I haven't had a chance to try it out yet, but if Beaker is really as helpful as it seems then it could really save me some time. It would be nice to automatically export sets of R code and output to pretty HTML, at least.

Tuesday, October 21, 2014

Oh say say say.

R provides a seemingly endless toolbox of data visualization options. As a quick example, I was trying to find a way to create a treemap yesterday and a Flowingdata post provided the ideal R solution. It requires the portfolio package. Here's a test with some randomized data:

> testdata = data.frame(replicate(4,sample(0:1000,150,rep=FALSE)))
> map.market(id=testdata$X1, area=testdata$X2, group=testdata$X4, color=testdata$X3, main="Random Map")

The result looks like this:
Ignore the color key - there are no negative values here.
True, this example defeats one aspect of the purpose of a treemap. There's usually a hierarchy directed by qualitative variables. There is, in fact, room for two different quantitative variables and one category for each item in the data. We can certainly add a category to the data as it is:
> testdata$category <- ifelse(testdata$X4 > 800,"Large", "Small")

Now, every item with an X4 value greater than 800 will be in one category and everything smaller will be in the other. The output:
Neatly-organized boxes.
As expected, the smaller category ("Large") gets squished into one size of the figure and its contents get resized to fit. I miss the individual data labels though they got a bit dense.

The remaining issue is one of colors. The map.markey function allows for different scales but doesn't appear to provide many customization options. This blog post describes an alternate implementation which is RColorBrewer-friendly. Once installing and loading RColorBrewer (it's essential code, but it never seems to be installed when I need it), the newer treemap method is handled like this:
> treemap(testdata$X1, testdata$X2, testdata$category, testdata$X3, main="Random Map with Categories", pal="Reds", textcol="black")
From Sunset Snowbank to Waxy Red Delicious.
A treemap like this is likely a bit overkill for most applications, especially if there are too many categories or data items to be informative. A figure like this treemap of Vietnam's industrial exports provides an example: the smaller items are all unlabeled, so why include them? As always, results will vary and depend upon both data and conclusions.

Tuesday, April 15, 2014

Today I found out about the grid.arrange function for R (it's in the gridExtra package). It's one of those things which would have been useful to know about years ago. I just found it on a Stackoverflow answer today. The function just takes plots and squishes them together into a single canvas. It's easy, too: just enter
grid.arrange(p1, p2, nrow=2)
and you have everything in one place. The results look great with ggplot's faceting, too!
The extensive package library is absolutely one of the nicest things about R.

Thursday, January 23, 2014

Most of my day so far has been spent fighting with R. R can be very powerful when it comes to data visualization and plotting but it can also be a furious beast when it comes to parsing data. Many of its modules were written with very specific intentions and aren't quite clear about how they work under the hood.

I'm trying to make something like this, but prettier. Who doesn't like pretty colors?