Search code examples
rggplot2geom-bar

How to Change colors IF in geom_bar ggplot2, IFelse dont work


Why the operator if does not work in this context?

enter image description here

The main file with data

    structure(list(age = c("0-4", "05-09", "10-14", "15-19", "20-24", 
"25-29", "30-34", "35-39", "40-44", "45-49", "50-54", "55-59", 
"60-64", "65-69", "70-74", "75-79", "80-84", "85+", "0-4", "05-09", 
"10-14", "15-19", "20-24", "25-29", "30-34", "35-39", "40-44", 
"45-49", "50-54", "55-59", "60-64", "65-69", "70-74", "75-79", 
"80-84", "85+", "0-4", "05-09", "10-14", "15-19", "20-24", "25-29", 
"30-34", "35-39", "40-44", "45-49", "50-54", "55-59", "60-64", 
"65-69", "70-74", "75-79", "80-84", "85+", "0-4", "05-09", "10-14"
), gender = c("F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", "F", 
"F", "F", "F", "F", "F", "M", "M", "M"), year = c(2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 2011, 
2011, 2011, 2011, 2011, 2011, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 2001, 
2001, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 
1991, 1991, 1991, 1991, 1991, 1991, 1991, 1991, 2011, 2011, 2011
), pop = c(25.4759696688669, 21.129961995163, 20.711726947066, 
27.4762242467223, 30.8584728965505, 31.4039968723292, 38.3503354972451, 
39.695961304166, 31.476733402433, 31.6403905951667, 32.6405178840943, 
38.2048624370374, 38.7867546778681, 28.4945356681759, 21.4390922481043, 
17.5658720200753, 14.3109122979288, 9.47393304602404, 21.1892611493233, 
27.4018193920568, 30.7299755935212, 31.8208712373345, 39.4571407440278, 
39.8639153908735, 31.0627912136676, 31.2846682937653, 33.2815620146439, 
39.4756305007026, 40.751423711264, 30.7854448635456, 25.3494564011538, 
24.0921529472672, 25.330966644479, 21.6699948228681, 8.20945196361216, 
1.92293469417942, 29.7360023208587, 31.567304902814, 40.125471424427, 
40.9413983173774, 31.404119524224, 31.3134609805628, 31.9299390774587, 
38.6568030171163, 40.361183637946, 30.1892950391645, 25.1486800116043, 
26.2365825355382, 29.3552364374819, 30.4794023788802, 16.1372207716855, 
18.53060632434, 13.6713083841021, 6.67246881346098, 28.4036150055462, 
22.8210863200771, 22.020984488935)), row.names = c(NA, 57L), class = "data.frame")
    

It was the part of the file with basic data. It is not clear how to make different colors for the left and right sides of 2001. I want to make a comparison of two age structures, and the ideal would be to make different colors on either side of the graph for only one year

library(dplyr)
        library(tidyr)
        library (ggthemes)
        library(readxl)
        library(ggplot2)
        pyramidRak1 <- read_excel("~/MetodyVSG/U3/Rstudio/pyramidRak1.xlsx")
        df3= data.frame(pyramidRak1)
        rgb.val <- col2rgb("white")
        t.col <- rgb(rgb.val[1], rgb.val[2], rgb.val[3],
                     max = 255,
                     alpha = (100 - 99) * 255 / 100,
                     names = "blue")
        
        ggplot(data = df3, aes(x = age, y = pop, fill = (as.factor(year)), color=gender,width = 1)) +
          geom_bar(data = df3 %>% filter(gender == "F", year != 2011) %>% arrange(rev(year)),
                   stat = "identity",
                   position = "identity") +
          geom_bar(data = df3 %>% filter(gender == "M", year != 2011) %>% arrange(rev(year)),
                   stat = "identity",
                   position = "identity",
                   mapping = aes(y = -pop)) +
          coord_flip() +
          scale_y_continuous(labels= abs, limits = (45)*c(-1,1)) +
          geom_hline(yintercept = 0) +
          scale_fill_economist() +
          labs(title = NULL, x = "Věk", y = "Může                                   Podíl obyvatel (v ‰)                                   Ženy",face="bold")+ 
          theme_bw()+  
          guides(fill=guide_legend(title=NULL))+
         scale_fill_manual(values=c(t.col,if ("gender" == "M") {"blue"} else {"pink"}))
        

enter image description here


Solution

  • Not sure whether I got everything right.

    As you want the fill color to both reflect the year and the gender you could map the interaction of year and gender on fill and set the fill color for each combination via scale_fill_manual.

    Additionally I simplified your code a little.

    library(ggplot2)
    library(dplyr)
    
    df4 <- df3 %>%
      filter(year != 2011) %>% 
      mutate(gender_year = interaction(gender, year)) %>% 
      # Sort data
      arrange(year)
    
    ggplot(data = df4, aes(x = age, y = ifelse(gender == "F", pop, -pop), fill = gender_year), width = 1) +
      geom_bar(stat = "identity", position = "identity") +
      scale_y_continuous(labels= abs, limits = (45)*c(-1,1)) +
      coord_flip() +
      geom_hline(yintercept = 0) +
      labs(title = NULL, x = "Vek", y = "Muze Podil obyvatel (v %) Zeny", fill = NULL) +
      theme_bw() +
      scale_fill_manual(values = c(F.1991 = "pink", M.1991 = "blue", F.2001 = "red", M.2001 = "red")) +
      theme(
        axis.title.x = element_text(color = "black", size = 14, face = "bold"),
        axis.title.y = element_text(color = "black", size = 14, face = "bold")
    

    enter image description here