【问题标题】:How to control label color depending on fill darkness of bars?如何根据条形的填充暗度控制标签颜色?
【发布时间】:2018-04-08 08:34:48
【问题描述】:

我有一个堆积条形图,标有geom_text。为了增加标签的可见性,我想根据背景的“暗度”将标签颜色设置为白色或黑色,即条形的填充颜色。因此,较暗的条应该有一个白色的标签,而较浅的条应该有一个黑色的标签。

我从问题Showing data values on stacked bar chart in ggplot2 的代码开始,问题Is there a is light or is dark color function in R? 的答案。最重要的是,我想将数据值的标签显示为“白色”或“黑色”,具体取决于堆叠条的填充颜色是深还是不深。

我做了两次尝试。第一个是使用geom_text 函数的aes(colour=...),但是这个失败了......为什么?

第二次尝试是使用函数scale_colour_manual。但这里堆叠的条形线也使用黑色或白色切换设置“着色”。

library(ggplot2)
library(RColorBrewer)

Year      <- c(rep(c("2006-07", "2007-08", "2008-09", "2009-10"), each = 4))
Category  <- c(rep(c("A", "B", "C", "D"), times = 4))
Frequency <- c(168, 259, 226, 340, 216, 431, 319, 368, 423, 645, 234, 685, 166, 467, 274, 251)
Data      <- data.frame(Year, Category, Frequency)

isDark <- function(color) {
  (sum(grDevices::col2rgb(color) *c(299, 587,114))/1000 < 123)
}

## control the color assignments
paletteName <- 'Set1' # 'Dark2'
colorsPerCat <- brewer.pal(name=paletteName,n=4)
names(colorsPerCat) <- c("A", "B", "C", "D")

## First attempt
Data$LabelColor <- as.character(as.vector(sapply(unlist(colorsPerCat)[Data$Category], function(color) { if (isDark(color)) 'white' else 'black' })))
Data
p <- ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(aes(colour=LabelColor), size = 3, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = colorsPerCat)
p

## Second attempt
labelColoursPerCat <- sapply(as.vector(colorsPerCat), function(color) { if (isDark(color)) 'white' else 'black' })
names(labelColoursPerCat) <- c("A", "B", "C", "D")
p <- ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency, colour = Category)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5)) +
  scale_fill_manual(values = colorsPerCat) +
  scale_colour_manual(values = labelColoursPerCat)
p

【问题讨论】:

标签: r ggplot2


【解决方案1】:

通过使用geom_bar colorsize 来解决此问题的两种方法:

Data$LabelColor <- as.factor(Data$LabelColor)
p <- ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency, colour = LabelColor)) +
              geom_bar(stat = "identity", color = "black") +
              geom_text(size = 3, position = position_stack(vjust = 0.5)) +
              scale_fill_manual(values = colorsPerCat) +  
              scale_colour_manual(values = levels(Data$LabelColor)) +
              guides(colour = FALSE)

Data$LabelColor <- as.factor(Data$LabelColor)
p <- ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency, colour=LabelColor)) +
              geom_bar(stat = "identity", size = 0) +
              geom_text(size = 3, position = position_stack(vjust = 0.5)) +
              scale_colour_manual(values = levels(Data$LabelColor)) +
              scale_fill_manual(values = colorsPerCat) +  
              guides(colour = FALSE)

【讨论】:

    猜你喜欢
    • 2021-01-18
    • 1970-01-01
    • 1970-01-01
    • 2014-10-27
    • 2021-03-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-31
    相关资源
    最近更新 更多