Help:Drawing graphs: Difference between revisions

From Opasnet
Jump to navigation Jump to search
Line 230: Line 230:


There is no established approach to Sankey diagrams. A few packages provide with functionalities, but the usebility and user-friendliness has not been tested.
There is no established approach to Sankey diagrams. A few packages provide with functionalities, but the usebility and user-friendliness has not been tested.
* [http://tagteam.harvard.edu/hub_feeds/1981/feed_items/227688 Harvard tagteam: rCharts] [http://www.r-bloggers.com/generating-sankey-diagrams-from-rcharts/ R bloggers]
* [https://cran.r-project.org/web/packages/riverplot/riverplot.pdf First choice: riverplot package]
* [http://sankeymatic.com/build/ Sankeymatic for creating simple Sankey diagrams online] (not R)
* [http://tagteam.harvard.edu/hub_feeds/1981/feed_items/227688 Harvard tagteam: rCharts] [http://www.r-bloggers.com/generating-sankey-diagrams-from-rcharts/ R bloggers] [http://www.r-bloggers.com/reshaping-horse-importexport-data-to-fit-a-sankey-diagram/ Horse import/export: d3.js plugin]
** [http://timelyportfolio.github.io/rCharts_d3_sankey/example_build_network_sankey.html Sankey from scratch using rCharts]
** [http://timelyportfolio.github.io/rCharts_d3_sankey/example_build_network_sankey.html Sankey from scratch using rCharts]
* [https://gist.github.com/aaronberdanier/1423501 Aaronberdanier: SankeyR function]
* [https://gist.github.com/aaronberdanier/1423501 Aaronberdanier: SankeyR function]
Line 236: Line 238:
* [http://stackoverflow.com/questions/9968433/sankey-diagrams-in-r Riverplot (and SankeyR?)]
* [http://stackoverflow.com/questions/9968433/sankey-diagrams-in-r Riverplot (and SankeyR?)]
* [http://www.sankey-diagrams.com/ General Sankey diagram website]
* [http://www.sankey-diagrams.com/ General Sankey diagram website]
* [http://ramnathv.github.io/rCharts/ rCharts] interactive charts for R; does not work for Opasnet?
* [http://timelyportfolio.github.io/rCharts_d3_sankey/example_build_network_sankey.html Example Sankeys with rCharts, d3.js and igraph]
=== Directed acyclic graphs DAGs ===
* [http://igraph.org/r/doc/igraph.pdf Package igraph] [http://igraph.org/r/doc/] First choice?
* [https://cran.r-project.org/web/packages/dagR/dagR.pdf Package dagR] Easy but does not organise the dag, must be done manually?
* [https://cran.r-project.org/web/packages/gRbase/vignettes/gRbase-graphs.pdf Package gRbase] (probably only works for small graphs?
* [http://www.dagitty.net/ Dagitty for online dags] but must be done by hand
* [https://rulesofreason.wordpress.com/2012/11/05/network-visualization-in-r-with-the-igraph-package/ Network visualization in R with the igraph package]


==See also==
==See also==

Revision as of 14:18, 18 September 2015


Question

How to draw graphs in Opasnet?

Answer

R-tools

In R-tools, you have the functionalities of R available. We recommend that you use the package ggplot2 whenever possible. It is very powerful, and borrowing good ideas from others is easier if we all use the same approach. Of course, it is also possible to use plot' (a kind of basic graph) as well, but the limits come sooner. This is an example code that contains all kinds of examples with comments.

+ Show code

rlnorm

Graph for cumulative probability distributions

Size of base font:

+ Show code

Colours and ordering of bars

+ Show code

Google charts

This is how you can make fancy Google motion or map charts. See documentation for R package googleVis and Google's help. Note that Google has copyright in its maps, but the license to use them is very flexible and in practice free [1].

+ Show code

Export a graph to EPS or PDF file

This code only works on your own computer, because you cannot save files when running code in Opasnet. [2]

# Saving an .eps file
setEPS()
postscript("whatever.eps")
plot(rnorm(100), main="Hey Some Data")
dev.off()

# Saving a .pdf file
pdf("whatever.pdf")
plot(rnorm(100), main="Hey Some Data")
dev.off()

If you are using ggplot2 to generate a figure, then a

ggsave(file="name.eps", width = 7, height = 7) 

will also work. It will save the last ggplot with the width and height you give (in inches).

Cumulative graphs

With ggplot, stat_ecdf() gives an empirical cumulative distribution function that sums up to 1. But if you want to get a cumulative sum of counts (that sum up to the number of observations), you need to do something else. For example, see [3].

ggplot(x,aes(x=X,color=A)) +
  stat_bin(data=subset(x,A=="a"),aes(y=cumsum(..count..)),geom="step")+
  stat_bin(data=subset(x,A=="b"),aes(y=cumsum(..count..)),geom="step")+
  stat_bin(data=subset(x,A=="c"),aes(y=cumsum(..count..)),geom="step")

Using positions

ggplot2 Quick Reference: position[4]

Position adjustments are used to adjust the position of each geom. The following position adjustments are available:

  • position_identity - default of most geoms
  • position_jitter - default of geom_jitter
  • position_dodge - default of geom_boxplot
  • position_stack - default of geom_bar==geom_histogram and geom_area
  • position_fill - useful for geom_bar==geom_histogram and geom_area

Setting the Position Adjustment: To set the position adjustment of a geom, use the position parameter of the layer() function:

layer(geom="point", ..., position="jitter")

Or use the position parameter of the geom_...() function:

geom_point(..., position="jitter")

Double dots in ggplot

What are double dots eg. ..density.. in ggplot?[5]

Unlike many other languages, in R, the dot is perfectly valid in identifiers. In this case, ..count.. is an identifier. However, there is special code in ggplot2 to detect this pattern, and to strip the dots. It feels unlikely that real code would use identifiers formatted like that, and so this is a neat way to distinguish between defined and calculated aesthetics.

It is used further up above in the map_statistic function. If a calculated aesthetic is present, another data frame (one that contains e.g. the count column) is used for the plot.

The single dot . is just another identifier, defined in the plyr package. As you can see, it is a function.

Maps and GIS-based data

There are several methods to produce maps. These are described on Opasnet map.

GoogleDocs

GoogleDocs is the method of choice for drawing causal diagrams.

  • Make a drawing.
  • Share it with everyone with open editing.
  • Download is in png or svg format.
  • Upload the file to Opasnet and copy a link to the original Google document to the image page.
  • Use like any image.

Sankey diagrams

There is no established approach to Sankey diagrams. A few packages provide with functionalities, but the usebility and user-friendliness has not been tested.

Directed acyclic graphs DAGs

See also