堆叠条形图,每个条形都有颜色渐变 [英] Stacked barplot with colour gradients for each bar

查看:41
本文介绍了堆叠条形图,每个条形都有颜色渐变的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为堆叠的条形图着色,以便每个条形都有自己的父颜色,每个条形内的颜色都是该父颜色的渐变.

I want to color a stacked barplot so that each bar has its own parent colour, with colours within each bar to be a gradient of this parent colour.

示例:

这是一个最小的例子.我希望 color 的每个条的颜色都不同,每个条内的渐变由 `clarity.

Here is a minimal example. I would like for the color of each bar to be different for color, with a gradient within each bar set by `clarity.

library(ggplot2)

ggplot(diamonds, aes(color)) + 
  geom_bar(aes(fill = clarity), colour = "grey")

在我的实际问题中,我有更多的组:需要 18 个不同的条形和 39 种不同的渐变颜色.

In my real problem, I have many more groups of each: requiring 18 different bars with 39 different gradient colours.

推荐答案

我制作了一个函数 ColourPalleteMulti,它可以让您根据数据中的子组创建多色板:

I have made a function ColourPalleteMulti, which lets you create a multiple colour pallete based on subgroups within your data:

ColourPalleteMulti <- function(df, group, subgroup){

  # Find how many colour categories to create and the number of colours in each
  categories <- aggregate(as.formula(paste(subgroup, group, sep="~" )), df, function(x) length(unique(x)))
  category.start <- (scales::hue_pal(l = 100)(nrow(categories))) # Set the top of the colour pallete
  category.end  <- (scales::hue_pal(l = 40)(nrow(categories))) # set the bottom

  # Build Colour pallette
  colours <- unlist(lapply(1:nrow(categories),
                          function(i){
                            colorRampPalette(colors = c(category.start[i], category.end[i]))(categories[i,2])}))
  return(colours)
}

本质上,该函数确定您有多少个不同的组,然后计算每个组中的颜色数量.然后将所有不同的调色板连接在一起.

Essentially, the function identifies how many different groups you have, then counts the number of colours within each of these groups. It then joins together all the different colour palettes.

要使用调色板,最简单的方法是添加一个新列 group,它将用于制作调色板的两个值粘贴在一起:

To use the palette, it is easiest to add a new column group, which pastes together the two values used to make the colour palette:

library(ggplot2)

# Create data
df <- diamonds
df$group <- paste0(df$color, "-", df$clarity, sep = "")

# Build the colour pallete
colours <-ColourPalleteMulti(df, "color", "clarity")

# Plot resultss
ggplot(df, aes(color)) + 
  geom_bar(aes(fill = group), colour = "grey") +
  scale_fill_manual("Subject", values=colours, guide = "none")

编辑:

如果您希望每个条形图的颜色不同,您只需更改用于绘制条形图的变量的方式:

If you want the bars to be a different colour within each, you can just change the way the variable used to plot the barplot:

# Plot resultss
ggplot(df, aes(cut)) + 
  geom_bar(aes(fill = group), colour = "grey") +
  scale_fill_manual("Subject", values=colours, guide = "none")

注意事项:老实说,您要绘制的数据集可能包含太多子类别,因此无法正常工作.

A Note of Caution: In all honesty, the dataset you have want to plot probably has too many sub-categories within it for this to work.

此外,虽然这在视觉上非常令人愉悦,但我建议避免使用这样的色阶.更多的是为了让绘图看起来更漂亮,不同的颜色是多余的,因为我们已经从 X 轴知道数据在哪一组.

Also, although this is visually very pleasing, I would suggest avoiding the use of a colour scale like this. It is more about making the plot look pretty, and the different colours are redundant as we already know which group the data is in from the X-axis.

这篇关于堆叠条形图,每个条形都有颜色渐变的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆