[英]Distinct scale_fill_gradient for each group in ggplot2
我绘制了几个与离散变量的类别相对应的密度值。 我可以同时为每个密度关联特定颜色或所有颜色的渐变颜色。 现在我想为每个密度添加一个具有不同值的特定梯度。
这是一个使用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)
# 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)
我基本上想要结合两个情节。 我也对每个密度的特定midpoint
值感兴趣。
这有些不太优雅,但是您可以向第一个代码添加第二次geom_density_gradient
调用,手动将颜色设置为白色,但是映射alpha就像这样: aes(alpha=Sepal.length)) +scale_alpha_continuous()
我提出了下面的解决方法作为一种好奇心,但就数据可视化而言,我认为这不是一个好的做法。 在密度图表中具有单个变化的梯度是不够的; 有多个不同的将不会更好。 请不要使用它。
制备:
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
包的两个函数)将它们添加到cowplot
,但这就像为已经很奇怪的可视化选择添加多余的内容。 ..
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.