Ben
Ben

Reputation: 6751

geom_vline, legend and performance

I want to plot a few vertical lines on my plot and have a legend for each corresponding vline.

After reading this post, here is what I implemented:

set.seed(99)
df.size <- 1e6
my.df <- data.frame(dist = rnorm(df.size, mean = 0, sd = 2))
library(ggplot2)
ggplot(my.df, aes(x=dist)) + geom_histogram(binwidth = 0.5)

vline1.threshold <- mean(my.df$dist)
vline2.threshold <- mean(my.df$dist) + 3*sd(my.df$dist)

Now for the plot:

g <- ggplot(my.df, aes(x = dist)) +
  geom_histogram(binwidth = 0.5) +
  geom_vline(aes(color = "vline1", xintercept = vline1.threshold)) +
  geom_vline(aes(color = "vline2", xintercept = vline2.threshold)) +
  scale_color_manual("Threshold", values = c(vline1 = "red", vline2 = "blue"), labels = c("Mean", "Mean + 3*SD"))
system.time(print(g))

This works quite well:

enter image description here

But it's very slow:

utilisateur     système      écoulé 
     51.667       1.883      53.652 

(Sorry, my system is in French)

However, when I do this (with xintercept outside the aes):

g <- ggplot(my.df, aes(x = dist)) +
  geom_histogram(binwidth = 0.5) +
  geom_vline(aes(color = "vline1"), xintercept = vline1.threshold, color = "red") +
  geom_vline(aes(color = "vline2"), xintercept = vline2.threshold, color = "blue") +
  scale_color_manual("Threshold", values = c(vline1 = "red", vline2 = "blue"), labels = c("Mean", "Mean + 3*SD"))
system.time(print(g))

The legend is not displayed:

enter image description here

But it's much faster:

utilisateur     système      écoulé 
      1.193       0.270       1.496 

How can I have the best of both worlds, i.e. a legend, displaying quickly?

Upvotes: 2

Views: 966

Answers (1)

pogibas
pogibas

Reputation: 28369

You can use first method, but pass empty data.frame as a data argument in geom_vline. Speed problem is caused by geom_vline plotting line for each row in my.df with data = data.frame() it's plotted only once.

g2 <- ggplot(my.df, aes(x = dist)) +
  geom_histogram(binwidth = 0.5) +
  # pass empty data.frame as data
  geom_vline(aes(color = "vline1", xintercept = vline1.threshold), data.frame()) +
  # pass empty data.frame as data
  geom_vline(aes(color = "vline2", xintercept = vline2.threshold), data.frame()) +
  scale_color_manual("Threshold", values = c(vline1 = "red", vline2 = "blue"), labels = c("Mean", "Mean + 3*SD"))

# OPs solution
# system.time(print(g))
#   user  system elapsed 
# 36.636   1.714  38.397 

# data.frame() solution
# system.time(print(g2))
#   user  system elapsed 
#  2.203   0.265   2.504 

Upvotes: 5

Related Questions