pretty_legend: helper function to create nice legends

Description Usage Arguments Value Examples

View source: R/plots.R

Description

helper function to create nice legends

Usage

1
pretty_legend(gg, last_vals, xmin, fontsize = 13)

Arguments

gg

ggplot2 object

last_vals,

data frame with columns label, colour, last_vals (i.e. place label with colour at y-coordinate last_vals)

xmin

Numeric, x axis position at which labels should be placed

fontsize

Integer, fontsize

Value

Another ggplot2 object

This replaces the default legend of a ggplot2 object. In particular, given a ggplot2 object ,it removes the existing legend and then places new labels based on the annotation data frame 'last_vals' (see parameter description) at a given x-coordinate of the original plot.

This function can be attributed to and is described in more detail in the following blog post: http://www.r-bloggers.com/coloring-and-drawing-outside-the-lines-in-ggplot/

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
  library("ggplot2")
  labels  <- c("A","B","C")
  mypoints <- rbind(data.frame(y=1:3, x=1, label=as.factor(labels)),
                     data.frame(y=2:4, x=2, label=as.factor(labels)))
  mycolours <- c("#F8766D","#00BA38","#619CFF")
  gg <- ggplot(mypoints,aes(x=x,y=y,color=label)) + 
               geom_line(size=2) + 
               scale_color_manual(values=mycolours) +
               xlim(c(0,2.2))
  gg
  annotation_df <- data.frame(colour=mycolours, last_vals=2:4, label=labels)
  pretty_legend(gg, annotation_df, 2.1)

nignatiadis/ihwPaper documentation built on Jan. 18, 2021, 3:13 p.m.