Darren Dahly PhD Statistical Epidemiology

Scatterplot with Densities

I got it in my head today that I wanted a scatter plot with the density of each variable plotted on the respective axis. FML. The solution was found in the Stack Overflow thread linked below. The tricky bit was getting the margins exactly right. To to this, I diplayed all the panel backgrounds (which I removed in the final plot) so I could see the misalignment, and then played with the plot margins until it was right.

Thanks Stack Overflow!

Scatterplot

library(ggplot2)
library(RColorBrewer)
library(gridExtra)

a <- ggplot(data, aes(x = birthWeight)) +
       geom_density(fill = "#377eb8", color = "#377eb8", alpha = .5) +
       theme(plot.margin      = unit(c(0, 0, -0.7, 1.1), "cm"),
             axis.ticks       = element_blank(), 
             panel.background = element_blank(), 
             panel.grid       = element_blank(),
             axis.text.x      = element_blank(), 
             axis.text.y      = element_blank(),           
             axis.title.x     = element_blank(), 
             axis.title.y     = element_blank()) 
  
b <- ggplot(data, aes(x = customCentile)) +
       geom_density(fill = "#377eb8", color = "#377eb8", alpha = .5) +
       theme(plot.margin      = unit(c(0, 0, .64, -0.9), "cm"),
             axis.ticks       = element_blank(), 
             panel.background = element_blank(), 
             panel.grid       = element_blank(),
             axis.text.x      = element_blank(), 
             axis.text.y      = element_blank(),           
             axis.title.x     = element_blank(), 
             axis.title.y     = element_blank()) +
       coord_flip()
    
empty <- ggplot() +
           geom_point(aes(1,1), colour="white") +
           theme(plot.margin      = unit(c(0, 0, 0, 0), "cm"),
                 axis.ticks       = element_blank(), 
                 panel.background = element_blank(), 
                 panel.grid.minor = element_blank(),
                 axis.text.x      = element_blank(), 
                 axis.text.y      = element_blank(),           
                 axis.title.x     = element_blank(), 
                 axis.title.y     = element_blank())

scatter <- ggplot(data, aes(x     = birthWeight, 
                            y     = customCentile, 
                            color = macroCust)) +
             geom_point() +
             xlab("Birth Weight (g)") +
             ylab("Customised Centile") +
             scale_x_continuous(breaks = c(3500, 4000, 4500, 5000)) +
             scale_y_continuous(breaks = c(50, 75, 90, 95, 97.5)) +
             theme(plot.margin = unit(c(0, 0 , 0, 0), "cm"),
                   legend.position = "", 
                   panel.grid.minor = element_blank()) + 
             scale_color_brewer("", palette = "Set1")
  
birthweightGrob <- grid.arrange(a, empty, scatter, b, 
                                ncol = 2, 
                                nrow = 2, 
                                widths  = c(4, 1), 
                                heights = c(1, 4))

Another solution, which is much easier to implement, is geom_rug, which gives a nice dot-and-dash plot when combined with geom_point.

Dot and Dash

ggplot(data, aes(x = birthWeight, 
                   y = customCentile, 
                   color = macroCust)) +
  geom_point() +
  geom_rug(aes(alpha = 0.2)) +
  xlab("Birth Weight (g)") +
  ylab("Customised Centile") +
  scale_x_continuous(breaks = c(3500, 4000, 4500, 5000)) +
  scale_y_continuous(breaks = c(50, 75, 90, 95, 97.5)) +
  theme(legend.position = "", 
        panel.grid.minor = element_blank()) + 
        scale_color_brewer("", palette = "Set1")