我正在用ggplot2软件包制作一个简单的R柱状图.我想将背景划分为五个区域,每个区域的 colored颜色 不同(但同样低调),而不是默认的灰色.我该怎么做?
更具体地说,我希望这五个 colored颜色 区域的范围为0-25、25-45、45-65、65-85和85-100,其中的 colored颜色 分别比青铜、青铜、银、金和铂更差. colored颜色 方案的建议也很受欢迎.
我正在用ggplot2软件包制作一个简单的R柱状图.我想将背景划分为五个区域,每个区域的 colored颜色 不同(但同样低调),而不是默认的灰色.我该怎么做?
更具体地说,我希望这五个 colored颜色 区域的范围为0-25、25-45、45-65、65-85和85-100,其中的 colored颜色 分别比青铜、青铜、银、金和铂更差. colored颜色 方案的建议也很受欢迎.
下面是一个让你开始的例子:
#Fake data
dat <- data.frame(x = 1:100, y = cumsum(rnorm(100)))
#Breaks for background rectangles
rects <- data.frame(xstart = seq(0,80,20), xend = seq(20,100,20), col = letters[1:5])
#As Baptiste points out, the order of the geom's matters, so putting your data as last will
#make sure that it is plotted "on top" of the background rectangles. Updated code, but
#did not update the JPEG...I think you'll get the point.
ggplot() +
geom_rect(data = rects, aes(xmin = xstart, xmax = xend, ymin = -Inf, ymax = Inf, fill = col), alpha = 0.4) +
geom_line(data = dat, aes(x,y))