對于這種型別的資料:
set.seed(123)
df <- data.frame(
Q = c(rep("q_pol",10), rep("q_wh",10)),
slope = c(rnorm(10,-0.5), rnorm(10, 0.5)),
Recipient = rep(c("A", "B"),10)
)
如何用兩種不同的顏色為這些箱線圖的主題(或背景)著色:值 > 0的上半部分,例如“淺藍色”,值 < 0的下半部分,例如“深藍色”:
library(ggplot2)
ggplot(df,
aes(x = Q, y = slope, color = Recipient))
geom_boxplot(notch = TRUE)
uj5u.com熱心網友回復:
一種選擇是使用geom_rect以下方法添加不同的填充背景:
library(ggplot2)
ggplot(df,
aes(x = Q, y = slope, color = Recipient))
geom_rect(data = data.frame(
xmin = c(-Inf, -Inf),
xmax = c(Inf, Inf),
ymin = c(-Inf, 0),
ymax = c(0, Inf),
fill = c("darkblue", "lightblue")
), aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill), inherit.aes = FALSE, alpha = .5)
scale_fill_manual(values = c("darkblue" = "darkblue", "lightblue" = "lightblue"), guide = "none")
geom_boxplot(notch = TRUE)

轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/409165.html
標籤:
