【发布时间】:2021-12-04 10:02:33
【问题描述】:
我正在尝试创建一个图,其中显示同一组点的两个度量,一个具有离散比例,一个具有连续比例。我想并排展示这些情节,并将它们融入各个方面会很棒。不幸的是,我不知道如何在一个方面使用一种色标,而在另一个方面使用不同的色标。
library(tidyverse)
disc <- tibble(x = rnorm(100), y = rnorm(100), color = sample(1:3, 100, replace = TRUE), model = "discrete")
cont <- tibble(x = rnorm(100), y = rnorm(100), color = rnorm(100, 10), model = "continuous")
# want this to be discrete
ggplot(disc, aes(x = x, y = y, color = factor(color))) +
geom_point() + scale_color_discrete()
# want this to be continuous
ggplot(cont, aes(x = x, y = y, color = color)) +
geom_point() + scale_color_viridis_c()
# This would be prettier!
bind_rows( disc, cont ) %>%
ggplot(aes(x = x, y = y, color = color)) +
geom_point() +
facet_wrap(~model)
由reprex package 创建于 2021-10-16 (v2.0.0)
我意识到这可能超出了 facet 的预期用途。但我很难让地图以连贯的方式并排打印,我认为这可能是更可持续的捷径。
【问题讨论】:
-
这个问题:stackoverflow.com/questions/3805029/… 类似,不过是 11 岁。
-
在原生
ggplot2中实现多色标并不容易,有一些包支持它(ggnewscale、gg4hx和relayer是我想到的三个)。另一种方法是制作两个完全不同的图并使用patchwork组合它们。