我正在用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))

在此处输入图像描述

R相关问答推荐

这两种创建S4对象的方法有何不同?

当y大于阈值和值范围时,在时间序列中突出显示区域

使用split.zoo界定xts物体的降水事件

将收件箱变量传递给ggplot 2函数

使用gsim删除特殊词

使用facet_wrap()时如何将面板标题转换为脚注?

如何使用Cicerone指南了解R Shiny中传单 map 的元素?

使用rlang s arg_match判断函数输入列表

如何使用`ggplot2::geom_segment()`或`ggspatial::geom_spatial_segment()`来处理不在格林威治中心的sf对象?

如果第一个列表中的元素等于第二个列表的元素,则替换为第三个列表的元素

自动变更列表

在连续尺度上转置标签[瀑布图,R]

使用R闪光显示所有数据点作为默认设置

如果可能,将数字列转换为整数,否则保留为数字

从外部文件读取多个值作为字符向量

R-按最接近午夜的时间进行筛选

如何使用前缀作为匹配来连接数据帧?

优化从每个面的栅格中提取值

在点图上绘制置信度或预测区间ggplot2

使用ifElse语句在ggploy中设置aes y值