Help:Drawing graphs: Difference between revisions
(→plotly) |
|||
(11 intermediate revisions by 2 users not shown) | |||
Line 57: | Line 57: | ||
</rcode> | </rcode> | ||
These are the equal sizes for different graphics settings. A typically good base_size is 24: | |||
* Opasnet graphics | |||
* png(width = 1024, height=768) # (in pixels) | |||
* pdf(width = 14, height=10.5) # (in inches) | |||
===plotly=== | |||
Export [https://plot.ly/r/ plotly graphs] to a file (but you may want to use orca function instead): | |||
p %>% | |||
export(file = "filename.svg", | |||
selenium = RSelenium::rsDriver(browser = "chrome")) | |||
=== chloropleth === | |||
[https://docs.mapbox.com/studio-manual/examples/choropleth-map/ Mapbox]: chloropleth maps | |||
===rlnorm=== | ===rlnorm=== | ||
Line 105: | Line 123: | ||
* [http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/ Cookbook for colours] | * [http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/ Cookbook for colours] | ||
* [http://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette Default colour palette] | * [http://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette Default colour palette] | ||
* [http://www.stat.columbia.edu/~tzheng/files/Rcolor.pdf Eight pages of names for colors in R] | |||
<rcode graphics=1 embed=1> | <rcode graphics=1 embed=1> | ||
Line 247: | Line 266: | ||
* [http://igraph.org/r/doc/igraph.pdf Package igraph] [http://igraph.org/r/doc/] First choice for dags. | * [http://igraph.org/r/doc/igraph.pdf Package igraph] [http://igraph.org/r/doc/] First choice for dags. | ||
** [http://michael.hahsler.net/SMU/LearnROnYourOwn/code/igraph.html Examples] [http://stackoverflow.com/questions/13961913/which-layout-should-i-use-to-get-non-overlapping-edges-in-igraph examples of non-overlapping edges] [http://www.inside-r.org/packages/cran/igraph/docs/igraph.plotting igraph parameter guide] | ** [http://michael.hahsler.net/SMU/LearnROnYourOwn/code/igraph.html Examples] [http://stackoverflow.com/questions/13961913/which-layout-should-i-use-to-get-non-overlapping-edges-in-igraph examples of non-overlapping edges] [http://www.inside-r.org/packages/cran/igraph/docs/igraph.plotting igraph parameter guide] | ||
* [https://cran.r-project.org/web/packages/dagR/dagR.pdf Package dagR] Easy but does not organise the dag, must be done manually? | ** Other possibilities (no experience about these): | ||
* [https://cran.r-project.org/web/packages/gRbase/vignettes/gRbase-graphs.pdf Package gRbase] (probably only works for small graphs? | *** [https://cran.r-project.org/web/packages/dagR/dagR.pdf Package dagR] Easy but does not organise the dag, must be done manually? | ||
* [http://www.dagitty.net/ Dagitty for online dags] but must be done by hand | *** [https://cran.r-project.org/web/packages/gRbase/vignettes/gRbase-graphs.pdf Package gRbase] (probably only works for small graphs? | ||
* [https://rulesofreason.wordpress.com/2012/11/05/network-visualization-in-r-with-the-igraph-package/ Network visualization in R with the igraph package] | *** [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] | |||
Opasnet server does not plot the default ''serif'' family font with igraphs. Therefore, you must give: 'vertex.label.family = "Helvetica"' to prevent an error. | |||
An example code where jygraph is an igraph object. See details from [[:op_fi:Keskipitkän aikavälin ilmastopolitiikan suunnitelma]]. | |||
<pre> | |||
plot(jygraph, | |||
vertex.label.cex = 0.8, | |||
vertex.size = ifelse(grepl("uuttuja", V(jygraph)$Tyyppi), 20, 10), | |||
vertex.color = ifelse(grepl("iistelty", V(jygraph)$Huom), "Red", "SkyBlue2"), | |||
vertex.shape = ifelse(grepl("äätös", V(jygraph)$Tyyppi), "square", "circle"), | |||
vertex.label.family = "Helvetica", | |||
edge.color = edgeparam$Color[match(E(jygraph)$Relaatio, edgeparam$Relaatio)], | |||
edge.width = edgeparam$Width[match(E(jygraph)$Relaatio, edgeparam$Relaatio)], | |||
edge.arrow.size = 0.5, | |||
layout = layout.fruchterman.reingold | |||
) | |||
</pre> | |||
Note: if parameter values are factors, ifelse converts them by using as.numeric(), not as.character(). Therefore, it is better to not use factors at all but explicitly convert them in the code. | |||
'''Other options to show DAGs and RDF data | |||
* TopBraid Composer: expensive proprietary software and therefore not applicable | |||
* Protégé: open source ontology system. [http://protege.stanford.edu/] It is RDF compatible but does not seem very visual. | |||
* VisualDataWeb [http://www.visualdataweb.org/tools.php] It is RDF compatible and looks really fancy. Open source. {{defend|# |Must learn more about this.|--[[User:Jouni|Jouni]] ([[User talk:Jouni|talk]]) 08:25, 28 October 2016 (UTC)}} | |||
* [[:en:Tulip (software)]] is information visualisation framework for relational data. It is open source. It is efficient for development of end-user applications. Written in C++. | |||
* [[:en:NetworkX]] is a Python library for studying graphs and networks. Open source. Suitable for graphs in excess of 10 million nodes and 100 million edges. | |||
* [[:en:Gephi]] is an open-source network analysis and visualisation software package written in Java. | |||
* [[:en:Graphviz]] open source tool package from AT&T for drawing graphs specified in DOT language. | |||
* R packages igraph, network, sna, and ndtv can be used. For instuctions, see [https://rpubs.com/kateto/netviz]. | |||
* [http://www.phil.cmu.edu/projects/tetrad/ Tetrad project] | |||
*[http://dagitty.net/dags.html Dagitty]. [http://dagitty.net] | |||
=== Using Unicode symbols in graphs === | |||
You can create infograms with symbols by replacing typical shapes with Unicode characters. However, ggplot does not seem to accept characters beyond ca. 64000 where the most interesting pictograms are. I tried [https://github.com/yixuan/showtext showtext] for [https://insileco.github.io/2017/05/23/add-icons-on-your-r-plot/ adding new fonts]. An example for [https://github.com/yixuan/showtext/issues/6 using showtext with Shiny], [https://cran.rstudio.com/web/packages/showtext/vignettes/introduction.html using Google fonts]. | |||
NOTE! Some of the code worked in Windows, some in Linux. An optimal solution has not been found yet. Probably the Linux cluster is the best way to produce graphs with strange Unicode characters and fonts. | |||
# 9728+256: erilaisia symboleita ml perushymiö ja käsiä [http://www.fileformat.info/info/unicode/block/miscellaneous_symbols_and_pictographs/images.htm?start=128389] | |||
#1F3C2... urheilijoita kaavamaisia | |||
#128697+1 miesten ja naisten vessa | |||
#Transport and map symbols 1F680 sisältää myös kävelijöitä ym. [https://www.fileformat.info/info/unicode/block/transport_and_map_symbols/images.htm] | |||
<pre> | |||
### Add fonts to your system | |||
# https://insileco.github.io/2017/05/23/add-icons-on-your-r-plot/ | |||
dir.create("assets", showWarnings = FALSE) | |||
##-- URLs | |||
urls <- c( | |||
'https://github.com/jpswalsh/academicons/raw/master/fonts/academicons.ttf', | |||
'https://github.com/inSileco/inSileco.github.io/raw/dev/static/fonts/fontawesome-webfont.ttf', | |||
'https://github.com/ionic-team/ionicons/blob/master/docs/fonts/ionicons.ttf?raw=true' | |||
) | |||
##-- download the fonts. For some reason this did not work, so I downloaded font files manually. | |||
for (i in 1:3){ | |||
download.file(urls[i], destfile=paste0("assets/", basename(urls[i]))) | |||
} | |||
font_paths("assets") | |||
font_add(family = 'academicons', regular = 'assets/academicons.ttf') | |||
font_add(family = 'FontAwesome', regular = 'fontawesome-webfont.ttf') | |||
font_add(family = 'ionicons', regular = 'ionicons.ttf') | |||
##-- check the font families available | |||
font_families() | |||
windowsFonts() | |||
windowsFonts(ionic = "ionicons", awesome = "FontAwesome", acade="academicons") | |||
cols <- c("#3fb3b2", "#8555b4", "#ffdd55", "#1b95e0") | |||
val <- 61970:62046 | |||
ias <- sapply(val, FUN=intToUtf8) | |||
coord <- expand.grid(rev(1:11), 1:7) | |||
## | |||
par(mar=c(2,2,2,2), family = "ionic") | |||
plot(coord[,1], coord[,2], ann=FALSE, axes=FALSE, pch=ias, cex=5, col=cols) | |||
ggplot(data.frame(A=rep(1:10,10),B=rep(1:10,each=10),C=1:100), aes(x=A,y=B,shape=factor(C)))+ | |||
geom_point(size=10, family="ionic")+ | |||
scale_shape_manual(values=intToUtf8(61970:62969, multiple=TRUE))+ | |||
theme(legend.position = "none") | |||
start <- 61970 | |||
dat <- data.frame( | |||
A=rep(1:10,10), | |||
B=rep(1:10,each=10), | |||
C=intToUtf8(start:(start+99), multiple=TRUE) | |||
) | |||
ggplot(dat, aes(x=A,y=B,label=C))+ | |||
geom_text(size=10)#, family="serif") | |||
library(showtext) | |||
font_add(family = 'academicons', regular = 'academicons.ttf') | |||
font_add(family = 'FontAwesome', regular = 'fontawesome-webfont.ttf') | |||
font_add(family = 'ionicons', regular = 'ionicons.ttf') | |||
font_families() | |||
showtext_begin() | |||
strt <- 61440 | |||
dat <- data.frame( | |||
A=rep(1:10,10), | |||
B=rep(1:10, each=10), | |||
C=intToUtf8(strt:(strt+99),multiple=TRUE) | |||
) | |||
ggplot(dat,aes(x=A,y=B,label="\uf006"))+ | |||
geom_text(size=10, family="FontAwesome") | |||
ggplot(data.frame(A=rep(1:10,10),B=rep(1:10,each=10),C=1:100), aes(x=A,y=B,shape=factor(C)))+ | |||
geom_point(size=10)+ | |||
scale_shape_manual(values=intToUtf8(9828:9927, multiple=TRUE))+ | |||
theme(legend.position = "none") | |||
</pre> | |||
=== Malmquist Productivity Growth Index === | === Malmquist Productivity Growth Index === | ||
Linear Programming for the Malmquist Productivity Growth Index [http://ekqvist.goeuropeinfo.com/rbloggerqvist/?p=199] is a method to make uncertain prediction into the future based on existing trend data. | Linear Programming for the Malmquist Productivity Growth Index [http://ekqvist.goeuropeinfo.com/rbloggerqvist/?p=199] is a method to make uncertain prediction into the future based on existing trend data. | ||
=== Html widgets === | |||
HTML widgets: Bring the best of JavaScript data visualization to R [http://www.htmlwidgets.org/] | |||
==See also== | ==See also== |
Latest revision as of 17:35, 3 July 2019
Moderator:Jouni (see all) |
|
Upload data
|
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.
These are the equal sizes for different graphics settings. A typically good base_size is 24:
- Opasnet graphics
- png(width = 1024, height=768) # (in pixels)
- pdf(width = 14, height=10.5) # (in inches)
plotly
Export plotly graphs to a file (but you may want to use orca function instead):
p %>% export(file = "filename.svg", selenium = RSelenium::rsDriver(browser = "chrome"))
chloropleth
Mapbox: chloropleth maps
rlnorm
- Graph for cumulative probability distributions
Colours and ordering of bars
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 [3].
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. [4]
# 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 [5].
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[6]
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?[7]
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.
- First choice: riverplot package
- Sankeymatic for creating simple Sankey diagrams online (not R)
- Harvard tagteam: rCharts R bloggers Horse import/export: d3.js plugin
- Aaronberdanier: SankeyR function
- Riverplot (and SankeyR?)
- General Sankey diagram website
- rCharts interactive charts for R; does not work for Opasnet?
- Example Sankeys with rCharts, d3.js and igraph
Directed acyclic graphs DAGs
- Package igraph [8] First choice for dags.
- Examples examples of non-overlapping edges igraph parameter guide
- Other possibilities (no experience about these):
- Package dagR Easy but does not organise the dag, must be done manually?
- Package gRbase (probably only works for small graphs?
- Dagitty for online dags but must be done by hand
- Network visualization in R with the igraph package
Opasnet server does not plot the default serif family font with igraphs. Therefore, you must give: 'vertex.label.family = "Helvetica"' to prevent an error.
An example code where jygraph is an igraph object. See details from op_fi:Keskipitkän aikavälin ilmastopolitiikan suunnitelma.
plot(jygraph, vertex.label.cex = 0.8, vertex.size = ifelse(grepl("uuttuja", V(jygraph)$Tyyppi), 20, 10), vertex.color = ifelse(grepl("iistelty", V(jygraph)$Huom), "Red", "SkyBlue2"), vertex.shape = ifelse(grepl("äätös", V(jygraph)$Tyyppi), "square", "circle"), vertex.label.family = "Helvetica", edge.color = edgeparam$Color[match(E(jygraph)$Relaatio, edgeparam$Relaatio)], edge.width = edgeparam$Width[match(E(jygraph)$Relaatio, edgeparam$Relaatio)], edge.arrow.size = 0.5, layout = layout.fruchterman.reingold )
Note: if parameter values are factors, ifelse converts them by using as.numeric(), not as.character(). Therefore, it is better to not use factors at all but explicitly convert them in the code.
Other options to show DAGs and RDF data
- TopBraid Composer: expensive proprietary software and therefore not applicable
- Protégé: open source ontology system. [9] It is RDF compatible but does not seem very visual.
- VisualDataWeb [10] It is RDF compatible and looks really fancy. Open source. ←--#: . Must learn more about this. --Jouni (talk) 08:25, 28 October 2016 (UTC) (type: truth; paradigms: science: defence)
- en:Tulip (software) is information visualisation framework for relational data. It is open source. It is efficient for development of end-user applications. Written in C++.
- en:NetworkX is a Python library for studying graphs and networks. Open source. Suitable for graphs in excess of 10 million nodes and 100 million edges.
- en:Gephi is an open-source network analysis and visualisation software package written in Java.
- en:Graphviz open source tool package from AT&T for drawing graphs specified in DOT language.
- R packages igraph, network, sna, and ndtv can be used. For instuctions, see [11].
- Tetrad project
- Dagitty. [12]
Using Unicode symbols in graphs
You can create infograms with symbols by replacing typical shapes with Unicode characters. However, ggplot does not seem to accept characters beyond ca. 64000 where the most interesting pictograms are. I tried showtext for adding new fonts. An example for using showtext with Shiny, using Google fonts.
NOTE! Some of the code worked in Windows, some in Linux. An optimal solution has not been found yet. Probably the Linux cluster is the best way to produce graphs with strange Unicode characters and fonts.
- 9728+256: erilaisia symboleita ml perushymiö ja käsiä [13]
- 1F3C2... urheilijoita kaavamaisia
- 128697+1 miesten ja naisten vessa
- Transport and map symbols 1F680 sisältää myös kävelijöitä ym. [14]
### Add fonts to your system # https://insileco.github.io/2017/05/23/add-icons-on-your-r-plot/ dir.create("assets", showWarnings = FALSE) ##-- URLs urls <- c( 'https://github.com/jpswalsh/academicons/raw/master/fonts/academicons.ttf', 'https://github.com/inSileco/inSileco.github.io/raw/dev/static/fonts/fontawesome-webfont.ttf', 'https://github.com/ionic-team/ionicons/blob/master/docs/fonts/ionicons.ttf?raw=true' ) ##-- download the fonts. For some reason this did not work, so I downloaded font files manually. for (i in 1:3){ download.file(urls[i], destfile=paste0("assets/", basename(urls[i]))) } font_paths("assets") font_add(family = 'academicons', regular = 'assets/academicons.ttf') font_add(family = 'FontAwesome', regular = 'fontawesome-webfont.ttf') font_add(family = 'ionicons', regular = 'ionicons.ttf') ##-- check the font families available font_families() windowsFonts() windowsFonts(ionic = "ionicons", awesome = "FontAwesome", acade="academicons") cols <- c("#3fb3b2", "#8555b4", "#ffdd55", "#1b95e0") val <- 61970:62046 ias <- sapply(val, FUN=intToUtf8) coord <- expand.grid(rev(1:11), 1:7) ## par(mar=c(2,2,2,2), family = "ionic") plot(coord[,1], coord[,2], ann=FALSE, axes=FALSE, pch=ias, cex=5, col=cols) ggplot(data.frame(A=rep(1:10,10),B=rep(1:10,each=10),C=1:100), aes(x=A,y=B,shape=factor(C)))+ geom_point(size=10, family="ionic")+ scale_shape_manual(values=intToUtf8(61970:62969, multiple=TRUE))+ theme(legend.position = "none") start <- 61970 dat <- data.frame( A=rep(1:10,10), B=rep(1:10,each=10), C=intToUtf8(start:(start+99), multiple=TRUE) ) ggplot(dat, aes(x=A,y=B,label=C))+ geom_text(size=10)#, family="serif") library(showtext) font_add(family = 'academicons', regular = 'academicons.ttf') font_add(family = 'FontAwesome', regular = 'fontawesome-webfont.ttf') font_add(family = 'ionicons', regular = 'ionicons.ttf') font_families() showtext_begin() strt <- 61440 dat <- data.frame( A=rep(1:10,10), B=rep(1:10, each=10), C=intToUtf8(strt:(strt+99),multiple=TRUE) ) ggplot(dat,aes(x=A,y=B,label="\uf006"))+ geom_text(size=10, family="FontAwesome") ggplot(data.frame(A=rep(1:10,10),B=rep(1:10,each=10),C=1:100), aes(x=A,y=B,shape=factor(C)))+ geom_point(size=10)+ scale_shape_manual(values=intToUtf8(9828:9927, multiple=TRUE))+ theme(legend.position = "none")
Malmquist Productivity Growth Index
Linear Programming for the Malmquist Productivity Growth Index [15] is a method to make uncertain prediction into the future based on existing trend data.
Html widgets
HTML widgets: Bring the best of JavaScript data visualization to R [16]
See also
- Double dots in a ggplot
- Visualising distributions
- Plotting means and error bars
- Cookbook for R: Scatterplots
- Scales of x and y axes
- Add a smoothed conditional mean
- theme_grey
- Different histograms
- Scale and flip graphs with coord_cartesian. Note that coord_flip() can take coord_cartesian() parameters.
- Parameters for geom_density (adjust, fill,...)