How to change fill colors based on x-axis values in R?

Hi all,

This is a follow-up to this question. Thanks to the help from @FJCC , I modified the code a bit. But it still did meet my expectation and I failed to find a solution for filling different colors based on the x-axis values.

My aim is to fill diary and journal with different colors like this. I appreciate your help and your time!

# test dataset
# A tibble: 10 x 4
    date gender genre   group
   <dbl> <chr>  <chr>   <chr>
 1  1778 female diary   s    
 2  1778 female journal of   
 3  1778 male   diary   s    
 4  1778 male   diary   of   
 5  1778 male   journal s    
 6  1782 female diary   of   
 7  1782 female diary   s    
 8  1782 female journal of   
 9  1782 female journal s    
10  1782 male   journal of 

ggplot(test) +
  geom_bar(mapping = aes(x = interaction(date, genre, lex.order = TRUE), fill = group, color = group), 
           position = "fill", width=0.7) +
  geom_segment(mapping = aes(x=x,y=y, xend=xend, yend= yend), 
               data = data.frame(x=2.5, xend=2.5, y =0, yend = -0.2)) +
  labs(y = "Percentage") +
  theme_bw() +
  theme(legend.position = "none",
        plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        axis.title = element_text(size=rel(1.5)), axis.text.y = element_text(size=rel(1.5))) +
  annotate(geom = "text", x = 1:4, y = -0.05, label = c("diary", "journal", "diary", "journal"), size = 6) +
  annotate(geom = "text", x = c(1.5, 3.5), y = -0.15, label = c("1778", "1782"), size = 7) +
  coord_cartesian(ylim = c(-0.2, 1), expand = FALSE, clip = "off", xlim = c(0.5, 4.5))

Here are two methods to get the kind of coloring you want, I think.

library(ggplot2)
test <- read.csv("~/R/Play/Dummy.csv")
#change transparency (alpha) by group
ggplot(test) +
  geom_bar(mapping = aes(x = interaction(date, genre, lex.order = TRUE), 
                         fill = genre, color = genre, alpha = group), 
           position = "fill", width=0.7) +
  geom_segment(mapping = aes(x=x,y=y, xend=xend, yend= yend), 
               data = data.frame(x=2.5, xend=2.5, y =0, yend = -0.2)) +
  labs(y = "Percentage") +
  theme_bw() +
  theme(legend.position = "none",
        plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        axis.title = element_text(size=rel(1.5)), axis.text.y = element_text(size=rel(1.5))) +
  annotate(geom = "text", x = 1:4, y = -0.05, label = c("diary", "journal", "diary", "journal"), size = 6) +
  annotate(geom = "text", x = c(1.5, 3.5), y = -0.15, label = c("1778", "1782"), size = 7) +
  coord_cartesian(ylim = c(-0.2, 1), expand = FALSE, clip = "off", xlim = c(0.5, 4.5)) + 
  scale_alpha_manual(values = c("s" = 1, "of" = 0.4))


#fill by interaction(genre, group)
ggplot(test) +
  geom_bar(mapping = aes(x = interaction(date, genre, lex.order = TRUE), 
                         fill = interaction(genre, group)), 
           position = "fill", width=0.7) +
  geom_segment(mapping = aes(x=x,y=y, xend=xend, yend= yend), 
               data = data.frame(x=2.5, xend=2.5, y =0, yend = -0.2)) +
  labs(y = "Percentage") +
  theme_bw() +
  theme(legend.position = "none",
        plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank(),
        axis.title = element_text(size=rel(1.5)), axis.text.y = element_text(size=rel(1.5))) +
  annotate(geom = "text", x = 1:4, y = -0.05, label = c("diary", "journal", "diary", "journal"), size = 6) +
  annotate(geom = "text", x = c(1.5, 3.5), y = -0.15, label = c("1778", "1782"), size = 7) +
  coord_cartesian(ylim = c(-0.2, 1), expand = FALSE, clip = "off", xlim = c(0.5, 4.5)) + 
  scale_fill_manual(values = c(journal.s = "blue", journal.of = "skyblue",
                               diary.s = "firebrick3", diary.of = "firebrick1"))

Created on 2021-01-27 by the reprex package (v0.3.0)

Thank you soooo much for your continuous help!

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.