如何根据条形的填充暗度控制标签颜色? [英] How to control label color depending on fill darkness of bars?

查看:90
本文介绍了如何根据条形的填充暗度控制标签颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a stacked bar plot, labelled with geom_text. To increase the visibility of the labels, I want to set the label color to either white or black depending of the 'darkness' of their background, i.e. the fill color of the bars. Thus, darker bars should have a white label, and lighter bars should have a black label.

我从问题中的代码开始ggplot2中堆积的条形图,问题

I started with code from the question Showing data values on stacked bar chart in ggplot2, the answer to the question Is there a is light or is dark color function in R?. On top of that I'd like to show the label of the data value in 'white' or 'black' depending whether the stacked bar fill color is dark or not dark respectively.

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

I made two attempts. The first is using aes(colour=...) of the geom_text function but this one fails ... why?.

第二次尝试使用功能scale_colour_manual.但是在这里,堆叠的条形线也使用黑色或白色切换设置进行了着色".

The second attempt is using the function scale_colour_manual. But here the stacked bar lines are also "coloured" using the black or white toggling setting.

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

推荐答案

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

Two ways to solve this issue by playing with geom_bar color or size:

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)

这篇关于如何根据条形的填充暗度控制标签颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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