dotsize behavior in geom_dotplot

1,700 views
Skip to first unread message

Tom W

unread,
Feb 27, 2012, 5:40:22 PM2/27/12
to ggp...@googlegroups.com
I've really been enjoying the new geom_dotplot facility in the developer version of ggplot. I've found it's a really nice way to annotate plots. 

I guess I'm confused about something though--I keep getting the size of the dots to scale to the variance on its home scale (or some other quantity) which I can't seem to override with the "dotsize" command. 

Consider the following 

library(devtools)
dev_mode()
library(ggplot2)

data(mtcars)
head(mtcars)

p1 <- ggplot(aes(x = disp, y = mpg), data=mtcars) +
       geom_point() +
       geom_dotplot(binaxis = "y", aes(x =50), dotsize= .75) +
       geom_dotplot(binaxis = "x", aes(y =80), dotsize= .75) 

ggsave(plot = p1, filename= "dotplot1.png")


I can normally futz around and make the points a similar size, but shouldn't this be the intuitive consequence of the dotsize command?



Winston Chang

unread,
Feb 27, 2012, 7:15:16 PM2/27/12
to Tom W, ggp...@googlegroups.com
Hi Tom -

The diameter is determined by the binwidth value  -- the diameter (when dotsize=1) is the same as the binwidth. The value of dotsize tells it to scale relative to binwidth. At present there's no way to set the dot size to be an absolute size. That wouldn't be too hard to do, though. When I wrote the code, it didn't occur to me that people might want to use dot plots this way, but I can see how it might be useful.

Also, I think Brian Danielak was experimenting with a geom for marginal dot plots -- maybe he'll chime in about it.

-Winston





--
You received this message because you are subscribed to the ggplot2 mailing list.
Please provide a reproducible example: http://gist.github.com/270442
 
To post: email ggp...@googlegroups.com
To unsubscribe: email ggplot2+u...@googlegroups.com
More options: http://groups.google.com/group/ggplot2

Winston Chang

unread,
Feb 27, 2012, 8:47:51 PM2/27/12
to Tom W, ggp...@googlegroups.com
Slight correction: the baseline of the vertical dot stacks will be at y=0, unless you set it like this:

ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point() +
  geom_dotplot(binaxis = "y", aes(x = 20), method = "histodot",
               dotsize = 2, binwidth = 2, abs_size = TRUE) +
  geom_dotplot(binaxis = "x", y = 5, method = "histodot",
               dotsize = 2, binwidth = 10, abs_size = TRUE) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0), limits=c(5, 35))

Notice that it's different from how x works -- the x position has to be set inside the aes(). There are some technical reasons for this... basically, it boils down to the the fact that x and y are treated somewhat differently in ggplot, and this geom wasn't designed for marginal plots like this.

-Winston

Winston Chang

unread,
Feb 27, 2012, 8:26:09 PM2/27/12
to Tom W, ggp...@googlegroups.com
On Mon, Feb 27, 2012 at 6:15 PM, Winston Chang <winsto...@gmail.com> wrote:
Hi Tom -

The diameter is determined by the binwidth value  -- the diameter (when dotsize=1) is the same as the binwidth. The value of dotsize tells it to scale relative to binwidth. At present there's no way to set the dot size to be an absolute size. That wouldn't be too hard to do, though. When I wrote the code, it didn't occur to me that people might want to use dot plots this way, but I can see how it might be useful.


The change to allow absolute dot sizes was pretty simple. There's now a parameter called abs_size. You can install the modified version and run this example:

library(devtools)
dev_mode()
install_github('ggplot2', 'wch', 'experimental')
library(ggplot2)

ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point() +
  geom_dotplot(binaxis = "y", aes(x = 50), method = "histodot",
               dotsize = 2, binwidth = 2, abs_size = TRUE) +
  geom_dotplot(binaxis = "x", aes(y = 80), method = "histodot",
               dotsize = 2, binwidth = 10, abs_size = TRUE) +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0))

The stuff with the scales removes the padding, so that the dot stacks go right on the edge.

Bear in mind that the dot diameter now has no relation to the bin width. I used histodot binning because using dot-density binning would be more confusing in this case, where the dot diameter doesn't tell you anything about bin width. Still, even histodot binning could be misleading if the dot diameter is wildly different from bin width.

-Winston

dotplot_abs_size.png

Tom W

unread,
Feb 27, 2012, 10:34:56 PM2/27/12
to ggp...@googlegroups.com, Tom W
wow, thanks Winston! That's a huge improvement for using the geom for this purpose.  

Winston Chang

unread,
Feb 27, 2012, 11:56:56 PM2/27/12
to Tom W, ggp...@googlegroups.com
I think I have a better solution... It doesn't require an absolute dot size, so it can be run with the regular version of ggplot2. It does, however, require you to specify the ratio of width to height of your output graphic. It also needs you to specify x and y range, but if you're clever about it, you can calculate it using range() and then expanding a bit to give room for the marginal dot plots.

The advantage to this code is that the dot width is always meaningful -- in the previous example I gave with dot size specified in mm, the dot width had no determinate relation to the bin width, and so you could have bins that are much narrower or wider than the dots. Adding to the confusion is that the relation can be different on the two axes: the bins could be narrower than the dots on one axis, and wider than the dots on the other.


xyratio <- 2/1     # Ratio of width/height in output graphic

xrange <- c(20,500)
yrange <- c(5,35)

nxbins <- 60       # Number of bins on x-axis
nybins <- nxbins / xyratio

xbinwidth <- diff(xrange)/nxbins
ybinwidth <- diff(yrange)/nybins

ggplot(mtcars, aes(x = disp, y = mpg)) + geom_point() +
  geom_dotplot(binaxis = "y", aes(x = xrange[1]), binwidth = ybinwidth) +
  geom_dotplot(binaxis = "x", y = yrange[1], binwidth = xbinwidth) +
  scale_x_continuous(expand = c(0, 0), limits = xrange) +
  scale_y_continuous(expand = c(0, 0), limits = yrange)


I've attached two versions: one where the x:y ratio is 1:1, and another where its 2:1. The x and y dot sizes aren't exactly the same in the 2:1 version, because even though the overall image is a perfect 2:1 ratio (400x200), the plotting area isn't exactly 2:1. Tweaking the value of xyratio would fix this.

-Winston


--
marginaldotplot_1-1.png
marginaldotplot_2-1.png
Reply all
Reply to author
Forward
0 new messages