Closed kylebaron closed 3 months ago
z <- rot_at(x, re = "CP", angle = 45) pm_grid(x)
There's a typo in this example, but pm_grid(z)
shows the expected output (Child-Pugh labels are rotated 45 degrees).
I love all this extra functionality! I was following along off-line as the options developed and this all looks to work as discussed off-line
Thanks @KatherineKayMRG and @kyleam for reviewing. This all started out as such a minor convenience function (probably replaced now with ggeasy
), but we worked ourselves into a place where we could really benefit from some good tools to really simplify the code. I'm feeling good that we can drop the last vestiges of the workarounds we've been carrying along for a while now.
Going to merge; I believe all the comments were implemented as suggested.
Summary
This PR tries to accomplish several things, with the common thread of modifying or using lists of plots, referring to names in that list.
New function
rot_xy()
. This is a wrapper around all of the other rot functions. It is generic with methods forgg
,patchwork
andlist
. The list method just callsrot_at()
. The other methods call this internal worker.rotxy()
, which knows how to userot_x()
androt_y()
for eithergg
objects (+
) orpatchwork
(&
). I wanted this master wrapper so we can pass in any of these objects and use a consistent interface. This also provides a "functional" approach for rotating axes, which I've wanted for some time; but I still expect to use lots ofrot_x()
androt_y()
getting "added" to the ggplot.New function:
rot_at()
, which provides functionality likerot_x()
androt_y()
for a list of plots, includinggg
andpatchwork
objects. The function expects all the plots to be named so we can selectively rotate particular plots in the list. By default, all plot axes will be rotated. To selectively rotate, use theat
argument for exact names orre
to pass a regular expression.Rotation is done by an non-exported generic function (
.rotxy()
) with methods forgg
andpatchwork
objects. We also pass anaxis
argument indicating if we want x or y rotated. 99.9% of the time, we do this to the x axis so this is reasonable IMO in order to cut down the amount of code we need to write.Because this is expecting names, this PR also adds names to list plot outputs; these plots were already vectorized (pass a data frame and a vector of y or x values and get a list of plots back) but we are now naming them so we can rotate. The functions are
list_plot_x()
,list_plot_y()
,list_plot_xy()
andlist_plot_yx()
.Now that we're generating lists of plots with names, I wanted to be able to arrange them using patchwork through a
with()
method (e.g.,with(plots, WT + ALB / SCR)
. Rather than adding a class attribute to these lists of plots and creating a new method forwith()
, I decided to just createpm_with()
which checks inputs and evaluates the plot assembly expression.EDIT: after testing
rot_at()
withaxis = "y"
, I wanted to makerot_y()
more consistent withrot_x()
; this means adding some equivalent convenience stuff I wrote intorot_x()
; I'm not crazy about it, but it works and I think having thevertical
argument torot_y()
is nice.Example
Rotate
Default output
Rotated
UPDATE: umbrella wrapper to rotate anything
A list
Ok not to name
But expecting an error if the list is not named
In this case, map
or
ggplot
patchwork
Pass a specific column name Also, we can pass arguments through to
rot_x()
androt_y()
Pass a regular expression
Change the axis
Check out refactored
rot_y()
Rotate both x and y axes with
vertical = TRUE
Name patterns
This gives us a single list
We get the categorical names back (x)
We get the continuous names back (x)
We get the eta names back (y)
We get YvX back
We get y names
We get y names
We still get y names
We get x names when we transpose
Check
list_plot()
behaviorpm_with()
We get y names when we take the list as-is
Created on 2024-07-17 with reprex v2.1.1