# Plotting 3d point cloud coordinates in three panels

#1

I'd like to plot a set of 3d point coordinates (x,y,z) as three 2d scatter plot panels in an "orthogonal view":

xy zy
xz

As a minimal example, let's consider the following data frame:

``````x <- c(0.9, 1.1, 1.0)
y <- c(2.0, 1.9, 2.1)
z <- c(3.1, 2.9, 3.0)

df <- data.frame(x,y,z)
``````

Now, I'd like to create three plots, such as:

``````p1 <- ggplot(df, aes(x=x, y=y)) +
+ geom_point() +
+ theme_minimal()

p2 <- ggplot(df, aes(x=z, y=y)) +
+ geom_point() +
+ theme_minimal()

p3 <- ggplot(df, aes(x=x, y=z)) +
+ geom_point() +
+ theme_minimal()
``````

... and arrange them in a way that the `x` and `y` axes align.
Is that something I can do using facets, and if so, how?

0 Likes

#2

Hi and welcome,

I've actually been playing with something along these lines myself recenlty (a customised pairs plot in my case), and I'd suggest using `GGally::ggmatrix` to create your array of plots:

``````x <- c(0.9, 1.1, 1.0)
y <- c(2.0, 1.9, 2.1)
z <- c(3.1, 2.9, 3.0)
df <- data.frame(x,y,z)

# Contains ggmatrix function...
library(GGally)

xy <- ggplot(df)+geom_point(aes(x=x,y=y))
zy <- ggplot(df)+geom_point(aes(x=z,y=y))
xz <- ggplot(df)+geom_point(aes(x=x,y=z))

mm <- ggmatrix(list(xy,zy,xz,NULL),nrow=2,ncol=2)
mm
``````

Created on 2019-04-04 by the reprex package (v0.2.1)

Ron.

1 Like

#3

If you wish to align different plots then there are packages like `cowplot`, `lemon` and `patchwork`.

An alternative to ron's solution would be to use ggplot2's facets:

``````library(tidyverse)

x <- c(0.9, 1.1, 1.0)
y <- c(2.0, 1.9, 2.1)
z <- c(3.1, 2.9, 3.0)

df <- data.frame(x,y,z)

df <- bind_rows(
df %>% select(axis1 = x, axis2 = y) %>% mutate(axes = "xy"),
df %>% select(axis1 = x, axis2 = z) %>% mutate(axes = "xz"),
df %>% select(axis1 = y, axis2 = z) %>% mutate(axes = "yz")
)

p <- ggplot(df, aes(x = axis1, y = axis2)) +
geom_point() +
facet_wrap(vars(axes), scales = "free")
``````

You can play with the scales argument to suit your requirements.

1 Like

#4

Building on Martin's answer you can arrange the facets this way

``````df <- df %>% mutate(axes = factor(axes, levels = c("xy", "yz", "xz")))

ggplot(df, aes(x = axis1, y = axis2)) +
geom_point() +
facet_wrap(vars(axes), scales = "free", ncol = 2)
``````

1 Like

#5

I ended up using the `multiplot` function described here:

http://www.cookbook-r.com/Graphs/Multiple_graphs_on_one_page_(ggplot2)/

... mainly for the reason that it allows to set a layout adjusting the relative size of the `xy` plot and the `z` axes of the other two plots.

See here for an example usage:

I noticed that this function (`multiplot`) apparently proliferated into a few different R packages, and wondered if there is a specific reason why it's not integrated centrally in one of the `ggplot` or `gg*` packages...

I also figured that another option to achieve this combined plot would be the `patchwork` package by @thomasp85:

... but I refrained from using it just now in a workflow I'm going to share, because it's not yet deployed to CRAN, and requires installation from GitHub. Are there any plans to put it on CRAN soon?

0 Likes

#6

If your question's been answered (even if by you), would you mind choosing a solution? (See FAQ below for how).

Having questions checked as resolved makes it a bit easier to navigate the site visually and see which threads still need help.

Thanks

2 Likes

#7

patchwork will hopefully get on CRAN sometimes this year...

1 Like

closed #8

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.

0 Likes