# R script for 3d PCOA plotting?

**URL:** <https://forum.mothur.org/t/r-script-for-3d-pcoa-plotting/2791>\
**Category:** Integrating mothur with other programs\
**Created:** [April 2, 2016, 1:38am UTC](https://forum.mothur.org/t/r-script-for-3d-pcoa-plotting/2791 "2016-04-02T01:38:59Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ch3coch3](https://avatars.discourse-cdn.com/v4/letter/c/c0e974/32.png) [@ch3coch3](https://forum.mothur.org/u/ch3coch3)\
**Post date:** [April 2, 2016, 1:38am UTC](https://forum.mothur.org/t/r-script-for-3d-pcoa-plotting/2791/1 "2016-04-02T01:38:59Z")

</div>

Hi all,

I got the pcoa results from mothur, named “xx.pcoa.axes”. I would like to plot the results as the 3d pcoa plot. Thanks to the script provided by dwaite, I was able to plot 2d pcoa. The code as below, in case it might be useful to someone else

```nohighlight
pcoa_data = read.table(file="10_horsepick.thetayc.0.03.lt.pcoa.axes", header=T, sep='\t')
design_file = read.table(file="10_horse.design", sep='\t',stringsAsFactors=FALSE)
uniq_groups = unique(design_file[,2])
num_uniq_groups = length(uniq_groups)

avail_cols = rainbow(num_uniq_groups)
avail_symbols = 1:num_uniq_groups

colour_map = list()
symbol_map = list()

for(i in 1:num_uniq_groups) {
  current_group = uniq_groups[i]
  colour_map[[current_group]] = avail_cols[i]
  symbol_map[[current_group]] = avail_symbols[i]
}

col_vector = rep(NA,nrow(design_file))
symbol_vector = rep(NA,nrow(design_file))

for(i in 1:nrow(design_file)) {
  group = design_file[i,2]
  col_vector[i] = colour_map[[group]]
  symbol_vector [i] = symbol_map [[group]]   
}

plot(pcoa_data$axis2~pcoa_data$axis1,xlab='Axis 1',ylab='Axis 2',main='PCoA',col='black', pch=symbol_vector)
legend(X,Y,uniq_groups,avail_cols)

```

It would be nice to assign different colors to each group as well.

Thanks many!!

---

<div class="post-metadata">

**Author:** ![dwaite](https://avatars.discourse-cdn.com/v4/letter/d/ecccb3/32.png) [@dwaite](https://forum.mothur.org/u/dwaite)\
**Post date:** [April 3, 2016, 8:47pm UTC](https://forum.mothur.org/t/r-script-for-3d-pcoa-plotting/2791/2 "2016-04-03T20:47:42Z")

</div>

For 3D plotting I’m quite a fan of the rgl package in R. It’s very simple to get started with, basically just:

```nohighlight
library(rgl)
plot3d(x=..., y=..., z=...)

```

As well as the typical static image exports, you can also export your graphs into an HTML file using the writeWebGL function which creates a graph that anyone can open in their web browser and rotate/zoom as required.

For what it’s worth, although I wrote that script a long time ago I’m not actually a big fan of it. I usually find it much better to manually choose and apply colours to the plot rather than rely on the automatically generated ones.
