簡體   English   中英

堆疊條形圖,每個條形都有顏色漸變

[英]Stacked barplot with colour gradients for each bar

我想為堆疊的條形圖着色,以便每個條都有自己的父顏色,每個條中的顏色是該父顏色的漸變。

例子:

這是一個最小的例子。 我希望每個條的顏色對於color是不同的,每個條內的漸變由 `clarity.

library(ggplot2)

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

在此處輸入圖像描述

在我真正的問題中,我有更多的組:需要 18 條不同的條和 39 種不同的漸變顏色。

我已經創建了一個ColourPalleteMulti函數,它允許您根據數據中的子組創建多個顏色的ColourPalleteMulti

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

本質上,該函數標識您擁有的不同組,然后計算每個組中的顏色數。 然后它將所有不同的調色板連接在一起。

要使用調色板,最簡單的方法是添加一個新的列group ,它將用於制作調色板的兩個值粘貼在一起:

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

在此輸入圖像描述


編輯

如果您希望每個條形圖中的條形圖不同,則可以更改用於繪制條形圖的變量的方式:

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

在此輸入圖像描述


注意事項 :誠實地說,您想要繪制的數據集可能包含太多子類別,因此無法使用。

此外,雖然這在視覺上非常令人愉悅,但我建議避免使用這樣的色標。 它更多的是使繪圖看起來漂亮,並且不同的顏色是多余的,因為我們已經知道數據來自X軸的哪個組。

實現顏色漸變的更簡單方法是使用alpha來改變顏色的透明度。 但是,這可能會產生意想不到的后果,因為透明度意味着您可以通過圖表查看指南。

library(ggplot2)

ggplot(diamonds, aes(color, alpha = clarity)) + 
  geom_bar(aes(fill = color), colour = "grey") +
  scale_alpha_discrete(range = c(0,1))

在此輸入圖像描述

我最近創建了創建此類圖的 package ggnested 它本質上是圍繞ggplot2的包裝器,它在美學映射中采用main_groupsub_group ,其中為main_group生成顏色,並為嵌套在sub_group的每個級別中的main_group的級別生成漸變。

devtools::install_github("gmteunisse/ggnested")
require(ggnested)
data(diamonds)
ggnested(diamonds, aes(main_group = color, sub_group = clarity)) +
  geom_bar(aes(x = color))

使用 ggnested 創建的嵌套條形圖示例

另一種選擇是使用任何自定義調色板,並根據填充類別簡單地變暗/變亮。 在每個條形中獲得平滑漸變可能會有些棘手,但如果您保持數據的自然順序(數據框中的外觀或因子級別),這不是一個大問題。

我正在使用色彩空間 package來完成這項任務。 陰影 package還可以選擇變暗/變亮 colors,但語法稍長。 更適合修改整個調色板,無需指定特定的 colors。

library(tidyverse)
library(colorspace)

## get some random colors, here n colors based on the Dark2 palette using the colorspace package.
## But ANY palette is possible
my_cols <- qualitative_hcl(length(unique(diamonds$color)), "Dark2")
## for easier assignment, name the colors
names(my_cols) <- unique(diamonds$color)

## assign the color to the category, by group
df_grad <-
  diamonds %>%
  group_by(color) %>%
  ## to keep the order of your stack and a natural gradient
  ## use order by occurrence in data frame or by factor
  ## clarity is an ordered factor, so I'm using a dense rank
  mutate(
    clarity_rank = dense_rank(as.integer(clarity)),
    new_cols = my_cols[color],
    ## now darken or lighten according to the rank
    clarity_dark = darken(new_cols, amount = clarity_rank / 10),
    clarity_light = lighten(new_cols, amount = clarity_rank / 10)
  )

## use this new color for your fill with scale_identity
## you additionally need to keep your ordering variable as group, in this case
## an interaction between color and your new rank
ggplot(df_grad, aes(color, group = interaction(color, clarity_rank))) +
  geom_bar(aes(fill = clarity_dark)) +
  scale_fill_identity()

ggplot(df_grad, aes(color, group = interaction(color, clarity_rank))) +
  geom_bar(aes(fill = clarity_light)) +
  scale_fill_identity()

代表 package (v2.0.1) 於 2022 年 7 月 3 日創建

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM