Difference between revisions of "Help:Drawing graphs"

From Testiwiki
Jump to: navigation, search
(Answer)
(Colours and ordering of bars)
Line 97: Line 97:
 
print(p)
 
print(p)
 
# dev.off()
 
# dev.off()
 +
</rcode>
 +
 +
===Colours and ordering of bars===
 +
 +
* [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]
 +
 +
<rcode graphics=1 embed=1>
 +
library(OpasnetUtils)
 +
library(ggplot2)
 +
library(RColorBrewer) # Makes color palettes http://www.cookbook-r.com/Graphs/Colors_%28ggplot2%29/
 +
# http://stackoverflow.com/questions/8197559/emulate-ggplot2-default-color-palette
 +
 +
a <- data.frame(
 +
X = c("A", "B", "C", "D"),
 +
Y = c(1, 3, 2, 4),
 +
FILL = c("Up", "Middle", "Down", "Up")
 +
)
 +
 +
ggplot(a, aes(x = reorder(X, Y, sum), weight = Y, fill = FILL)) + # X is reordered to decreasing order of Y.
 +
geom_bar() + coord_flip() + # Flip x and y axes
 +
theme_gray(base_size = 24) + # increase font
 +
labs( # Define labels
 +
title = "Environmental burden of disease in Finland",
 +
x = "",
 +
y = "Impact (DALY/year)"
 +
) +
 +
scale_fill_manual(values = brewer.pal(4, "Set2")) + # Use palette Set2 to define colours.
 +
# Always use 4 colours, as the palette depends on the amount of colours used.
 +
theme(legend.position = c(0.8, 0.2)) # Adjust the legend position.
 +
 
</rcode>
 
</rcode>
  

Revision as of 02:42, 22 March 2014


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.

+ Show code

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.

See also