infotroph / ggplotTicks

Copy axes from bottom to top and left to right sides of a ggplot.
Other
9 stars 1 forks source link

Support multipanel plots #1

Closed infotroph closed 8 years ago

infotroph commented 11 years ago

In the current version, axes in single-panel plots are found by ggtable_filtering on axis-l and axis-b, then copying them into grobs having the same extents as panel. In panel n of the multi-panel plots I've checked, these change to axis_l-n, axis_b-n, and panel-n respectively.

If this naming scheme is consistent, then the easiest approach I see is to break mirror.ticks into two functions: one that takes a one-panel gtable and returns only the mirrored axis grobs in a format compatible with gtable_add_grob, and one wrapper that looks something like this:

npanel = length(gtable_filter(ggobj, "panel"))
for (i in 1:npanel){
    ptable = gtable_filter(ggobj, paste("-", i, sep=""), trim=FALSE)
    ggobj = do.call("gtable_add_grob", c(x=ggobj, mirror.ticks(ptable))
}

We'll need special cases to handle the naming differences between one-panel and multi-panel plots, of course, unless this can be changed in the gtable package instead.

infotroph commented 11 years ago

Clearly I had only looked at facet_wrap when I wrote the previous. I assumed, as is the case for facet_wrap, that every panel has its own left and bottom axes, whereas I now see that facet_grid produces only one left axis for each row and one bottom axis for each column. What's more, all n axes share the names axis-b and axis-l with no distinguishing identifiers.

In addition, with facet_wrap the axes for any panel not at the left or bottom edge of the plot area do exist and are numbered, but when scales="fixed" (which is the default condition), they're also null:

> testplot=(ggplot(mtcars, aes(wt, hp))
> gtable_filter(ggplotGrob(testplot+facet_wrap(~carb)), "axis")
TableGrob (6 x 8) "layout": 12 grobs
         z     cells     name
axis_l1 13 (1-1,1-1) axis_l-1
axis_l2 14 (1-1,4-4) axis_l-2
axis_l3 15 (1-1,7-7) axis_l-3
axis_l4 16 (5-5,1-1) axis_l-4
axis_l5 17 (5-5,4-4) axis_l-5
axis_l6 18 (5-5,7-7) axis_l-6
axis_b1 19 (2-2,2-2) axis_b-1
axis_b2 20 (2-2,5-5) axis_b-2
axis_b3 21 (2-2,8-8) axis_b-3
axis_b4 22 (6-6,2-2) axis_b-4
axis_b5 23 (6-6,5-5) axis_b-5
axis_b6 24 (6-6,8-8) axis_b-6
                                            grob
axis_l1 absoluteGrob[axis-l-1.absoluteGrob.8564]
axis_l2         zeroGrob[axis-l-2.zeroGrob.8565]
axis_l3         zeroGrob[axis-l-3.zeroGrob.8566]
axis_l4 absoluteGrob[axis-l-4.absoluteGrob.8573]
axis_l5         zeroGrob[axis-l-5.zeroGrob.8574]
axis_l6         zeroGrob[axis-l-6.zeroGrob.8575]
axis_b1         zeroGrob[axis-b-1.zeroGrob.8534]
axis_b2         zeroGrob[axis-b-2.zeroGrob.8535]
axis_b3         zeroGrob[axis-b-3.zeroGrob.8536]
axis_b4 absoluteGrob[axis-b-4.absoluteGrob.8543]
axis_b5 absoluteGrob[axis-b-5.absoluteGrob.8550]
axis_b6 absoluteGrob[axis-b-6.absoluteGrob.8557]

> gtable_filter(ggplotGrob(testplot+facet_grid(~carb)), "axis")
TableGrob (2 x 12) "layout": 7 grobs
   z         cells   name
1 13 ( 1- 1, 1- 1) axis-l
2 14 ( 2- 2, 2- 2) axis-b
3 15 ( 2- 2, 4- 4) axis-b
4 16 ( 2- 2, 6- 6) axis-b
5 17 ( 2- 2, 8- 8) axis-b
6 18 ( 2- 2,10-10) axis-b
7 19 ( 2- 2,12-12) axis-b
                                  grob
1 absoluteGrob[GRID.absoluteGrob.8339]
2 absoluteGrob[GRID.absoluteGrob.8303]
3 absoluteGrob[GRID.absoluteGrob.8309]
4 absoluteGrob[GRID.absoluteGrob.8315]
5 absoluteGrob[GRID.absoluteGrob.8321]
6 absoluteGrob[GRID.absoluteGrob.8327]
7 absoluteGrob[GRID.absoluteGrob.8333]
infotroph commented 10 years ago

https://cpsievert.shinyapps.io/ggtree/ looks very useful for visualizing the structure while I'm mucking around in the ggplot internals.

infotroph commented 9 years ago

Partially fixed in the multi panel-support branch, but includes a new allPanels flag that doesn't really work like it's supposed to yet. Use with caution, expect behavior to change more before the branch is merged.

infotroph commented 9 years ago

Getting closer with 66121128eb7f562cf134b1455b451149064b8b88 -- When whole columns have no x-axis (say, a five-panel facet_wrap(..., ncol=3, as.table=TRUE)), the x-axis is copied from the last complete column.

Left and bottom axes are still not correctly duplicated into rows/columns that already have then at the ends, though.

infotroph commented 8 years ago

This is basically fixed in v. 0.0.2. Depending on the plot and the theme, It's still really easy to produce something ugly when the plot is multipaneled (ticks hide behind facet labels and overlap behind panel edges, etc), but I'd argue that the tick placement part works as intended.