我正在嘗試在 ggplot 中制作一個圖形,其中顏色、形狀和大小映射到一個變數,如下所示:0 值顯示為紅十字。> 0 的值顯示為圓圈,圓圈大小和顏色根據變數縮放(即圓圈越大,值越高)。我想對顏色使用分箱的 viridis 比例。映射到顏色的值隨機變化,因此不應硬編碼縮放。下圖:
library(tidyverse)
x <- tibble(x = sample(1:100, 10), y = sample(1:100, 10), z = c(0, sample(1:1e6, 9)))
color_breaks <- sort(unique(c(0, 1, pretty(x$z, n = 5), ceiling(max(x$z)))))
ggplot(x, aes(x = x, y = y, color = z, shape = z == 0, size = z))
geom_point(stroke = 1.5)
scale_shape_manual(values = c(`TRUE` = 3, `FALSE` = 21), guide = "none")
scale_size(range = c(1, 8),
breaks = color_breaks,
limits = c(0, ceiling(max(x$z)))
)
binned_scale(aesthetics = "color",
scale_name = "stepsn",
palette = function(x) c("red", viridis::viridis(length(color_breaks) - 3)),
limits = c(0, ceiling(max(x$z))),
breaks = color_breaks,
show.limits = TRUE
)
guides(color = guide_legend(), size = guide_legend())
theme_bw()

由
uj5u.com熱心網友回復:
您可以覆寫內部的美學guides:
x <- tibble(x = sample(1:100, 10), y = sample(1:100, 10), z = c(0, sample(1:1e6, 9)))
color_breaks <- sort(unique(c(0, pretty(x$z, n = 5)[-6], ceiling(max(x$z)) 1)))
ggplot(x, aes(x = x, y = y, color = z, shape = z == 0, size = z))
geom_point(stroke = 1.5)
scale_shape_manual(values = c(`TRUE` = 3, `FALSE` = 21), guide = "none")
scale_size(range = c(1, 8),
breaks = color_breaks,
limits = c(-1, ceiling(max(x$z)) 2)
)
binned_scale(aesthetics = "color",
scale_name = "stepsn",
palette = function(x) c("red", viridis::viridis(length(color_breaks) - 1)),
limits = c(-1, ceiling(max(x$z)) 2),
breaks = color_breaks,
show.limits = FALSE
)
guides(color = guide_legend(),
size = guide_legend(override.aes = list(shape = c(3, rep(16, 5)))))
theme_bw()

轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/453814.html
