4
votes

I'm trying to color a bar plot on ggplot that shows the proportions. I call the plot using:

v2 <- ggplot(data = Visual2_Data, 
         mapping = aes(x = violation, y = ..prop.., group = 1, fill = violation)) +
      geom_bar() +
      facet_grid(~driver_race) +
      scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple"))+
      theme(axis.text.x = element_text(angle=90))

and my result is the following:

Despite using the fill and scale_fill_manual, the bars are still the default color.

As soon as I use ..count.. as y variable instead of ..prop.. and delete group = 1 :

v2 <- ggplot(data = Visual2_Data, 
         mapping = aes(x = violation, y = ..count.., fill = violation)) +
      geom_bar() +
      facet_grid(~driver_race) +
      scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple"))+
      theme(axis.text.x = element_text(angle=90))

I get the following:

and this is what I want, except I would like to have these colors in the first plot using y = ..prop.. and group = 1, instead of using y = ..counts... So is there a way to do this?

Thanks in advance

For reproducibility:

I have to note that it is a relatively big data set.

I work with the Colorado data from this source:

https://openpolicing.stanford.edu/data/

I tidied it a bit:

data <- read_csv() #insert data here

Visual2_Data <- data %>%
  subset(out_of_state == FALSE) %>%
  select(county_name, county_fips, police_department, driver_gender,          
         driver_age, driver_race, violation, search_conducted, 
         contraband_found, stop_outcome, is_arrested) %>%
  drop_na(county_name) %>%
  filter(driver_race != "Other",
         violation %in% c("Lights", "Speeding", "Safe movement", "License", 
                          "Seat belt", "Registration/plates"))

# After this I used the code for v2 which already is described above.

v2 <- ggplot(data = Visual2_Data, etcetera)
1
If you only remove group=1 from your first code (still using ..prop..) would it not work? Can you give us a sample for reproducibility?onlyphantom

1 Answers

3
votes

If you replace the fill = ... with fill = factor(..x..) you get the desired result:

  ggplot(diamonds, aes(x = color, y = ..prop.., fill = factor(..x..), group = 1)) +
    geom_bar() +
    facet_grid(~cut)+ 
    scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple", "black")) + 
    theme(axis.text.x = element_text(angle=90))

enter image description here


Alternatively, I always like to do my pre-processing beforehand. You could do this with:

 library(data.table)
  df <- setDT(copy(diamonds))[, .(N = .N), by = .(cut, color)][, .(prop = N/sum(N), color = color), by = cut]

  ggplot(data = df, 
             mapping = aes(x = color, y = prop, fill = color)) +
  geom_col() +
  facet_grid(~cut) +
  scale_fill_manual(values = c("red", "green", "yellow", "blue", "pink", "purple", "black"))+
  theme(axis.text.x = element_text(angle=90))