0

I am trying to show two plots, one histogram and one scatter plot, and I wanted them to be aligned with respect to x-axis. I have created my histogram with hist() and my scatter plot with ggplot2. However, I found problems to create a multiplot mixing these two, so I decided to create the histogram with ggplot2. Please, find below the codes:

loadedimage <- readTIFF("my_data05.tiff")
rasterizedimage <- rasterGrob(loadedimage, interpolate = TRUE)
realmadrid_colors <-c("#00529f", "#8cc1f3", "blue")
data_test <- subset(full_data, Rival == "Barcelona")
data_kroos <- subset(data_test, Jugador == "Kroos")

myscatter <- ggplot(data_kroos, aes(x = x1, y = y1)) + 
  annotation_custom(rasterizedimage, xmin = 0, xmax = 713, ymin = 0 , ymax = 497) +
  geom_point(aes(fill = factor(data_kroos$Resultado)),
         shape = 21, 
         size = 5) +
  scale_fill_manual(values=realmadrid_colors) + 
  guides(fill=guide_legend(title=NULL)) +
  coord_fixed() + 
  theme_bw() + 
  theme_no_axes(base.theme = theme_bw()) + 
  theme(legend.position = c(0.5, 0.04),
    legend.box = "horizontal",
    legend.direction = "horizontal",
    legend.box.background = element_rect(fill = NA, colour = "transparent"),
    legend.text = element_text(family = "Courier", size = 14),
    axis.title = element_blank(),
    axis.text = element_blank(),
    axis.ticks = element_blank(),
    plot.margin=unit(c(-0.05,-0.05,-0.05,-0.1),"in")
    )

myhistogram <- qplot(data_barca[data_barca$Sección == "Passes_All",]$x1, breaks=seq(0, 660, by = 66), alpha = 0.5, geom = "histogram")

If I try using grid.newpage() and grid.draw(), I get an error message: ncol(x) == ncol(y) is not TRUE

grid.newpage()
grid.draw(rbind(ggplotGrob(myscatter), ggplotGrob(myhistogram), size = "first"))

I want to get something like in http://biokit.readthedocs.io/en/latest/auto_examples/plot_scatter.html (indeed I am still in the first step, my target is to exactly as in the former link), or something like combine histogram with scatter plot in R, but in that case it is not used ggplot2, and I am not sure if I can play around like using that package.

Any help is welcome! If you need me to upload any file, please, let me know.

Thanks in advance!

  • You'll find the solution in [stackoverflow: ggplot2 - multiple plots scaling](https://stackoverflow.com/questions/42220917/ggplot2-multiple-plots-scaling) – B.Gees Jun 06 '17 at 08:43
  • Thank you! Finally the answer was using ggExtra:: ggMarginal. It was exactly what I was looking for! – futbolscientist Jun 06 '17 at 10:43

1 Answers1

0

A further search maybe is the answer for me! The word I was looking for is marginal histograms!

Scatterplot with marginal histograms in ggplot2