How to control label color depending on fill darkness of bars?

前端 未结 1 1541
不思量自难忘°
不思量自难忘° 2021-01-03 02:05

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 o

相关标签:
1条回答
  • 2021-01-03 02:54

    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)
    

    0 讨论(0)
提交回复
热议问题