0

my plot looks like this

enter image description here

This is what I've tried. I make individual scatter plots and combined them together with grid.arrange.

data(methylmercurydata) 
p1 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=logTHg)) + geom_point()
p2 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=OM)) + geom_point()
p3 <- ggplot(data=methylmercurydata,aes(x=MeHg, y=FeRB)) + geom_point()
grid.arrange(p1,p2,p3)
Ronak Shah
  • 377,200
  • 20
  • 156
  • 213
  • So what exactly do you want it to look like? It's easier to help you if you include a [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). What package is `methylmercurydata` from? you probably want to re-shape your data and then do faceting if you are using `ggplot2` – MrFlick Sep 23 '20 at 03:20

2 Answers2

1

Easiest way to do this would likely be using dplyr::facet_wrap() after creating a longer table.

Something like:

library(tidyverse)

methylmercurydata %>%
    pivot_longer(cols = c(logTHg, OM, FeRB), names_to = 'metric') %>%
    ggplot() +
    geom_point(aes(MeHG, value)) +
    facet_wrap(~metric)

Edit: r2evans makes a good point; if you need separate scales across y, you can use scales = 'free_y' within the facet_wrap call. Likewise, scales = 'free_x' would provide different x axes, and scales = 'free' would provide different scales for each axis. One other thing to consider when recreating the above plot would be specifying the ncol argument as well, in this case ncol = 1.

bradisbrad
  • 131
  • 4
  • 1
    Since the y-axes are a bit different, it might be good to use `facet_wrap(~ metric, scales = "free_y")`. – r2evans Sep 23 '20 at 03:41
  • @r2evans Great point. I've edited my response to include the `scales` argument, as well as the `ncol` argument. – bradisbrad Sep 23 '20 at 05:15
1

It's not clear to me what you mean by 'scatterplot matrix', but if you want to make a correlation matrix, you can use ggforce (e.g. per https://ihaddadenfodil.com/post/it-s-a-bird-it-s-a-plane-it-s-a-ggforce-function/):

library(tidyverse)
library(ggforce)
library(palmerpenguins)

ggplot(penguins, aes(col = sex)) +
  geom_autopoint(na.rm = TRUE) +
  facet_matrix(rows = vars(bill_length_mm:body_mass_g),
               switch = "x")

example plot

jared_mamrot
  • 22,354
  • 4
  • 21
  • 46