几个geom area ggplot的常见传说

时间:2015-11-02 10:33:17

标签: r ggplot2

我根据以下代码创建了一个包含几个geom_area的图:

library(ggplot2)

set.seed(1)
dat <- data.frame(matrix(rnorm(100, 10, 2), 100, 1))

dat_density <- data.frame(density(dat[, 1])[c("x", "y")])
quant <- quantile(dat[, 1], probs = seq(0, 1, 0.10))
library(RColorBrewer)
color_pal <- brewer.pal(length(quant)-1, "RdYlBu")

dens <- ggplot(data = dat_density, aes(x = x, y = y)) +
geom_line(size = 2)
for(i in 1:(length(color_pal))){
  dens <- dens +
  geom_area(data = subset(dat_density, x > quant[[i]] & x < quant[[i + 1]]), fill = color_pal[i])
  }
dens

如何为color_pal向量的每种颜色添加一个公共图例(对应于所有10%的数据区域)?

1 个答案:

答案 0 :(得分:2)

最简单的方法是在数据集中定义组

/usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require': cannot load such file -- mail (LoadError)
from /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/site_ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in `require'
from /ruby/send_mail:23:in `<main>'