我绘制了与离散变量的类别相对应的几个值密度。我可以同时为每个密度关联特定颜色或颜色渐变。现在我想为每个具有不同值的密度添加一个特定的渐变。
这是一个可重现的示例,使用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)
Run Code Online (Sandbox Code Playgroud)
# 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)
Run Code Online (Sandbox Code Playgroud)
我基本上想结合两个情节。我也对midpoint每个密度有一个特定的值感兴趣。
出于好奇,我想出了下面的解决方法,但就数据可视化而言,我认为这并不是一个很好的实践。密度图中只有一个变化的梯度就已经足够不稳定了;拥有多个不同的不会更好。请不要使用它。
准备:
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]))
Run Code Online (Sandbox Code Playgroud)
阴谋:
# 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")
Run Code Online (Sandbox Code Playgroud)
请注意,此方法不会创建填充图例。如果需要,可以从split.plotvia中的各个图中检索填充图例get_legend并将它们添加到上面的图中via plot_grid(包中的两个函数cowplot),但这就像向已经很奇怪的可视化选择添加装饰......
| 归档时间: |
|
| 查看次数: |
1250 次 |
| 最近记录: |