ggplot: it can do what I want!

2015/06/11

As a scientist, I often run into this scenario:

A Cleveland dot chart is a nice way to show data, but if I have multiple replicates then the dots can start to mush on top of each other. R has a command dotchart that can do some of what I want: it can take a data frame, recognize that the y-axis has factors, and plot all the points. Unfortunatley, dotchart falls short when I want to, say, add some y-jitter so that all the points don’t overlap.

ggplot to the rescue! Say you have a dataframe df with values in column value and the treatment group name in column variable. Then you can quickly get an approximation of dotchart with ggplot(dat, aes(x=value, y=variable)) + geom_point(). Then, to get 10% jitter in the up-down direction, just change geom_point() to geom_point(position=position_jitter(height=0.1)).

Horizontal dots with jitter

Horizontal dots with jitter

If I have more data points, I often want a boxplot. With ggplot, it’s a piece of cake to keep the same data but change the plot: just add + coord_flip(). If I were using the boxplot command, then adding points is a big pain, and adding jittered points is an even bigger pain, especially if there are many treatments. ggplot makes this easy: the geom_boxplot command just takes the position_jitter argument as above!

Boxplot with jitter

Boxplot with jitter