gpt4 book ai didi

r - ggplot2 中每个组的不同 scale_fill_gradient

转载 作者:行者123 更新时间:2023-12-01 05:53:34 38 4
gpt4 key购买 nike

我绘制了与离散变量的类别相对应的几个值密度。
我可以同时为每个密度关联特定颜色或颜色渐变。
现在我想为每个具有不同值的密度添加一个特定的渐变。

这是一个使用 ggridges 的可重现示例:

data(iris)

library(ggplot2)
library(ggridges)
library(RColorBrewer)

cols <- brewer.pal(3, "BrBG")

# Plot with one color per group
ggplot(iris, aes(Sepal.Length, as.factor(Species))) +
geom_density_ridges(aes(fill = as.factor(Species))) +
scale_fill_manual("Sepal", values = cols)

enter image description here
# Plot with one gradient
ggplot(iris, aes(Sepal.Length, as.factor(Species))) +
geom_density_ridges_gradient(aes(fill = ..x..)) +
scale_fill_gradient2(low = "grey", high = cols[1], midpoint = 5)

enter image description here

我基本上想结合两个情节。我也有兴趣拥有一个特定的 midpoint每个密度的值。

最佳答案

我出于好奇想出了下面的解决方法,但就数据可视化而言,我认为这不是一个很好的做法。在密度图中有一个单一的变化梯度已经足够不稳定了;有多个不同的不会更好。请不要使用它。

plot

准备:

ggplot(iris, aes(Sepal.Length, as.factor(Species))) +
geom_density_ridges_gradient()
# plot normally & read off the joint bandwidth from the console message (0.181 in this case)

# split data based on the group variable, & define desired gradient colours / midpoints
# in the same sequential order.
split.data <- split(iris, iris$Species)
split.grad.low <- c("blue", "red", "yellow") # for illustration; please use prettier colours
split.grad.high <- cols
split.grad.midpt <- c(4.5, 6.5, 7) # for illustration; please use more sensible points

# create a separate plot for each group of data, specifying the joint bandwidth from the
# full chart.
split.plot <- lapply(seq_along(split.data),
function(i) ggplot(split.data[[i]], aes(Sepal.Length, Species)) +
geom_density_ridges_gradient(aes(fill = ..x..),
bandwidth = 0.181) +
scale_fill_gradient2(low = split.grad.low[i], high = split.grad.high[i],
midpoint = split.grad.midpt[i]))

阴谋:
# Use layer_data() on each plot to get the calculated values for x / y / fill / etc,,
# & create two geom layers from each, one for the gradient fill & one for the ridgeline
# on top. Add them to a new ggplot() object in reversed order, because we want the last
# group to be at the bottom, overlaid by the others where applicable.
ggplot() +
lapply(rev(seq_along(split.data)),
function(i) layer_data(split.plot[[i]]) %>%
mutate(xmin = x, xmax = lead(x), ymin = ymin + i - 1, ymax = ymax + i - 1) %>%
select(xmin, xmax, ymin, ymax, height, fill) %>%
mutate(sequence = i) %>%
na.omit() %>%
{list(geom_rect(data = .,
aes(xmin = xmin, xmax = xmax, ymin = ymin, ymax = ymax, fill = fill)),
geom_line(data = .,
aes(x = xmin, y = ymax)))}) +

# Label the y-axis labels based on the original data's group variable
scale_y_continuous(breaks = seq_along(split.data), labels = names(split.data)) +

# Use scale_fill_identity, since all the fill values have already been calculated.
scale_fill_identity() +
labs(x = "Sepal Length", y = "Species")

请注意,此方法不会创建填充图例。如果需要,可以从 split.plot 中的相应图中检索填充图例。通过 get_legend并通过 plot_grid 将它们添加到上面的图中(这两个函数都来自 cowplot 包),但这就像在已经很奇怪的可视化选择中添加装饰一样......

关于r - ggplot2 中每个组的不同 scale_fill_gradient,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51514994/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com