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

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

问题描述

我想对堆叠的条形图进行着色,以使每个条形都有自己的父色,并且每个条形内的颜色都是该父色的渐变。



示例:



这里是一个最小的示例。我希望每个条形的颜色对于颜色是不同的,并且每个条形内的渐变都由净度设置。

 库(ggplot2)

ggplot(钻石,aes(颜色))+
geom_bar(aes(填充=净度),颜色= 灰色)



在我的实际问题中,每个组都有更多的组:需要18种不同的色条以及39种不同的渐变颜色。

解决方案

我已经创建了一个函数 ColourPalleteMulti ,该函数可让您根据数据中的子组创建多种颜色的调色板:

  ColourPalleteMulti<-函数(df,组,子组){

#查找要创建的颜色类别和颜色数量在每个
类别中<-聚合(as.formula(paste(s ubgroup,group,sep =〜)),df,function(x)length(unique(x)))
category.start<--(scales :: hue_pal(l = 100)(nrow(categories )))#设置颜色调色板
类别的顶部.end<-(scales :: hue_pal(l = 40)(nrow(categories)))#设置底部

#构建颜色调色板
颜色<-取消列表(lapply(1:nrow(categories),
function(i){
colorRampPalette(colors = c(category.start [i],category .end [i]))(类别[i,2])}))
return(colours)
}

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



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

  library(ggplot2)

#创建数据
df<-钻石
df $ group<-paste0(df $ color,-,df $ clarity,sep =)

#建立颜色调色板
colors< -ColourPalleteMulti(df, color, clarity)

#绘制结果
ggplot(df,aes(color)) +
geom_bar(aes(fill = group),color = grey)+
scale_fill_manual( Subject,values = colours,guide = none)






编辑



如果您希望条形图的颜色不同,则可以更改方式用于绘制条形图的变量:

 #绘制结果
ggplot(df,aes(cut))+
geom_bar(aes(fill = group),color = grey)+
scale_fill_manual( Subject,values = colours,guide = none)







注意事项:老实说,要绘制的数据集中可能有太多子类别,因此无法正常工作。 / p>

此外,尽管这在视觉上非常令人愉悦,但我还是建议避免使用这样的色标。这更多的是使绘图看起来更漂亮,并且不同的颜色是多余的,因为我们已经从X轴知道了数据所在的组。



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.

Example:

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")

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

解决方案

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.

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")


Edit:

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.

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天全站免登陆