背景带有g中的ggplot

dax*_*x90 4 r ggplot2

我正在尝试为不同的群体创建箱形图.我想在3个水平带中为背景着色.中心区域,其中所有观察结果都接近整体平均值

平均值(重量)-0.5 <x <平均值(重量)+0.5

其他2个频段是下方和上方.

Theese是我的情节

library(ggplot2)
bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) + geom_boxplot()
bp
Run Code Online (Sandbox Code Playgroud)

Ric*_*son 6

用途geom_rect:

bp <- ggplot(data=PlantGrowth, aes(x=group, y=weight, fill=group)) +
    geom_rect(ymin = -Inf, ymax = lwWt, 
              xmin = -Inf, xmax = Inf, fill = 'blue') +
    geom_rect(ymin = lwWt, ymax = upWt, 
              xmin = -Inf, xmax = Inf, fill = 'pink') + 
    geom_rect(ymin = upWt, ymax = Inf, 
          xmin = -Inf, xmax = Inf, fill = 'skyblue') +
    geom_boxplot() 
print(bp)
ggsave("example.jpg", bp)
Run Code Online (Sandbox Code Playgroud)

这给了你这个数字:在此输入图像描述

希望你能改变背景颜色:)