Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Displaying ggplot2 graphs from R in Jupyter

When I create a plot in Jupyter using the ggplot2 R package, I get a link to the chart that says "View PDF" instead of the chart being presented inline.

I know that traditionally in IPython Notebook you were able to show the charts inline using the %matplotlib magic function. Does Jupyter have something similar for R and ggplot2?

What do I need to do to show the graph inline versus as a link to a PDF?

like image 790
Ryan Wade Avatar asked Jun 18 '15 16:06

Ryan Wade


People also ask

How do I view plots in Jupyter?

The inline option with the %matplotlib magic function renders the plot out cell even if show() function of plot object is not called. The show() function causes the figure to be displayed below in[] cell without out[] with number. Now, add plt. show() at the end and run the cell again to see the difference.

Can you use ggplot2 in Python?

plotnine is based on ggplot2 from the R programming language, so if you have a background in R, then you can consider plotnine as the equivalent of ggplot2 in Python. In this tutorial, you'll learn how to: Install plotnine and Jupyter Notebook. Combine the different elements of the grammar of graphics.

Can you use R in Jupyter Notebook?

Using R in a Jupyter NotebookTo run a Jupyter Notebook with R, you need to create a conda environment and activate the kernel so Jupyter can recognize it. Then you can work with the R language in a notebook.


2 Answers

You can show the graphs inline with this option.

options(jupyter.plot_mimetypes = 'image/png')

You can also produce pdf files as you would regularly in R, e.g.

pdf("test.pdf")
ggplot(data.frame(a=rnorm(100,1,10)),aes(a))+geom_histogram()
dev.off()
like image 147
SlowLearner Avatar answered Oct 16 '22 08:10

SlowLearner


There are currently some issues with producing ggplot2 outputs using the IRkermel. There is an open issue on GitHub about it. The only workaround at the moment appears to be using PDF, jpg, png outputs for your plots.

like image 40
scribbles Avatar answered Oct 16 '22 08:10

scribbles