Re: fix width of plotting area / margin

Showing 1-2 of 2 messages
Re: fix width of plotting area / margin Dennis Murphy 1/28/13 4:28 PM
Hi:

The reason the x-axes of the scatterplot and the density plots above
them don't match is because the y-axis tick labels of the two panels
don't have the same length. Rotating the labels by 90 degrees should
fix the problem, especially if you reduce the number of labels in the
upper density plot. Try this:

library(ggplot2)
library(gtable)
data(mtcars)
p <- qplot(data = mtcars, mpg, hp, geom = "point", colour = cyl) +
        theme(axis.text.y = element_text(angle = 90, hjust = 0.5))
#p1 <- p + theme(legend.position="none")

p2 <- ggplot(mtcars, aes(x=mpg, group=cyl, colour=cyl)) + geom_density()
#p2 <- p2 + stat_density(fill = NA, position="dodge")
p2 <- p2 + theme(legend.position="none",
                 axis.text.x = element_blank(),
                 axis.title.x = element_blank(),
                 axis.text.y = element_text(angle = 90, hjust = 0.5)) +
           scale_y_continuous(breaks = c(0, 0.1, 0.2))

p3 <- ggplot(mtcars, aes(x=hp, group=cyl, colour=cyl)) +
          geom_density() +
          coord_flip()
#p3 <- p3 + stat_density(fill = NA, position="dodge") + coord_flip()
p3 <- p3 + theme(legend.position="none",
                 axis.text.y = element_blank(),
                 axis.title.y = element_blank())

gt1 <- ggplot_gtable(ggplot_build(p))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))

gt <- gtable(widths = unit(c(2, 1), "null"), height = unit(c(1, 2), "null"))
gt <- gtable_add_grob(gt, gt1[, -5], 2, 1)
gt <- gtable_add_grob(gt, gt2, 1, 1)
gt <- gtable_add_grob(gt, gt3, 2, 2)
gt <- gtable_add_grob(gt, gt1[, 5], 1, 2)
grid.newpage()
grid.draw(gt)

Dennis

On Mon, Jan 28, 2013 at 12:29 PM,  <sha...@web.de> wrote:
> I would like to re-create a plot similar to this:
> http://blog.mckuhn.de/2009/09/learning-ggplot2-2d-plot-with.html
> It uses deprecated syntax, so with the help of
> http://article.gmane.org/gmane.comp.lang.r.ggplot2/5791
> I managed to get this far:
>
>
> library(ggplot2)
> library(gtable)
> data(mtcars)
> p <- qplot(data = mtcars, mpg, hp, geom = "point", colour = cyl)
> #p1 <- p + theme(legend.position="none")
>
> p2 <- ggplot(mtcars, aes(x=mpg, group=cyl, colour=cyl)) + geom_density()
> #p2 <- p2 + stat_density(fill = NA, position="dodge")
> p2 <- p2 + theme(legend.position="none", axis.text.x = element_blank(),
> axis.title.x = element_blank())
>
> p3 <- ggplot(mtcars, aes(x=hp, group=cyl, colour=cyl)) + geom_density() +
> coord_flip()
> #p3 <- p3 + stat_density(fill = NA, position="dodge") + coord_flip()
> p3 <- p3 + theme(legend.position="none", axis.text.y = element_blank(),
> axis.title.y = element_blank())
>
> gt1 <- ggplot_gtable(ggplot_build(p))
> gt2 <- ggplot_gtable(ggplot_build(p2))
> gt3 <- ggplot_gtable(ggplot_build(p3))
>
> gt <- gtable(widths = unit(c(2, 1), "null"), height = unit(c(1, 2), "null"))
> gt <- gtable_add_grob(gt, gt1[, -5], 2, 1)
> gt <- gtable_add_grob(gt, gt2, 1, 1)
> gt <- gtable_add_grob(gt, gt3, 2, 2)
> gt <- gtable_add_grob(gt, gt1[, 5], 1, 2)
> grid.newpage()
> grid.draw(gt)
>
>
> What I dislike about this is that the x-axis of the scatter plot does not
> align with the x-axis of the density curves above.
> This is due to the different lengths of y-axis tick labels (in my real use
> case the difference is larger).
> Is there a solution to set the plotting area to a fixed size / fix the space
> for the margin that contains the axis texts and label?
>
> I found a similar question on the R mailing list, but no reply:
>  https://stat.ethz.ch/pipermail/r-help/2012-December/332363.html
>
> Thanks in advance...
>
> --
> --
> You received this message because you are subscribed to the ggplot2 mailing
> list.
> Please provide a reproducible example:
> https://github.com/hadley/devtools/wiki/Reproducibility
>
> To post: email ggp...@googlegroups.com
> To unsubscribe: email ggplot2+u...@googlegroups.com
> More options: http://groups.google.com/group/ggplot2
>
> ---
> You received this message because you are subscribed to the Google Groups
> "ggplot2" group.
> To unsubscribe from this group, send email to
> ggplot2+u...@googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
>
Re: fix width of plotting area / margin sha...@web.de 1/30/13 9:05 AM
Thanks, this works!

I changed the plot.margin to minimize the white space between the plots.

Now I'm looking for a flexible solution for the y axis labels. Instead of using hard coded breaks as suggested

+ scale_y_continuous(breaks = c(0, 0.1, 0.2))

I read that it's possible to give a function to set the breaks.

So, I made a VERY simple (not to stay stupid) approach:
MyBreaks <- function (a, ...) {
  min = a[1]
  max = a[2]
##as I understood that's what the function gets
  return(c(min, max))
}

and then use this for the scale in p2 (possibly others as well)

So, that does work, i.e. it does something, but now it would need to do something smart...
Is there a way to get the values that are found by default function and work with them instead of figuring out everything from scratch?

The code as it looks now...


library(ggplot2)
library(gtable)
data(mtcars)
p <- qplot(data = mtcars, mpg, hp, geom = "point", colour = cyl) +
        theme(axis.text.y = element_text(angle = 90, hjust = 0.5),
              plot.margin = unit(c(0.5,0.5,0,0), "lines"))

p2 <- ggplot(mtcars, aes(x=mpg, group=cyl, colour=cyl)) + geom_density() +

        theme(legend.position="none",
              axis.text.x = element_blank(),
              axis.title.x = element_blank(),
              axis.text.y = element_text(angle = 90, hjust = 0.5),
              plot.margin = unit(c(1,0.5,0,0), "lines")) +
        scale_y_continuous(breaks = MyBreaks)


p3 <- ggplot(mtcars, aes(x=hp, group=cyl, colour=cyl)) +
        geom_density() +
        coord_flip() +

        theme(legend.position="none",
              axis.text.y = element_blank(),
              axis.title.y = element_blank(),
              plot.margin = unit(c(0.5,1,0,0), "lines"))