ggplot2 中各个方面的顺序调色板
Sequential Colour Palettes Across Facets in ggplot2
我正在构建一个仪表板,其中包含按访问者数量排列的前 10 个位置的棒棒糖图表,按年份分类。这是我使用虚拟数据构建的大致相似的图:
要按每个方面的访问者总数重新排序位置,I used reorder_within() and scale_x_reorder(), created by Tyler Rinker。对于顺序调色板,我使用了 RColorBrewer 和 scale_color_distiller.
关于条形图的颜色,我有三处想要更改,但我不确定如何更改其中的任何一项。
- 我希望颜色从深一点开始,而不是接近白色,因为它们有点难看。
- 我希望每个栏都有自己的颜色,即使访问者数量相同,并且这些颜色彼此之间可以清楚地区分,同时仍然是连续的。
- 我希望每个方面都有相同的配色方案,这样看起来就一致了 - 我知道方案在各个方面有所不同,因为它们都有不同的访问者数量。
下面带有一些虚拟数据的可重现示例:
library(tidyverse)
library(RColorBrewer)
#facet reorder code - by Tyler Rinker
reorder_within <-
function(x,
by,
within,
fun = mean,
sep = "___",
...) {
new_x <- paste(x, within, sep = sep)
stats::reorder(new_x, by, FUN = fun)
}
scale_x_reordered <- function(..., sep = "___") {
reg <- paste0(sep, ".+$")
ggplot2::scale_x_discrete(
labels = function(x)
gsub(reg, "", x),
...
)
}
dummy <- data.frame(
Year = rep(c(2019, 2020, 2021), c(10)),
Destination = c("loc_4322", "loc_43267", "loc_6786", "loc_43294", "loc_45566",
"loc_1234", "loc_367", "loc_14765", "loc_49865", "loc_90765",
"loc_4332", "loc_367", "loc_2112", "loc_596111", "loc_54980",
"loc_539", "loc_5699", "loc_1965", "loc_6387", "loc_213",
"loc_5245", "loc_4787", "loc_34098", "loc_67609", "loc_50954",
"loc_54421", "loc_548901", "loc_23245", "loc_4322", "loc_0986"),
Visitor_numbers = c(102234, 234984, 39546, 108943, 430985, 243056, 342890,
253980, 129803, 134954, 21954, 128904, 223242, 223242,
223242, 23242, 243980, 134324, 542323, 12545, 905334,
123434, 123434, 569085, 5085, 235463, 209384, 230923,
243089, 120923)
)
destinations <- dummy %>%
ggplot() +
(aes(
x = reorder_within(Destination, -Visitor_numbers, Year),
y = Visitor_numbers,
color = Visitor_numbers
)) +
geom_point(size = 4) +
geom_segment(
aes(
x = reorder_within(Destination, -Visitor_numbers, Year),
xend = reorder_within(Destination, -Visitor_numbers, Year),
y = 0,
yend = Visitor_numbers
),
size = 2
) +
scale_x_reordered() +
scale_color_distiller(type = "seq", palette = "Oranges", direction = 1) +
facet_wrap(~ Year,
dir = "v",
scales = "free",
ncol = 1) +
coord_flip()
destinations
如有任何帮助,我们将不胜感激。
我建议在一年内按等级或缩放值着色。以下是两种可能性:
destinations <- dummy %>%
group_by(Year) %>%
mutate(Visitor_numbers_rank = min_rank(Visitor_numbers)) %>%
#mutate(Visitor_numbers_scaled = Visitor_numbers/max(Visitor_numbers)) %>%
ungroup() %>%
ggplot(aes(
x = reorder_within(Destination, -Visitor_numbers, Year),
y = Visitor_numbers,
color = Visitor_numbers_rank
)) +
guides(color = "none") +
...
要修改调色板以使最亮的值变暗,您可以扩展颜色限制以包括小于任何数据的值,从而有效地移动颜色以从更高的比例开始。我知道等级不会低于 1,所以色阶从 -5 开始会改变一切。
scale_color_distiller(type = "seq", palette = "Oranges", direction = 1,
limits = c(-5, NA)) +
我正在构建一个仪表板,其中包含按访问者数量排列的前 10 个位置的棒棒糖图表,按年份分类。这是我使用虚拟数据构建的大致相似的图:
要按每个方面的访问者总数重新排序位置,I used reorder_within() and scale_x_reorder(), created by Tyler Rinker。对于顺序调色板,我使用了 RColorBrewer 和 scale_color_distiller.
关于条形图的颜色,我有三处想要更改,但我不确定如何更改其中的任何一项。
- 我希望颜色从深一点开始,而不是接近白色,因为它们有点难看。
- 我希望每个栏都有自己的颜色,即使访问者数量相同,并且这些颜色彼此之间可以清楚地区分,同时仍然是连续的。
- 我希望每个方面都有相同的配色方案,这样看起来就一致了 - 我知道方案在各个方面有所不同,因为它们都有不同的访问者数量。
下面带有一些虚拟数据的可重现示例:
library(tidyverse)
library(RColorBrewer)
#facet reorder code - by Tyler Rinker
reorder_within <-
function(x,
by,
within,
fun = mean,
sep = "___",
...) {
new_x <- paste(x, within, sep = sep)
stats::reorder(new_x, by, FUN = fun)
}
scale_x_reordered <- function(..., sep = "___") {
reg <- paste0(sep, ".+$")
ggplot2::scale_x_discrete(
labels = function(x)
gsub(reg, "", x),
...
)
}
dummy <- data.frame(
Year = rep(c(2019, 2020, 2021), c(10)),
Destination = c("loc_4322", "loc_43267", "loc_6786", "loc_43294", "loc_45566",
"loc_1234", "loc_367", "loc_14765", "loc_49865", "loc_90765",
"loc_4332", "loc_367", "loc_2112", "loc_596111", "loc_54980",
"loc_539", "loc_5699", "loc_1965", "loc_6387", "loc_213",
"loc_5245", "loc_4787", "loc_34098", "loc_67609", "loc_50954",
"loc_54421", "loc_548901", "loc_23245", "loc_4322", "loc_0986"),
Visitor_numbers = c(102234, 234984, 39546, 108943, 430985, 243056, 342890,
253980, 129803, 134954, 21954, 128904, 223242, 223242,
223242, 23242, 243980, 134324, 542323, 12545, 905334,
123434, 123434, 569085, 5085, 235463, 209384, 230923,
243089, 120923)
)
destinations <- dummy %>%
ggplot() +
(aes(
x = reorder_within(Destination, -Visitor_numbers, Year),
y = Visitor_numbers,
color = Visitor_numbers
)) +
geom_point(size = 4) +
geom_segment(
aes(
x = reorder_within(Destination, -Visitor_numbers, Year),
xend = reorder_within(Destination, -Visitor_numbers, Year),
y = 0,
yend = Visitor_numbers
),
size = 2
) +
scale_x_reordered() +
scale_color_distiller(type = "seq", palette = "Oranges", direction = 1) +
facet_wrap(~ Year,
dir = "v",
scales = "free",
ncol = 1) +
coord_flip()
destinations
如有任何帮助,我们将不胜感激。
我建议在一年内按等级或缩放值着色。以下是两种可能性:
destinations <- dummy %>%
group_by(Year) %>%
mutate(Visitor_numbers_rank = min_rank(Visitor_numbers)) %>%
#mutate(Visitor_numbers_scaled = Visitor_numbers/max(Visitor_numbers)) %>%
ungroup() %>%
ggplot(aes(
x = reorder_within(Destination, -Visitor_numbers, Year),
y = Visitor_numbers,
color = Visitor_numbers_rank
)) +
guides(color = "none") +
...
要修改调色板以使最亮的值变暗,您可以扩展颜色限制以包括小于任何数据的值,从而有效地移动颜色以从更高的比例开始。我知道等级不会低于 1,所以色阶从 -5 开始会改变一切。
scale_color_distiller(type = "seq", palette = "Oranges", direction = 1,
limits = c(-5, NA)) +