【问题标题】:Add gradient color within groups in ggplot2在 ggplot2 中的组内添加渐变颜色
【发布时间】:2021-08-28 05:22:14
【问题描述】:

我需要帮助才能为 ggplot 对象(特别是 geom_bar)添加颜色。

这是我的数据

Names       Family          Groups    Values
H.sapiens   A               G1        2
H.erectus   A               G1        6 
H.erectus   B               G2        12
M.griseus   C               G2        3
A.mellifera D               G3        3
L.niger     D               G3        8
H.erectus   D               G3        2
L.niger     A               G1        3
L.niger     B               G2        3
A.mellifera A               G1        8

到目前为止,我成功地创建了这个情节:

使用此代码:

library(ggplot2)
library(ggstance)
library(ggthemes)
ggplot(table, aes(fill=Family, y=Names, x=Values)) + 
  geom_barh(stat="identity",colour="white")+ theme_minimal() +
  scale_x_continuous(limits = c(0,60), expand = c(0, 0))

现在我想根据组更改颜色。更准确地说,我想为每个组选择一种主要颜色,例如:G1= blue ; G2 = Green ; G3= Red

并为每个家庭在这些颜色中获得渐变。例如,B 是深蓝色,C 是浅蓝色。

有人有想法吗?

这是数据:

dput(table)
structure(list(Names = structure(c(3L, 2L, 2L, 5L, 1L, 4L, 2L, 
4L, 4L, 1L), .Label = c("A.mellifera", "H.erectus", "H.sapiens", 
"L.niger", "M.griseus"), class = "factor"), Family = structure(c(1L, 
1L, 2L, 3L, 4L, 4L, 4L, 1L, 2L, 1L), .Label = c("A", "B", "C", 
"D"), class = "factor"), Groups = structure(c(1L, 1L, 2L, 2L, 
3L, 3L, 3L, 1L, 2L, 1L), .Label = c("G1", "G2", "G3"), class = "factor"), 
    Values = c(2L, 6L, 12L, 3L, 3L, 8L, 2L, 3L, 3L, 8L)), class = "data.frame", row.names = c(NA, 
-10L))

【问题讨论】:

  • 在一张图表中使用多个色标,只能通过一些变通方法来实现。见this

标签: r ggplot2 geom-bar


【解决方案1】:

您可能会根据您的要求调整这个(我已经稍微更改了您的示例数据以向您展示同一组之间的不同梯度)

df <- read.table(header = T, text = "Names       Family          Groups    Values
H.sapiens   A               G1        2
H.erectus   B               G1        6 
H.erectus   B               G2        12
M.griseus   C               G2        3
A.mellifera D               G3        3
L.niger     D               G3        8
H.erectus   A               G3        2
L.niger     A               G1        3
L.niger     B               G2        3
A.mellifera C               G1        8")

library(tidyverse)
df %>% ggplot() +
  geom_col(aes(x = Names, y = Values, fill = Groups, alpha = as.integer(as.factor(Family)))) +
  coord_flip() +
  scale_fill_manual(name = "Groups", values = c("blue", "green", 'red')) +
  scale_alpha_continuous(name = "Family", range = c(0.2, 0.7)) +
  theme_classic()

reprex package (v2.0.0) 于 2021 年 6 月 12 日创建

【讨论】:

  • 这个解决方案非常干净,使用 alpha 是个好主意。我建议删除背景灰色。
  • @zx8754 谢谢。这个想法是只显示领先。事实上,还有很多其他的事情需要处理,比如标签等。不仅仅是传说
【解决方案2】:

我们可以为每个 Group 创建颜色范围,然后按照 Family 的顺序进行匹配。您可能需要使用colours 来使差异更加突出:

cols <- lapply(list(G1 = c("darkblue", "lightblue"),
                    G2 = c("darkgreen", "lightgreen"),
                    G3 = c("red4", "red")),
               function(i) colorRampPalette(i)(length(unique(table$Family))))

table$col <- mapply(function(g, i) cols[[ g ]][ i ], 
                    g = table$Groups, i = as.numeric(table$Family))

ggplot(table, aes(x = Values, y = Names, fill = col )) + 
  geom_barh(stat = "identity", colour = "white") +
  scale_x_continuous(limits = c(0, 60), expand = c(0, 0)) +
  scale_fill_identity() +
  theme_minimal()

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-03-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多