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.
>
>