0

I adopt the answer from here:

library(ggplot2)
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)
ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5))

I would like to change the color of number frequency in every bar and change it from black to white. Is it possible to make it?

jay.sf
  • 60,139
  • 8
  • 53
  • 110
foc
  • 947
  • 1
  • 9
  • 26
  • 1
    Does this answer your question? [Change font color on stacked bar chart](https://stackoverflow.com/questions/58032567/change-font-color-on-stacked-bar-chart) – tjebo Jul 06 '20 at 18:21

2 Answers2

4

If I understand correctly, you just need to add color = "white" inside geom_text():

library(ggplot2)

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)

ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
    geom_bar(stat = "identity") +
    geom_text(size = 3, position = position_stack(vjust = 0.5), color = "white")

enter image description here

Count Orlok
  • 997
  • 4
  • 13
4

Do you mean something like this:

library(ggplot2)
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)
ggplot(Data, aes(x = Year, y = Frequency, fill = Category, label = Frequency)) +
  geom_bar(stat = "identity") +
  geom_text(size = 3, position = position_stack(vjust = 0.5),color='white')

enter image description here

Duck
  • 39,058
  • 13
  • 42
  • 84