geom_vline,图例和性能

时间:2019-02-27 11:47:41

标签: r ggplot2 geom-vline

我想在绘图上绘制几条垂直线,并为每个对应的vline设置图例。

阅读this post之后,我将执行以下操作:

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)

现在该情节:

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))

效果很好:

enter image description here

但这很慢:

utilisateur     système      écoulé 
     51.667       1.883      53.652 

(对不起,我的系统是法语)

但是,当我这样做时(在aes之外使用xintercept):

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))

图例不显示:

enter image description here

但这要快得多:

utilisateur     système      écoulé 
      1.193       0.270       1.496 

我怎么能同时显示两个方面的优点?

1 个答案:

答案 0 :(得分:2)

您可以使用第一种方法,但是将空的data.frame作为data中的geom_vline参数传递。速度问题是由于geom_vlinemy.df中用data = data.frame()为每一行绘制一条线而引起的,它只绘制了一次。

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