# Create plots in R based on the hail metrics

**URL:** <https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613>\
**Category:** Hail Query & hailctl\
**Created:** [September 1, 2020, 4:25pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613 "2020-09-01T16:25:22Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Maria](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Maria](https://discuss.hail.is/u/Maria)\
**Post date:** [September 1, 2020, 4:25pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/1 "2020-09-01T16:25:22Z")

</div>

Hi there!

I’m new to hail, and I was hoping you could help me with the following:

I have installed hail to the cluster, but I cannot create/view plots there. Ideally, I would like to create plots for all the metrics that come up when I use the mt.summarize() command. How can I save this output, so that I can then read it in R?

Thank you!  
Maria

---

<div class="post-metadata">

**Author:** ![kumarveerapen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/kumarveerapen/32/355_2.png) [@kumarveerapen](https://discuss.hail.is/u/kumarveerapen)\
**Post date:** [September 1, 2020, 5:21pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/2 "2020-09-01T17:21:51Z")

</div>

Hi Maria

Thanks for reaching out to us!

If you would like to visualize QC metrics like call rates etc, I would highly recommend using the [Hail plotting tools](https://hail.is/docs/0.2/tutorials/08-plotting.html#Plotting-Tutorial)  
You can either use the save button on the interactive notebook or use [this](https://stackoverflow.com/questions/24060173/with-bokeh-how-to-save-to-a-png-or-jpg-instead-of-a-html-file) discussion to guide you to it .

It would be more cost effective then to summarize what you will need in a smaller dataset/table, export it as a [tsv](https://hail.is/docs/0.2/hail.Table.html#hail.Table.export), and import it into your R environment for further plotting with the admittedly amazing `ggplot2`

---

<div class="post-metadata">

**Author:** ![Maria](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Maria](https://discuss.hail.is/u/Maria)\
**Post date:** [September 1, 2020, 9:17pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/3 "2020-09-01T21:17:49Z")

</div>

Hi Kumar,

Thank you very much for your prompt response. This is very helpful. I don’t have the jupiter notebook installed, instead I use ipython. I followed the tutorial and used these instructions:  
from [bokeh.io](http://bokeh.io) import show, output\_notebook  
from bokeh.layouts import gridplot  
output\_notebook()

And then it looks like I created the plots, but I can’t find a way to save them. I looked at the discussion link you provided, but it seems that I need to install dependencies (selenium etc). Hail has been installed as a singularity image at the cluster, so I have now asked our technician to see if he can add these to the image. In the meantime, is there another way to save the plots? Also, you mentioned that I could summarize what I will need in a smaller dataset/table and export it. Could you please provide me with some example commands of how I could do this?  
Thank you very much!

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [September 2, 2020, 3:40pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/4 "2020-09-02T15:40:43Z")

</div>

Hey @Maria!

Sorry you’re having trouble with Hail’s plotting libraries.

You’re right, there’s no easy way to save plots generated using Hail’s plotting libraries. My apologies. This is a significant known problem for us. I recommend not using Hail’s plotting libraries if you need to save the plots for later use.

Kumar is suggesting something like this:

```auto
import hail as hl

mt = hl.balding_nichols_model(3, 10, 10)
mt = hl.sample_qc(mt, name='sample_qc')
mt.cols().select('sample_qc').export('output/table1.tsv.bgz')

```

---

<div class="post-metadata">

**Author:** ![Maria](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Maria](https://discuss.hail.is/u/Maria)\
**Post date:** [September 2, 2020, 4:41pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/5 "2020-09-02T16:41:10Z")

</div>

Hi @danking!

No problem at all. Thank you clarifying! I used the commands you suggested and the table is saved in the cluster now! woohoo!  
Do you know how I could also get the variant\_qc metrics in a table?

I have one more question. I read the file in R using this command:  
t1\<-read.table(gzfile("/Volumes/output/table1.tsv.bgz"))  
And now I have a file with two columns, one has the sample ID and the other one has the sample\_qc statistics per sample ID in the following format:  
{“dp\_stats”:{“mean”:35.87442442237584,“stdev”:22.4342,“min”:0.0, etc

Do you know to ‘destring’ the sample\_qc column, so that I can then create the plots?

Thank you very much for your help!  
Maria

---

<div class="post-metadata">

**Author:** ![kumarveerapen](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/kumarveerapen/32/355_2.png) [@kumarveerapen](https://discuss.hail.is/u/kumarveerapen)\
**Post date:** [September 2, 2020, 4:47pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/6 "2020-09-02T16:47:33Z")

</div>

For variant QC, similarly to the sample QC, you would use `variant_qc` [here](https://hail.is/docs/0.2/methods/genetics.html#hail.methods.variant_qc)

To split strings, I tend to use the function `R:strsplit` where usage instructions are found [here](http://rfunction.com/archives/1499#:~:text=Split%20a%20character%20string%20or,x%20that%20has%20been%20split.&text=If%20the%20split%20is%20an,is%20split%20between%20every%20character.)

---

<div class="post-metadata">

**Author:** ![danking](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/danking/32/43_2.png) [@danking](https://discuss.hail.is/u/danking)\
**Post date:** [September 2, 2020, 4:51pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/7 "2020-09-02T16:51:31Z")

</div>

Add a `.flatten()` before the `.export(...)` and you’ll get many columns.

---

<div class="post-metadata">

**Author:** ![Maria](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Maria](https://discuss.hail.is/u/Maria)\
**Post date:** [September 2, 2020, 7:22pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/8 "2020-09-02T19:22:18Z")

</div>

Thank you @danking and @kumarveerapen!

One last question, re the variant\_qc. I use these commands as in the sample\_qc above:  
mt=hl.read\_matrix\_table(’/hail/chr22.mt’)  
mt = hl.variant\_qc(mt, name=‘variant\_qc’)  
mt.cols().select(‘variant\_qc’).export(‘output/tablevc.tsv.bgz’)

But I get an error at the last command: LookupError: Table instance has no field ‘variant\_qc’

Do you maybe know why I get this error?

Thanks again, I really appreciate your help and prompt responses.  
Maria

---

<div class="post-metadata">

**Author:** ![tpoterba](https://yyz2.discourse-cdn.com/flex036/user_avatar/discuss.hail.is/tpoterba/32/61_2.png) [@tpoterba](https://discuss.hail.is/u/tpoterba)\
**Post date:** [September 2, 2020, 7:23pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/9 "2020-09-02T19:23:41Z")

</div>

you’ll want `.rows()` for variant\_qc and `.cols()` for sample\_qc. The variants are the row axis in Hail, samples are the column axis.

---

<div class="post-metadata">

**Author:** ![Maria](https://avatars.discourse-cdn.com/v4/letter/m/34f0e0/32.png) [@Maria](https://discuss.hail.is/u/Maria)\
**Post date:** [September 2, 2020, 8:42pm UTC](https://discuss.hail.is/t/create-plots-in-r-based-on-the-hail-metrics/1613/10 "2020-09-02T20:42:48Z")

</div>

Thank you!!!
