Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
branch: master
Fetching contributors…

Cannot retrieve contributors at this time

file 74 lines (60 sloc) 2.639 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74

library(ggplot2)
library(gtable)
data(mtcars)

# Main scatterplot
p1 <- ggplot(mtcars, aes(mpg, hp)) +
  geom_point() +
  scale_x_continuous(expand = c(0, 0)) +
  scale_y_continuous(expand = c(0, 0)) +
  expand_limits(y = c(min(mtcars$hp) - .1*diff(range(mtcars$hp)),
                      max(mtcars$hp) + .1*diff(range(mtcars$hp)))) +
  expand_limits(x = c(min(mtcars$mpg) - .1*diff(range(mtcars$mpg)),
                      max(mtcars$mpg) + .1*diff(range(mtcars$mpg)))) +
  theme(plot.margin= unit(c(0, 0, 0.5, 0.5), "lines"))
 
# To remove all axis labelling and marks from the two marginal plots
theme_remove_all <- theme(axis.text = element_blank(),
  axis.title = element_blank(),
  axis.ticks = element_blank(),
  axis.ticks.margin = unit(0, "lines"),
  axis.ticks.length = unit(0, "cm"))

# Horizontal marginal density plot - to appear at the top of the chart
p2 <- ggplot(mtcars, aes(x = mpg)) +
  geom_density() +
  scale_x_continuous(expand = c(0, 0)) +
  expand_limits(x = c(min(mtcars$mpg) - .1*diff(range(mtcars$mpg)),
                      max(mtcars$mpg) + .1*diff(range(mtcars$mpg)))) +
  theme_remove_all +
  theme(plot.margin= unit(c(0.5, 0, 0, 0.5), "lines"))
               
# Vertical marginal density plot - to appear at the right of the chart
p3 <- ggplot(mtcars, aes(x = hp)) +
  geom_density() +
  scale_x_continuous(expand = c(0, 0)) +
  expand_limits(x = c(min(mtcars$hp) - .1*diff(range(mtcars$hp)),
                      max(mtcars$hp) + .1*diff(range(mtcars$hp)))) +
  coord_flip() +
  theme_remove_all +
  theme(plot.margin= unit(c(0, 0.5, 0.5, 0), "lines"))

# Get the gtables
gt1 <- ggplot_gtable(ggplot_build(p1))
gt2 <- ggplot_gtable(ggplot_build(p2))
gt3 <- ggplot_gtable(ggplot_build(p3))

# Get maximum widths and heights for x-axis and y-axis title and text
maxWidth = unit.pmax(gt1$widths[2:3], gt2$widths[2:3])
maxHeight = unit.pmax(gt1$heights[4:5], gt3$heights[4:5])

# Set the maximums in the gtables for gt1, gt2 and gt3
gt1$widths[2:3] <- as.list(maxWidth)
gt2$widths[2:3] <- as.list(maxWidth)

gt1$heights[4:5] <- as.list(maxHeight)
gt3$heights[4:5] <- as.list(maxHeight)

# Combine the scatterplot with the two marginal boxplots
# Create a new gtable
gt <- gtable(widths = unit(c(7, 2), "null"), height = unit(c(2, 7), "null"))

# Instert gt1, gt2 and gt3 into the new gtable
gt <- gtable_add_grob(gt, gt1, 2, 1)
gt <- gtable_add_grob(gt, gt2, 1, 1)
gt <- gtable_add_grob(gt, gt3, 2, 2)

# And render the plot
grid.newpage()
grid.draw(gt)


grid.rect(x = 0.5, y = 0.5, height = 0.995, width = 0.995, default.units = "npc",
    gp = gpar(col = "black", fill = NA, lwd = 1))
Something went wrong with that request. Please try again.