Package: ggshow
ggshowProduce ggplot2 graphs from PythonInstall# from pypipipinstallggshow# or from githubgitclone--depth1https://github.com/kota7/ggshow.git
pipinstall-U./ggshowRequirementsPython 3.5+Rscriptcommand and the R environment withggplot2package installedFunctionalitiesggshow: Draw graphs using ggplot2 on R and return the Image object that you can show on Jupyter notebookggwrite: Draw graphs using ggplot2 on R and save it to a file.%gg,%%gg: ipython magic for drawing ggplot2 graph.fromggshowimportggshow,ggwrite# Minimal example# pass R code to produce ggplot graph to ggshow function# note: ggplot2 is automatically importedggshow("""x <- c(1,2,3)y <- c(4,5,6)qplot(x, y, geom="line")""",savesize=(3,2))# Example with data frames# pass pandas data frames as keyword arguments# then you can use them in the R codeimportnumpyasnpimportpandasaspdx=np.linspace(-20,20,500)y=np.sin(x)y2=0.4*np.cos(x)df1=pd.DataFrame({"x":x,"y":y})df2=pd.DataFrame({"x":x,"y":y2})ggshow("""ggplot(a, aes(x, y)) +geom_line(color="blue") +geom_line(data=b, linetype="dashed", color="red") +theme_bw()""",dispwidth=500,savesize=(4,2),a=df1,b=df2)# Example to save the graph to a fileggwrite("""x <- c(1,2,3)y <- c(4,5,6)qplot(x, y, geom="line")""","foo.jpg",savesize=(4,2))fromIPython.displayimportImageImage("foo.jpg",width=400)# Example to specify the image file format# But there may not be significant differences# Default: pngggshow("""x <- c(1,2,3)y <- c(4,5,6)qplot(x, y, geom="line") + ggtitle("PNG")""",savesize=(3,2),imageformat="png")ggshow("""x <- c(1,2,3)y <- c(4,5,6)qplot(x, y, geom="line") + ggtitle("JPEG")""",savesize=(3,2),imageformat="jpeg")ggshow("""x <- c(1,2,3)y <- c(4,5,6)qplot(x, y, geom="line") + ggtitle("SVG")""",savesize=(3,2),imageformat="svg")None# Example using %gg magic%load_extggshow## line magic%ggqplot(1,2)-s43--dispwidth300## line magic with the code as a string variablex=np.linspace(-20,20,500)y=np.sin(x)y2=0.4*np.cos(x)df1=pd.DataFrame({"x":x,"y":y})df2=pd.DataFrame({"x":x,"y":y2})code="""ggplot(a, aes(x, y)) +geom_line(color="blue") +geom_line(data=b, linetype="dashed", color="red") +theme_bw() +ggtitle("Sine and Cosine Waves")"""%gg{code}-s42--dispwidth400--dataa=df1b=df2%%gg-s42--dispwidth400--dataa=df1b=df2## cell magicggplot(a,aes(x,y))+geom_line(color="blue")+geom_line(data=b,linetype="dashed",color="red")+theme_bw()+ggtitle("Sine and Cosine Waves")# show the full command options%gg--helpHelp on method gg in module ggshow.ggshow:

gg(line, cell=None) method of ggshow.ggshow.GGMagic instance
    ::

      %gg [--help] [-s SAVESIZE SAVESIZE] [--scale SCALE] [--units UNITS]
              [--dpi DPI] [--message_encoding MESSAGE_ENCODING]
              [--rscriptcommand RSCRIPTCOMMAND] [-w DISPWIDTH] [-h DISPHEIGHT]
              [--imageformat {png,jpeg,svg}] [--libs [LIBS [LIBS ...]]]
              [--data [DATA [DATA ...]]]
              [plotcode [plotcode ...]]

    positional arguments:
      plotcode              R code

    optional arguments:
      --help
      -s <SAVESIZE SAVESIZE>, --savesize <SAVESIZE SAVESIZE>
                            height, width
      --scale SCALE         ggsave option scale
      --units UNITS         ggsave option units
      --dpi DPI             ggsave option dpi
      --message_encoding MESSAGE_ENCODING
                            Encoding of the subprocess outputs
      --rscriptcommand RSCRIPTCOMMAND
                            Rscript command name
      -w DISPWIDTH, --dispwidth DISPWIDTH
                            display width
      -h DISPHEIGHT, --dispheight DISPHEIGHT
                            display width
      --imageformat <{png,jpeg,svg}>
                            imagefile format
      --libs <[LIBS [LIBS ...]]>
                            R libraries to use
      --data <[DATA [DATA ...]]>
                            data frames mapping as {name in r}={name in python}# By default, we use 'Rscript' as the command to run R code.# If this is not a valid command on the environment,# we can specify the command by `rscriptcommand` optionggshow("""x <- c(1,2,3)y <- c(4,5,6)qplot(x, y, geom="line")""",savesize=(3,2),rscriptcommand="/usr/bin/Rscript")# Or set the new command path as the default using set_rscript function.fromggshowimportconfig,set_rscriptset_rscript("/usr/bin/Rscript")print(config.rscript)# this is just the full path of the command on this environment# so the command will work in the same way/usr/bin/Rscript