ggplot2

ggplot2 is my favorite R library to plot data

Alluvial diagrams

Requires the ggalluvial package, on top of ggplot2 (via Mathieu Perona).

Alluvial diagrams are like this (my own production):

You can also use networkD3 with the function sankeyNetwork() (via Antoine Belgodere): more here. An interactive example can be found here (via Pauline R.).

Animate graphs

Bar plots

Simple bar plot

Suppose you have those data in df:

[ninja_tables id=”577″]

You want to plot how many n are in each year in a simple bar plot.

plot <- ggplot(df, aes(x = year, y = n)) +
  geom_bar(stat = "identity")
plot

Do NOT forget to add stat = "identity" in geom_bar(). It tells ggplot2 to “count” n.

Here’s the result of the aforementioned code. Of course you can customize its look if you are not happy with the colours, and so on.

Percentage bar plot

plot <- ggplot(df, aes(x = var1, y = (..count..)/sum(..count..))) + 
  geom_bar()
plot

Source and more details.

More sophisticated code, not tested yet:

ggplot(df, aes(x= var1,  group=var_group)) + 
  geom_bar(aes(y = ..prop.., fill = factor(..x..)), stat="count") +
    geom_text(aes( label = scales::percent(..prop..),
                   y= ..prop.. ), stat= "count", vjust = -.5) +
    labs(y = "Percent", fill="var1") +
    facet_grid(~var_group) +
    scale_y_continuous(labels = scales::percent)

Requires the library scales.

(Same source)

Plot several series in one single bar plot

ggplot(df, aes(x=var1, y=(..count..), fill=var2)) +
  geom_bar(stat="identity", position="dodge")

If the variable used with fill is kind of categorical (for instance it’s years), beware to be sure it’s a factor (as.factor(var2)). Otherwise it may be treated as a continuous variable, and displayed incorrectly.

(..count..) means that ggplot2 will display the count of var1 for each value of var2.

Draw lines

How to draw straight lines in a ggplot graph?

Vertical line

Add geom_vline(xintercept = 10) on the ggplot2 object to add a vertical line at x = 10.

Horizontal line

Similarly, to draw a horizontal line on a ggplot2 object, use geom_hline(yintercept = 10). Change yintercept to your desired y value.

Lines

Multiple lines

Themes

  • ggthemr: quite promising (see here) but I never managed to make it work properly