使用
ggnewscale
该包装允许为同一美学设计多个刻度:
library(ggplot2)
library(ggnewscale)
ggplot(
data = df,
aes(
x = Time,
y = Type,
fill = Value
)
) +
geom_tile(
data = ~subset(.x, Category == "SS"),
height = 0.8,
width = 1
) +
scale_fill_gradient2(
limits = c(-3, 3),
breaks = c(-3, 0, 3),
low = "white",
mid = "mistyrose",
high = "#A63446",
midpoint = 0,
guide = guide_colorbar(
frame.colour = "black",
ticks.colour = "black"
),
name = "SS"
) +
ggnewscale::new_scale_fill() +
geom_tile(
data = ~subset(.x, Category == "BB"),
aes(
x = Time,
y = Type,
fill = Value
),
height = 0.8,
width = 1
) +
scale_fill_gradient(
limits = c(-3, 3),
breaks = c(-3, 0, 3),
low = "white",
high = "navy",
guide = guide_colorbar(
frame.colour = "black",
ticks.colour = "black"
),
name = "BB"
) +
theme_bw() +
theme(
legend.box = "horizontal"
)