我将以mtcars数据集为例。我想基于填充颜色在一个变量(cyl)和另一个变量(hp)之间转换相同的数据点。但是,比例设定为4:335之间的整个范围。因此,当显示cyl时,所有点看起来都相同,因为它们的颜色被“压扁”,因为缺乏更好的术语,所以hp的上限。我想要发生的是,在填充圆柱体时的色标是4:8,当显示hp时是52:335。这是一个最小的例子
library(ggplot2)
library(gganimate)
library(RColorBrewer)
library(grDevices)
myPalette <- colorRampPalette(rev(brewer.pal(11, "Spectral")))
anim <- ggplot(mtcars, aes(mpg, disp)) +
geom_point(shape = 21, colour = "black", size = 3, stroke = 0.5, show.legend = T, aes(fill = cyl)) +
geom_point(shape = 21, colour = "black", size = 3, stroke = 0.5, show.legend = T, aes(fill = hp)) +
scale_fill_gradientn(colours = myPalette(100))+
transition_layers(layer_length = 1, transition_length = 2) +
enter_fade() + enter_grow()
anim
一种选择是使用填充为一个变量,使用颜色为另一个,使用一些点形状使用填充和其他颜色的事实。我对结果并不是百分之百满意,可能需要进行一些调整才能获得匹配的磅值:
anim <- ggplot(mtcars, aes(mpg, disp)) +
geom_point(shape = 16, size = 3, show.legend = T, aes(colour = cyl)) +
geom_point(shape = 21, size = 3, stroke = 0, show.legend = T, aes(fill = hp)) +
scale_fill_gradientn(colours = myPalette(100))+
scale_colour_gradientn(colours = myPalette(100))+
transition_layers(layer_length = 1, transition_length = 2) +
enter_fade() + enter_grow()
这是另一种方法。这里需要更多的预处理,但我认为这种方法仍然有一些价值,因为对于更广泛的用例,它更适合在> 2个变量之间进行转换。
定义新数据集,其中聚集的列和填充值缩放到公共范围(0-1):
library(dplyr)
# modify dataset
mtcars2 <- mtcars %>%
select(mpg, disp, cyl, hp) %>%
tidyr::gather(key, value, -mpg, -disp) %>%
group_by(key) %>%
mutate(scaled.value = (value - min(value)) / diff(range(value))) %>%
ungroup()
> head(mtcars2)
# A tibble: 6 x 5
mpg disp key value scaled.value
<dbl> <dbl> <chr> <dbl> <dbl>
1 21 160 cyl 6 0.5
2 21 160 cyl 6 0.5
3 22.8 108 cyl 4 0
4 21.4 258 cyl 6 0.5
5 18.7 360 cyl 8 1
6 18.1 225 cyl 6 0.5
由于相同的图例比例将在动画期间应用于不同的值,因此我们需要不同的标签。我们可以在绘图中包含其他几何图层来模拟这个,同时抑制实际的填充图例。
# may need further tweaking, depending on the actual plot's dimensions; this worked
# sufficiently for me
legend.position <- c("xmin" = max(mtcars2$mpg) - 0.05 * diff(range(mtcars2$mpg)),
"xmax" = max(mtcars2$mpg) - 0.02 * diff(range(mtcars2$mpg)),
"ymin" = max(mtcars2$disp) - 0.2 * diff(range(mtcars2$disp)),
"ymax" = max(mtcars2$disp) - 0.01 * diff(range(mtcars2$disp)))
生成情节:
anim1 <- ggplot(mtcars2, aes(mpg, disp)) +
geom_point(aes(fill = scaled.value, group = interaction(mpg, disp, scaled.value)),
shape = 21, colour = "black", size = 3, stroke = 0.5) +
# pseudo legend title
geom_text(aes(x = legend.position[["xmin"]],
y = legend.position[["ymax"]],
label = key),
vjust = -1, hjust = 0, check_overlap = TRUE) +
# pseudo legend labels (tweak number of breaks, font size, etc., as needed)
geom_text(data = . %>%
group_by(key) %>%
summarise(y = list(modelr::seq_range(scaled.value, n = 5)),
label = list(modelr::seq_range(value, n = 5))) %>%
ungroup() %>%
tidyr::unnest() %>%
mutate(y = legend.position[["ymin"]] +
y * (legend.position[["ymax"]] - legend.position[["ymin"]])),
aes(x = legend.position[["xmax"]], y = y, label = as.character(round(label))),
hjust = 0, nudge_x = 0.5, size = 3, check_overlap = TRUE) +
# pseudo colour bar
annotation_custom(grob = grid::rasterGrob(rev(myPalette(100)),
width = unit(1, "npc"), height = unit(1, "npc")),
xmin = legend.position[["xmin"]],
xmax = legend.position[["xmax"]],
ymin = legend.position[["ymin"]],
ymax = legend.position[["ymax"]]) +
scale_fill_gradientn(colours = myPalette(100), guide = FALSE) +
labs(title = "{closest_state}") +
transition_states(key) +
enter_fade() +
enter_grow()
# reducing nframes to speed up the animation, since there are only two states anyway
animate(anim1, nframes = 20)