ggplot2中每个组的不同scale_fill_gradient

时间:2018-07-25 09:05:26

标签: r 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)

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值。

2 个答案:

答案 0 :(得分:0)

这有点不雅致,但是您可以在第一个代码中向geom_density_gradient添加第二个调用,将颜色手动设置为白色,但是像这样映射alpha:aes(alpha=Sepal.length)) +scale_alpha_continuous()

答案 1 :(得分:0)

出于好奇,我想出了以下解决方法,但就数据可视化而言,我认为这不是真正的好习惯。在密度图中只有一个变化的梯度足够不稳定。有多个不同的人不会更好。请不要使用它。

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.plotget_legend的各个图中检索填充图例,并通过plot_grid将其添加到上方的图中(两个函数都来自cowplot包),但这就像在已经很奇怪的可视化选择中增加了装饰……