Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Alignment and scaling issue #4

Closed
atkinsjeff opened this issue Mar 19, 2015 · 4 comments
Closed

Alignment and scaling issue #4

atkinsjeff opened this issue Mar 19, 2015 · 4 comments
Assignees

Comments

@atkinsjeff
Copy link

Follow-up from Twitter conversation...

Trying to make multiple waffle charts, in this case three of them, of raw abundance count, for comparison of abundance against the individual site. Example below is for brook trout abundance in three streams:

part one making the individual plots:

> pain.adult.1997 <- c( `YOY (406)`=406, `Adult (24)`=24)
p.pain.waffle.1997 <- waffle(pain.adult.1997/2, rows=7, size=0.5, 
       colors=c("#c7d4b6", "#a3aabd"), 
       title="Paine Run Brook Trout Abundance (1997)", 
       xlab="1 square = 2 fish")

> pine.adult.1997 <- c( `YOY (221)`=221, `Adult (143)`=143)
p.pine.waffle.1997 <- waffle(pine.adult.1997/2, rows=7, size=0.5, 
                             colors=c("#c7d4b6", "#a3aabd"), 
                             title="Piney River Brook Trout Abundance (1997)", 
                             xlab="1 square = 2 fish")

> stan.adult.1997 <- c( `YOY (270)`=270, `Adult (197)`=197)
p.stan.waffle.1997 <- waffle(stan.adult.1997/2, rows=7, size=0.5, 
                             colors=c("#c7d4b6", "#a3aabd"), 
                             title="Staunton River Trout Abundance (1997)", 
                             xlab="1 square = 2 fish")

I tried a back door method that I have found works well with typical plot elements using the gridExtra and gtable packages to arrange plots. This code is pretty rough. This actually fixes the problem of scaling, but then centers everything.

attempting to gride the waffles

require(gridExtra)
require(gtable)

pain panel graph

gA7 <- ggplot_gtable(ggplot_build(p.pain.waffle.1997))
gB7 <- ggplot_gtable(ggplot_build(p.pine.waffle.1997))
gC7 <- ggplot_gtable(ggplot_build(p.stan.waffle.1997))

maxWidth = unit.pmax(gA7$widths[2:3],
gB7$widths[2:3],
gC7$widths[2:3])

Set the widths

gA7$widths[2:3] <- maxWidth
gB7$widths[2:3] <- maxWidth
gC7$widths[2:3] <- maxWidth

Arrange the four charts

grid.arrange(gA7, gB7, gC7, nrow=3)

This may be a rather crude way to do it if one wants to make multiple graphs (belaying my abject novice status here). But interested in how to best go about this.

@hrbrmstr hrbrmstr self-assigned this Mar 19, 2015
@hrbrmstr
Copy link
Owner

Perfect examples. Very much appreciated. I pushed (literally minutes ago) a version with many nulled margins. Give that one a go if you can (it won't fix all the issues). I ran that updated version with your code and did a grid.arrange of the output.

image

The charts aren't left-aligned, but the blocks are the same size at least.

@atkinsjeff
Copy link
Author

Oh that's awesome. Thanks. I am going to play around with the left-align
and see if I can get that to work. Some threads in stackoverflow lead to
dead ends.

--jeff

On Thu, Mar 19, 2015 at 11:52 AM, boB Rudis [email protected]
wrote:

Perfect examples. Very much appreciated. I pushed (literally minutes ago)
a version with many nulled margins. Give that one a go if you can (it won't
fix all the issues). I ran that updated version with your code and did a
grid.arrange of the output.

[image: image]
https://cloud.githubusercontent.com/assets/509878/6734404/59e6b5e0-ce2e-11e4-92da-e6cf488fcf2e.png

The charts aren't left-aligned, but the blocks are the same size at least.


Reply to this email directly or view it on GitHub
#4 (comment).

Jeff Atkins
Ph.D. Candidate
Department of Environmental Sciences
University of Virginia

@hrbrmstr
Copy link
Owner

I added a parameter pad to waffle to add pad columns of blocks. If you find the max width (I guess I shld write a small function for that, eh? ;-) in terms of # of blocks for the largest plot in a series, then pad the others to get to the same total width. Then use the new iron function to stitch them together. Like this:

pain.adult.1997 <- c( `YOY (406)`=406, `Adult (24)`=24)
A <- waffle(pain.adult.1997/2, rows=7, size=0.5, 
       colors=c("#c7d4b6", "#a3aabd"), 
       title="Paine Run Brook Trout Abundance (1997)", 
       xlab="1 square = 2 fish", pad=3)

pine.adult.1997 <- c( `YOY (221)`=221, `Adult (143)`=143)
B <- waffle(pine.adult.1997/2, rows=7, size=0.5, 
                             colors=c("#c7d4b6", "#a3aabd"), 
                             title="Piney River Brook Trout Abundance (1997)", 
                             xlab="1 square = 2 fish", pad=8)

stan.adult.1997 <- c( `YOY (270)`=270, `Adult (197)`=197)
C <- waffle(stan.adult.1997/2, rows=7, size=0.5, 
                             colors=c("#c7d4b6", "#a3aabd"), 
                             title="Staunton River Trout Abundance (1997)", 
                             xlab="1 square = 2 fish")


iron(A, B, C)

image

Let me know how that works for you.

@atkinsjeff
Copy link
Author

This looks great. That the function is called Iron makes it even better!

--jeff

On Thu, Mar 19, 2015 at 7:32 PM, boB Rudis [email protected] wrote:

I added a parameter pad to waffle to add pad columns of blocks. If you
find the max width (I guess I shld write a small function for that, eh? ;-)
in terms of # of blocks for the largest plot in a series, then pad the
others to get to the same total width. Then use the new iron function to
stitch them together. Like this:

pain.adult.1997 <- c( YOY (406)=406, Adult (24)=24)
A <- waffle(pain.adult.1997/2, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd"),
title="Paine Run Brook Trout Abundance (1997)",
xlab="1 square = 2 fish", pad=3)

pine.adult.1997 <- c( YOY (221)=221, Adult (143)=143)
B <- waffle(pine.adult.1997/2, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd"),
title="Piney River Brook Trout Abundance (1997)",
xlab="1 square = 2 fish", pad=8)

stan.adult.1997 <- c( YOY (270)=270, Adult (197)=197)
C <- waffle(stan.adult.1997/2, rows=7, size=0.5,
colors=c("#c7d4b6", "#a3aabd"),
title="Staunton River Trout Abundance (1997)",
xlab="1 square = 2 fish")

iron(A, B, C)

[image: image]
https://cloud.githubusercontent.com/assets/509878/6743322/b3d85e60-ce6e-11e4-981f-5266c4e0c833.png

Let me know how that works for youl


Reply to this email directly or view it on GitHub
#4 (comment).

Jeff Atkins
Ph.D. Candidate
Department of Environmental Sciences
University of Virginia

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants