1

How to creating scatterplots with left and bottom histograms just like in the sample below in ggplot2?

enter image description here

library(ggplot2)
library(gridExtra)

data1<-diamonds
detrend<-lm(log(price)~log(carat),data=data1)
data1$lprice2<-resid(detrend)

empty <- ggplot()+geom_point(aes(1,1), colour="white")+
     opts(axis.ticks=theme_blank(), 
     panel.background=theme_blank(), 
     axis.text.x=theme_blank(), axis.text.y=theme_blank(),           
     axis.title.x=theme_blank(), axis.title.y=theme_blank())

scatter<-qplot(log(carat),lprice2,data=data1,xlab="Weight",ylab="Price Residuals",
     colour=factor(color),main="Diamonds - Weight to Price by Color")
scatter<-scatter+theme(legend.position="top")
scatter<-scatter+theme(plot.title=element_text(size=20,colour="blue"))


hist_left<-ggplot(data1,aes(x=price, fill=color))+geom_histogram(aes(y = ..density..))+
theme(legend.position = "none")+coord_flip()

hist_bottom<-ggplot(data1,aes(x=carat, fill=color))+geom_histogram()
 +theme(legend.position =     "none")

How to use grid.arrange to arrange these plot and how to flip the hist_left like the picture?

stata
  • 545
  • 1
  • 8
  • 20
  • 1
    http://stackoverflow.com/a/17372093/1412059 – Roland Oct 26 '14 at 10:36
  • Thank you! But I use grid.arrange(arrangeGrob(hist_left,scatter,ncol=2,widths=c(3,1)), arrangeGrob(hist_bottom,ncol=2,widths=c(3,1)), heights=c(1,3)) and can not plot pictures. – stata Oct 26 '14 at 10:44

2 Answers2

0

Try this as a starting point:

grid.arrange(hist_left, scatter, empty, hist_bottom, 
             widths=c(1, 4), as.table=FALSE, nrow=2)

enter image description here

Andrie
  • 176,377
  • 47
  • 447
  • 496
0

You should also remove the panel.grid in your blank placeholder plot and switch to element_blank vs theme_blank. Also, remove the labels on your hist_left.

library(ggplot2)
library(gridExtra)

data1 <- diamonds
detrend <- lm(log(price)~log(carat) ,data=data1)
data1$lprice2 <- resid(detrend)

empty <- ggplot()
empty <- empty + geom_point(aes(1,1), colour="white")
empty <- empty + theme(axis.ticks=element_blank(), 
                       panel.background=element_blank(), 
                       axis.text.x=element_blank(), 
                       axis.text.y=element_blank(),           
                       axis.title.x=element_blank(), 
                       axis.title.y=element_blank(),
                       panel.grid=element_blank())

scatter <- qplot(log(carat), lprice2, data=data1, 
                 xlab="Weight", ylab="Price Residuals",
                 colour=factor(color),
                 main="Diamonds - Weight to Price by Color")
scatter <- scatter + theme(legend.position="top")
scatter <- scatter + theme(plot.title=element_text(size=20, colour="blue"))

hist_left <- ggplot(data1,aes(x=price, fill=color))
hist_left <- hist_left + geom_histogram(aes(y = ..density..))
hist_left <- hist_left + labs(x=NULL, y=NULL, title=NULL)
hist_left <- hist_left + theme(legend.position = "none")

hist_bottom <- ggplot(data1, aes(x=carat, fill=color))
hist_bottom <- hist_bottom + geom_histogram()
hist_bottom <- hist_bottom + theme(legend.position = "none")

grid.arrange(arrangeGrob(hist_left + coord_flip(), scatter, ncol=2, widths=c(1,3)),
             arrangeGrob(empty, hist_bottom, ncol=2, widths=c(1,3)),
             heights=c(3,1))

enter image description here

You can come close to your goal with scale_x_reverse:

grid.arrange(arrangeGrob(hist_left + scale_x_reverse(), scatter, ncol=2, widths=c(1,3)),
             arrangeGrob(empty, hist_bottom, ncol=2, widths=c(1,3)),
             heights=c(3,1))

And, you can try to get your exact image by converting hist_left' to a grob witheditGrob` and playing with viewport parameters (e.g. but note that this is not what you want):

hlg <- ggplotGrob(hist_left)
hlg <- editGrob(hlg, vp=viewport(angle=90))

but you'll need to brush up on grid graphics to figure out how you want to manipulate the grob table components.

enter image description here

hrbrmstr
  • 77,368
  • 11
  • 139
  • 205