1

Let's reproduce the example we are working with:

chol <- read.table(url("http://assets.datacamp.com/blog_assets/chol.txt"), header = TRUE)

We are now representing two ggplot histograms:

library(ggplot2)
plot1 <- ggplot(data=chol, aes(chol$AGE)) + 
geom_histogram(breaks=seq(20, 50, by = 2), 
               col="red", 
               fill="green", 
               alpha = .2) + 
labs(title="Histogram for Age") +
labs(x="Age", y="Count") + 
xlim(c(18,52)) + 
ylim(c(0,100))

plot2 <- ggplot(data=chol, aes(WEIGHT)) + 
    geom_histogram() + 
    labs(title="Histogram for Weigth") +
    labs(x="Weigth", y="Count") +
    ylim(0,50)

These are the two histogramas, first plot1 and second plot2. enter image description here enter image description here

I would like to merge both of them, representing plot1 in the X axis, and plot2 in the Y axis of the new plot. The result might be similar to this: enter image description here

How can I achieve this goal?

mnm
  • 1,962
  • 4
  • 19
  • 46
antecessor
  • 2,688
  • 6
  • 29
  • 61

2 Answers2

1

You can try following. Although I'm not understanding the sense of such overplotting.

plot1 <- ggplot(data=chol, aes(AGE)) + 
  geom_histogram(breaks=seq(20, 50, by = 2), 
                 col="red", 
                 fill="green", 
                 alpha = .2) + 
  labs(x="Age", y="Count") + 
  xlim(c(18,52)) + 
  ylim(c(0,100))

plot2 <- ggplot(data=chol, aes(WEIGHT)) + 
  geom_histogram() + 
  labs(x="Weigth", y="Count") +
  scale_x_continuous(position = "top")+
  scale_y_reverse(position = "right",limits = c(50,0)) +
  coord_flip()

library(cowplot)
ggdraw(plot1) + 
  draw_plot(plot2)

enter image description here

camille
  • 16,432
  • 18
  • 38
  • 60
Roman
  • 17,008
  • 3
  • 36
  • 49
  • Right, and how can I make right bottom corner to fit among both plots @Jimbou? – antecessor May 30 '18 at 12:55
  • The issue is that I would like to represent a square in the middle representing 95% confidence intervals of both data, that's why I want this kind of graphic representation. But this would be the next question in stackoverflow @Jimbou – antecessor May 30 '18 at 12:59
  • @Jimbou I'm editing to change the `chol$AGE` inside your `aes` to the proper bare column name `AGE`—looks like a holdover from the OP's original code – camille May 30 '18 at 14:51
0

I use the code of next blog and I adapted to your example. You have both histograms in one chart, and also a scatterplot.But instead to be infront off they are back to back.

http://sas-and-r.blogspot.com/2011/06/example-841-scatterplot-with-marginal.html

scatterhist = function(x, y, xlab="", ylab=""){
zones=matrix(c(2,0,1,3), ncol=2, byrow=TRUE)
layout(zones, widths=c(4/5,1/5), heights=c(1/5,4/5))
xhist = hist(x, plot=FALSE)
yhist = hist(y, plot=FALSE)
top = max(c(xhist$counts, yhist$counts))
par(mar=c(3,3,1,1))
plot(x,y)
par(mar=c(0,3,1,1))
barplot(xhist$counts, axes=FALSE, ylim=c(0, top), space=0)
par(mar=c(3,0,1,1))
barplot(yhist$counts, axes=FALSE, xlim=c(0, top), space=0, horiz=TRUE)
par(oma=c(3,3,0,0))
mtext(xlab, side=1, line=1, outer=TRUE, adj=0, 
      at=.8 * (mean(x) - min(x))/(max(x)-min(x)))
mtext(ylab, side=2, line=1, outer=TRUE, adj=0, 
      at=(.8 * (mean(y) - min(y))/(max(y) - min(y))))
}

#with your code#

with(chol, scatterhist(chol$AGE, chol$WEIGHT, xlab="AGE", ylab="WEIHGT"))
Community
  • 1
  • 1
Marta
  • 27
  • 7