0

Hi I would like to stack a dot plot on top of a density plot. I can do this with ggplot2 however I can only do so with grid align. Which sort of works but I cannot seem to force the zero on each plot to align and secondly would be better if I can get the density plot to be on the same panel instead just two plots stack together.

For example here is what I can do so far. Here a purposely put the legend to the left to show that that the zero's are not aligning. Also notice that both plots are not on the same panel.

library ( ggplot2 )
library(gridExtra)
set.seed(12345)
N <- 10000
x <- rnbinom(N, 1, .15)
x = data.frame ( value=x, yyy= rep("z", length(x)))


g1 <- ggplot(x, aes(y=value, x=yyy))+
    geom_point(aes(fill = yyy), size = 3, shape = 21, position = position_jitterdodge(), alpha = .14 )+
    theme_bw()  + coord_flip() + theme(legend.position="left")


h1 = ggplot( x, aes(value)) +
    geom_histogram(aes(y = stat(density)), binwidth = .1) 

grid.arrange(g1, h1,  ncol=1 )

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
Ahdee
  • 4,679
  • 4
  • 34
  • 58
  • Read about [cowplot](https://cran.r-project.org/web/packages/cowplot/vignettes/plot_grid.html). – zx8754 Jan 16 '19 at 07:58
  • 1
    @zx8754 I guess it's not a duplicate if OP wants to *get the density plot to be on the same panel instead just two plots*. Or question is very unclear. – pogibas Jan 16 '19 at 08:10
  • 1
    @PoGibas Feel free to vote to re-open. From what they have tried it looks like 2 plots alignment problem. I understand your solution is ideal for this, so decided to close it. – zx8754 Jan 16 '19 at 08:15

2 Answers2

3

You can easily put them on the same plot using geom_point + geom_histogram principal. For that you will need to adjust two parameters manually: position of points on the y-axis (yPos) and strength of vertical jitter (jit).

yPos <- 1700
jit <- 100

ggplot(x) + 
    geom_point(aes(value, yPos, fill = yyy), 
               position = position_jitterdodge(jitter.height = jit), 
               size = 3, shape = 21, alpha = 0.14) +
    geom_histogram(aes(value), binwidth = 0.1) + 
    theme_bw() +
    theme(legend.position = "left")

Final solution looks like this:

enter image description here

pogibas
  • 27,303
  • 19
  • 84
  • 117
  • this is great. Is there a way to easily figure out yPos without trial and error? – Ahdee Jan 16 '19 at 14:28
  • Looking it more I guess it would be determined by the highest density point from geom_histogram but how to do that is the question I guess. Thanks. – Ahdee Jan 16 '19 at 14:36
0

If the requirement to have the legend on the left can be relaxed and put the legend to the top then you can use ggarrange, both graphs y axis will align

library ( ggplot2 )
library(ggpubr)

set.seed(12345)
N <- 10000
x <- rnbinom(N, 1, .15)
x = data.frame ( value=x, yyy= rep("z", length(x)))


g1 <- ggplot(x, aes(y=value, x=yyy))+
  geom_point(aes(fill = yyy), size = 3, shape = 21, position = position_jitterdodge(), alpha = .14 )+
  theme_bw()  + coord_flip() + theme(legend.position="top")


h1 = ggplot( x, aes(value)) +
  geom_histogram(aes(y = stat(density)), binwidth = .1) 


ggarrange(g1, h1,  ncol=1, nrow=2, align = "v" )